UART is the abbreviation for Universal Asynchronous Receiver/Transmitter and is a commonly used hardware module for serial communication based on communication protocols like RS232. This chapter discusses how such a device is appropriately interfaced with an 8051 microcontroller.
[fusion_imageframe image_id=”2883″ style_type=”none” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”no” gallery_id=”” lightbox_image=”” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2016/11/UART-interfacing-PIC18F4550-1-1-1024×666.png[/fusion_imageframe]
The DB9 connector is an analog connector mainly used for serial connections for the asynchronous transmission of data as provided by RS-232 standard. Here is the pinout of PC DB9 connector.
[fusion_imageframe image_id=”1956″ style_type=”none” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”no” gallery_id=”” lightbox_image=”” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2016/09/PC-DB9-pinout-1-232×300.png[/fusion_imageframe]
These are the registers that must be configured for UART peripheral.
SCON is an 8-bit register for configuring serial control register.
[fusion_imageframe image_id=”4662″ style_type=”none” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”no” gallery_id=”” lightbox_image=”” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2018/03/UART_SCON-1024×154.png[/fusion_imageframe]
The serial port mode is selection:
[fusion_imageframe image_id=”4665″ style_type=”none” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”no” gallery_id=”” lightbox_image=”” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2018/03/UART_SMOD-1024×248.png[/fusion_imageframe]
In mode 0, serial data are transmitted and received through the RXD pin, while the TXD pin output clocks. The baud rate is fixed at 1/12 the oscillator frequency. On transmit, the least significant bit (LSB bit) is sent/received first.
In mode 1, 10 bits are transmitted through the TXD pin or received through the RXD pin in the following manner: a START bit (always 0), 8 data bits (LSB first) and a STOP bit (always 1). The START bit is only used to initiate data receive, while the STOP bit is automatically written to the RB8 bit of the SCON register.
In mode 2, 11 bits are transmitted through the TXD pin or received through the RXD pin: a START bit (always 0), 8 data bits (LSB first), a programmable 9th data bit and a STOP bit (always 1). On transmit, the 9th data bit is actually the TB8 bit of the SCON register. This bit usually has a function of parity bit. On receive, the 9th data bit goes into the RB8 bit of the same register (SCON).The baud rate is either 1/32 or 1/64 the oscillator frequency.
Mode 3 is the same as Mode 2 in all respects except the baud rate. The baud rate in Mode 3 is variable.
The parity bit is the P bit of the PSW register. The simplest way to check the correctness of the received byte is to add a parity bit to it. Simply, before initiating data transmit, the byte to transmit is stored in the accumulator and the P bit goes into the TB8 bit in order to be “a part of the message”. The procedure is opposite on receive, received byte is stored in the accumulator and the P bit is compared with the RB8 bit. If there are no variations the data received is correct.
[fusion_imageframe image_id=”4664″ style_type=”none” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”no” gallery_id=”” lightbox_image=”” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2018/03/UART_PCON-1024×108.png[/fusion_imageframe]
Baud Rate = Oscillator frequency in HZ / N[256-(TH1)]
Baud Rate is the rate of bits sent or received per second. In case of the UART, baud rate depends on the selected mode, oscillator frequency and in some cases it also depends on the state of the SMOD bit of the SCON register.
The main criteria for UART communication is its baud rate. Both the devices Receiver and Transmitter should be set to same baud rate for successful communication.
For the 8051 the Timer 1 is used to generate the baud rate in Auto reload mode.
The crystal frequency Fosc is divided by 12 internally which is used to execute instructions also known as Machine Clock, Mosc. The timer again divides the Mosc by 32 and uses it as the timer frequency, say Tosc.
Mosc = Fosc / 12 = 16MHz / 12 = 1333.33KHz
Tosc = Mosc / 32 = 1333.33KHz / 32 = 41.667KHz
If we look at the standard baud rates used for serial communication shown in the table below, we can observe that all the baud rates are factors or multiples of the Tosc. This results in low error rates.
[fusion_imageframe image_id=”4663″ style_type=”none” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”no” gallery_id=”” lightbox_image=”” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2018/03/UART_BD-300×159.png[/fusion_imageframe]
The above factors should be loaded to Timer1(TH1) in Mode2 in order to generate the required baud rate. The final formula for baud rate is as below.
Baudrate = Fosc / (N * (256 – TH1))
TH1 = 256 – (Fosc / (384 * Baudrate)) //If( SMOD1==0 in PCON register)
TH1 = 256 – (Fosc / (192 * Baudrate)) //If( SMOD1==1 in PCON register)
Now with Fosc = 16Mhz, TH1 value for 9600 baudrate will be:
TH1 = 256-(16*10^6)/(192 * 9600)
= 247 = 0xF7
SCON=0x50; // Configure serial control register PCON=0x80; // SMOD bit set TMOD=0x20; // Using timer1,8-bit reload mode for baudrate generation TH1=0xF7; // 9600 baudrate(16 mhz clock) TR1=1; // Start timer
unsigned char read(){ unsigned char character; while(!RI); // Wait until character received completely character=SBUF; // Read the character from buffer reg RI=0; // Clear the flag return character; // Return the received character }
void write(unsigned char character){ SBUF=character; // Load the character to be transmitted in to the buffer reg while(!TI); // Wait until transmission complete TI=0; // Clear flag }
In the below example, using an 8051 microcontroller we are writing a string to the UART port, then read the character from the port and then send them back. We’re utilizing above functions along with new functions to print strings.