Categories: Firmware Guide-8051

UART in 8051 microcontroller

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.

UART Interfacing with 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]

Register configuration

These are the registers that must be configured for UART peripheral.

  1. PCON: Power Control Register.
  2. SCON: SCON is an 8-bit register for configuring serial control register.
  3. TCON: Timer Control Register for Baud Rate Generator (For more details refer chapter for Timer)
  4. TMOD: Timer Mode Control for Baud Rate Generator (For more details refer chapter for Timer)
  5. SBUFF: Serial Buffer holds the data to be transmitted and the data received

SCON

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]

  • FE – Framing error bit.
  • SM0 – This bit is used for serial port mode selection.
  • SM1 – This bit is used for serial port mode selection.
  • SM2 – This bit is used for serial port mode selection, also known as multiprocessor communication enable bit. When set, it enables multiprocessor communication in mode 2 and 3, and eventually mode 1. It should be cleared in mode 0.
  • RENReception Enable bit enables serial reception when set. When cleared, serial reception is disabled.
  • TB8Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem of transmitting the 9th bit in modes 2 and 3. It is set to transmit a logic 1 in the 9th bit.
  • RB8 – Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if 9th bit received is a logic 0. Set by hardware if 9th bit received is a logic 1.
  • TITransmit Interrupt flag is automatically set at the moment the last bit of one byte is sent. It’s a signal to the processor that the line is available for a new byte to transmit. It must be cleared from within the software.
  • RIReceive Interrupt flag is automatically set upon one byte to receive. It signals that byte is received and should be read quickly prior to being replaced by new data. This bit is also cleared from within the software.

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]

Mode 0

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.

Transmission and Reception process

  • Transmission – Data transmit is initiated by writing data to the SBUF register. In fact, this process starts after any instruction being performed upon this register. When all 8 bits have been sent, the TI bit of the SCON register is automatically set.
  • Reception – Data receive through the RXD pin are recognized when the following two conditions are met: bit REN=1 and RI=0. When all 8 bits have been received, the RI bit of the SCON register is automatically set indicating that one-byte receive is complete. Since there is no START and STOP bits or any other bit except data sent from the SBUF register in the pulse sequence, this mode is mainly used when the distance between devices is short, noise is minimized and operating speed is improved.

Mode 1

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.

Transmission and Reception process

  • Transmission – Data transmit is initiated by writing data to the SBUF register. End of data transmission is indicated by setting the TI bit of the SCON register.
  • Reception – The START bit (logic zero (0)) on the RXD pin initiates data receive. The following two conditions must be met: bit REN=1 and bit RI=0. Both of them are stored in the SCON register. The RI bit is automatically set upon data reception is complete. The Baud rate in this mode is determined by the timer 1 overflow.

Mode 2

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.

Transmission and Reception process

  • Transmission – Data transmit is initiated by writing data to the SBUF register. End of data transmission is indicated by setting the TI bit of the SCON register.
  • Reception – The START bit (logic zero (0)) on the RXD pin initiates data receive. The following two conditions must be met: bit REN=1 and bit RI=0. Both of them are stored in the SCON register. The RI bit is automatically set upon data reception is complete.

Mode 3

Mode 3 is the same as Mode 2 in all respects except the baud rate. The baud rate in Mode 3 is variable.

In short

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.

PCON – Power Control Register

[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]

  • SMOD – Serial mode bit used to determine the baud rate with Timer 1.
  • POF – Power-Off Flag

Baud Rate = Oscillator frequency in HZ / N[256-(TH1)]

  • If SMOD1 = 0 then N = 384. If SMOD1 = 1 then N = 192. TH1 is the high byte of Timer 1 when it is in 8-bit auto-reload mode. GF1 and GF0 are General purpose flags not implemented on the standard device
  • PD is the power-down bit. Not implemented on the standard device
  • IDL activate the idle mode to save power. Not implemented on the standard device

Baud Rate Calculation

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

Firmware

  • Register configuration for UART with a baud rate of 9600
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
  • Function for read input through UART
  • The data received from the other device will be stored in the SBUF register. To read the character received, we have to wait until the receive buffer is full.
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
}
  • Function for Transmitting character through UART
  • Setting the register bits appropriately would configure the UART module for the desired baud rate. The transmission of a character serially using the module would require the character to be loaded to the SBUF register and wait until the transmission is complete.
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
}

8051 UART – A complete Firmware example

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.



[fusion_global id=”18383″]
Share