Programming 8051 UART to Transmit Data
1. Set the Timer 1 of AT89C2051 to operate in Mode 2 (8-bit auto reload). To do this, load the TMOD register with 20h. The Timer 1 of AT89C2051 is the one responsible to UART baud rates. More about AT89C2051 timers here.
2. Load the TH1 register with a value that will set the desired baud rate. To calculate the value to be loaded to the TH1 register, use the following formula:

Example:
Find the value to be loaded to TH1 if the crystal frequency is 11.0592MHz and the desired baud rate is 9600bps.
Solution:
First, divide the clock frequency (11.0592MHz) by 12
11059200/12 = 921600
Divide the resulting value by 32
921600/32 = 28800
Divide the resulting value by the desired baud rate (9600bps)
28800/9600 = 3
Therefore, the value to be loaded to TH1 is -3. To get the equivalent of -3 to hexadecimal, subtract 3 from FFh and add 1.
FFh +1h – 3h = FDh
To get 9600bps baud rate for UART, load TH1 with FDh.
It is important to choose a standard baud rate when setting up a communication using UART. The standard baud rates are the following: 1200bps, 2400bps, 4800bps, 9600bps, 19200bps, 38400bps, 57600bps, 115200bps, etc
3. Load the SCON register with a value that will set the UART to operate in Mode 1 with 8bits, 1 start bit, 1 stop bit, no parity bit, and receive enabled. To do this, load SCON with 50h.
4. Start Timer 1 by setting TR1.
5. Clear TI. If TI is set, there will be no transmission of data.
6. Load the SBUF register with a character byte to be transferred.
7. Monitor if the TI flag is set. Once it is set, clear it.
8. To transfer another character byte, go to step 6.
Example:
Write a program that will enable AT89C2051 to transmit the word “ONE” using the UART that operates in Mode 1, 8 bits, 1 start bit, 1 stop bit, and no parity bit at 9600bps baud rate. The crystal frequency is 11.0592MHz.
Solution:
#include<reg51.h>
void main(void)
{ TMOD = 0x20; //Timer 1, mode 2 (auto-reload)
TH1 = 0xFD; //load TH with -3 or FDh
SCON = 0x50; //UART mode 1, receive enabled
TR1 = 1; //start Timer1
TI = 0; //clear TI
SBUF = 'O'; //send 'O'
while(!TI)
{;} //monitor if TI is set
TI = 0; //clear TI once TI is set
SBUF = 'N'; //send 'N'
while(!TI)
{;} //monitor if TI is set
TI = 0; //clear TI once TI is set
SBUF = 'E'; //send 'E'
while(!TI)
{;} //monitor if TI is set
TI = 0; //clear TI once TI is set
while(1); //loop forever
}
