UART in 8051 microcontroller

///UART in 8051 microcontroller

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

uart-interfacing-pic18f4550

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.

pc-db9-pinout

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.

  • 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 receive. It signals that byte is received and should be read quickly prior to being replaced by a new data. This bit is also cleared from within the software.

The serial port mode is selection:

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 are 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

  • 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 autoreload 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 the UART, baud rate depends on 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.

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

  • 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.

  • Function for Transmitting character through UART
  • Setting the register bits appropriately would configure the UART module for 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.

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this

Leave A Comment

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
close-link
25% OFF - OpenLabPro Course
Use Coupon: CRS25UL

 
8051 Microcontroller Primer
Want to learn indepth programming, peripheral interfacing and driver development?
close-link
Launched: IoT online Course - 25% Off when you complete your order today.
Checkout Now
close-image

Subscribe To our Email Newsletter

Be the first to get latest updates and exclusive content straight to your email inbox.
Send me Tutorials and Guides
close-link