8051 consists of a programmable serial port with two transmission lines, Rx(receive) and Tx(transmit). The data is serially transmitted through Tx pin and received through the Rx pin. In 8051 UART communication, data can be transmitted and received simultaneously which makes it a full duplex communication. The number of bits transmitted or received per second(Baud rate) through UART can be obtained by setting the appropriate bits in the SCON register.
The other registers associated with 8051 UART communication are SBUF, TCON and SMOD bit in PCON register.
Let us see the role of each register one by one.
SCON – Serial Control Register(Bit addressable).
SM0 and SM1 (Serial port mode bits)
|SM1||SM0||Serial Mode||Description||Baud rate|
|0||0||Mode 0||Shift register||Fixed (Fosc/12)|
|0||1||Mode 1||8 – bit UART||Variable|
|1||0||Mode 2||9 – bit UART||Fixed (Fosc/32) or (Fosc/64)|
|1||1||Mode 3||9 – bit UART||Variable|
Mode 0: It is an 8-bit half-duplex mode. Both transmission and reception are done through RxD line, whereas the TxD line provides the clock for the data transfer. The baud rate in fixed in Mode 0 and is given by oscillator frequency/12. This mode is generally not used for communication between computers or devices.
Mode 1: It is a 10-bit full duplex mode, i.e transmission and reception can occur simultaneously. It consists of 1 start bit, 1 stop bit and 8 data bits. Data to be transmitted or received is loaded into SBUF register(See code below). Once data is transmitted successfully, TI bit of SCON register is set automatically and must be cleared by the programmer. During reception of data, the 10th bit i.e the stop bit is stored in RB8. If RB8 = 1 (indicating a valid stop bit), the RI bit of SCON register will be set, and must be cleared by the programmer.
The baud rate in mode 1 is given by: ((2^SMOD )/32) * Timer 1 overflow frequency (will be discussed later). The bit SMOD in PCON can be used to double the baud rate(by setting it to 1).
Mode 2: In mode 2, 11 bits are involved. Just like mode 1, it has 1 start bit, 1 stop bit, 8 data bits and an additional programmable bit, which is the 9th bit of the 11 bits that are transmitted or received. During transmission, the 9th bit transmitted is actually the value contained in the TB8 bit of SCON register. Whereas during reception, this 9th bit is received and stored in the RB8 bit of SCON register. This bit is generally used for parity checking. Baud rate in Mode 2 is fixed.
Baud rate = ((2^SMOD )/64) * oscillator frequency.
Mode 3: This mode is same as that of mode 2 except that the baud rate is variable. Just like in mode 1, the baud rate can be adjusted by timer overflow frequency.
SM2: (Serial port mode bit 2)
This bit is used for multiprocessor communication. When you have a master device which wants to communicate to different slaves, Multiprocessor feature is used in mode 2 and 3.
REN: (Receiver enable bit)
This bit must be set for enabling the reception.
This bit is not used in mode 0 and 1. In mode 2 and 3, it is used to transmit the 9th programmable bit. I.e you can write into this bit and its content would be transmitted as the 9th bit in mode 2 and 3.
In mode 0, this bit is not used. In mode 1, it receives the stop bit(1 being a valid stop bit). In mode 2 and 3, It stores the 9th bit during reception.
TI: (Transmit Interrupt)
This bit is set automatically when the transmission of one byte is complete. This bit must be cleared by the programmer.
RI: (receive interrupt)
This bit is set automatically when one complete byte is received. Like TI this bit must also be cleared by the programmer.
SMOD bit in PCON register
This bit is used to double the baud rate of the timer in mode 1,2 and 3.
For e.g. in mode 1 the formula for baud rate is given by ((2^SMOD )/32) * Timer 1 overflow frequency. If you set SMOD as 1, Timer 1 overflow frequency will be multiplied by 1/16 which would essentially double the baud rate.
SBUF register holds the data to be transmitted. I.e the programmer writes into SBUF during transmission. During reception, the data received is stored in SBUF. It is 2 registers, one for transmission and one for reception, with both have same address space.
Timer calculation for generating appropriate baud rate
In mode 1 and 3, Timer 1 is used to obtain the baud rate. There are 4 different modes in which 8051 timers can operate. For generating baud rate, 8051 timer is used in mode 2 i.e Auto reload mode.
In the programs below I’ve used Serial port mode 1, where the baud rate is given by ((2^SMOD )/32) * Timer 1 overflow frequency.
The Timer overflow frequency is given by Fosc/12*(256-TH1).
Setting SMOD to 0, the equation becomes, Baud rate = (1/32)*(11.0596Mhz/(12*(256-TH1))).
For Baud rate = 9600, The value obtained for TH1 is 253 which is FDh in hexadecimal. This is the value that needs to be loaded in TH1 for obtaining the baud rate.
The reason Fosc is divided by 12 is that in 8051 one machine cycle consists of 12 clock pulses. A machine cycle is the minimum number of clock pulse required for the execution of a single 8051 instruction. There are instructions which may consume more than one machine cycle. So essentially 8051 consumes 12 clock pulses to execute a single sized instruction. Hence the clock frequency 11.0596MHz has to be divided by 12.
8051 UART ASSEMBLY CODE
mov scon, #42h
mov tmod, #20h
mov th1, #0fdh
tx: mov sbuf, a
jnb ti, $
jnb ri, $
mov a, sbuf
8051 UART C CODE
void transmit(unsigned char ch1);
unsigned char receive();
unsigned char ch;
SCON = 0x52;
TMOD = 0x20;
TH1 = 0xfd;
TR1 = 1;
void transmit(unsigned char ch1)
SBUF = ch1;
while (TI ==0);
TI =0 ;
unsigned char receive()
ch2 = SBUF;