How to interface UART(RS232) using 8051/AT89c51.
UART (RS232) interfacing in 8051/AT 89c51
This program is for interfacing UART (RS232) in 8051 /AT89c51. In this program I have written a UART echo back code. It means that whatever 8-bit data micro-controller received at its RXD pin it will send it back to its TXD pin (i.e. what ever you send you will get that back). The main program is shown in image below:-
In the main function I have called the “uart_ini()” function to configure UART (RS232).the complete “uart_ini()” function is declared in below image:-
In “uart_ini()” function firstly I have loaded 0x20 value to TMOD register to configure the timer 1 in 8-bit auto reload mode because Timer 1 is assigned to generate baud rate for uart communication. Then after I have loaded oxfd value in TH1 for selecting 9600 baud rate. For different baud rate you have to loaded different values in TH1 register explain in table below:-
TH1 value for generating different baud rate:-
Fosc (Mhz)
|
11.0592
|
Baudrate
|
TH1
|
1200
|
E8h
|
2400
|
F4h
|
4800
|
FAh
|
9600
|
FDh
|
After that I have loaded the 0x50 value at SCON register to selecting UART (RS232) in 8-bit mode and also enable reception bit4. For your reference SCON register explained below:-
Bit 7
|
Bit 6
|
Bit 5
|
Bit 4
|
Bit 3
|
Bit 2
|
Bit 1
|
Bit 0
|
FE/SM0
|
SM1
|
SM2
|
REN
|
TB8
|
RB8
|
TI
|
RI
|
Then 0x90 value has been loaded to IE register to enable UART RX interrupt and at last I have start timer to generate UART baud rate.
After this initialization when ever data receives at RXD pin of controller then program will go into “void receiver (void) interrupt 4” and here i received data through SBUF (serial buffer) register and transmit it back through “transmit ()” function. In “transmit()” function we have loaded value to SBUF for transmitting and wait while data is transmitted completed.
For understanding complete code you will have to considered “uart.h” also which is included in main is explain in image shown below:-
Download Link
For Downloading complete code click here.
No comments:
Post a Comment