Home/ Tutorials / AVR/ 8051/ How to interface computers Serial Port (RS232) with Atmel AT89S51/89S52 Microcontroller?
Atmel AT89S51/52 microcontroller has an integrated UART module for carrying serial communication. Serial communication makes use of asynchronous mode of operation. Serial port is defined as an interface between the PC and a device for transfer of data. AT89S51/52 with a serial port will allow reading and writing values to and from computer. Also note that the transfer of data through a serial port is bit by bit.
Block diagram showing Interfacing of 8051 Microcontroller with Serial Port
The block diagram consists of:
DB-9 RS-232 is a serial I/O standard, used commonly in PCs and other devices.
The output of RS232 is not compatible with the TTL. Inorder to connect RS232 to AT89S51/52 microcontroller, a converter is required. Here we make use of MAX232. This can convert the output of the microcontroller to the RS232 output level and vice versa. Usually, MAX232 consists of two line drivers for the transmission and reception of data.
AT89s51/52 uses SCON and SBUF registers for serial communication. SBUF is basically a 8-bit serial communication register. For serial transmission of datas, the data is first placed in SBUF . Also when a data is received serially, it comes in the SBUF register.
Program to transmit the character 'B' serially
Program to receive the character 'B' serially
Serial communication between the computer and the microcontroller follows the below steps:
Step1: Set the serial port mode.
Step2: Set the serial port baud rate.
Step3: Write and read serial port.
Step1
Inorder to configure the serial port, AT89S51/52 uses the SCON register. The SCON register consists of the following registers as in the figure: In the program for serial transmission and reception, SCON is entered with the value 0x40.
BIT |
NAME |
EXPLANATION |
7 |
SM0 |
Serial port mode bit0 |
6 |
SM1 |
Serial port mode bit1 |
5 |
SM2 |
Multiprocessor Communication Enable |
4 |
REN |
Receiver Enable |
3 |
TB8 |
Transmit Bit |
2 |
RB8 |
Receiver Bit |
1 |
TI |
Transmit Flag |
0 |
RI |
Receive Flag |
The bits SM0 and SM1 will determine the baud rate to be selected. Table below shows how the baud rate is selected.
SM0 |
SM1 |
BAUD RATE |
EXPLANATION |
0 |
0 |
8-bit shift register |
Oscillator Frequency/12 |
0 |
1 |
9 bit UART |
Set by Timer1 |
1 |
0 |
9 bit UART |
Oscillator Frequency/64 |
1 |
1 |
9 bit UART |
Set by Timer1 |
Baud Rate
Baud rate is defined as the rate at which the data is transferred in bits per second. Usually for serial communication the baud rate is set according to SM0 and SM1. If the value of SM0 and SM1 are “00” or “10”, then baud rate depends upon the oscillator frequency. In other cases, the baud rate depends upon the Timer 1 of the microcontroller.
The value that has to be placed in TH1 to generate the required baud rate depends upon the following equation.
TH1 = 256 - ((Crystal / 384) / Baud)
In the program, TH1 is set to the value, 0xFD such that the baud rate is 9600bps according to the above equation for a crystal frequency of 11.059MHz.
Step3
After step 1 and 2, the serial port can be used for transmission and reception of data. Hyper terminal is used for the reception and transmission of data through RS232.
In the program of serial transmission, the letter ‘B’ is transmitted through SBUF of the controller and is displayed on the hyper terminal of PC. While, in case of serial reception, data is entered on the hyper terminal and it is serially received through the SBUF of microcontroller. Inorder to view the output on the microcontroller, in case of serial reception, LCD interfacing has to be done.