8051 Serial Port
8051 Serial Port
Introduction
Serial communication means to transfer data bit by bit serially at a time, whereas in
parallel communication, the number of bits that can be transferred at a time depends
upon the number of data lines available for communication.
8051 has built-in UART with RXD (serial data receive pin) and TXD (serial data
transmit pin) on PORT3.0 and PORT3.1 respectively.
Asynchronous Communication
Asynchronous serial communication is widely used for byte-oriented transmission.
In an asynchronous serial communication frame, the first START bit followed by the
data byte and at last STOP bit forms a 10-bit frame. Sometimes the last bit is also
used as a parity bit.
Interface standard
• 8051 serial communication has TTL voltage level which are 0 v for logic 0 and
5 v for logic 1.
• In computers and most of the old devices for serial communication, RS232
protocol with DB9 connector is used. RS232 serial communication has
different voltage levels than 8051 serial communication. i.e. +3 v to +25 v for
logic zero and -3 v to -25 v for logic 1.
• So to communicate with RS232 protocol, we need to use a voltage level
converter like MAX232 IC.
• Although there are 9 pins in the DB9 connector, we don’t need to use all the
pins. Only 2nd Tx(Transmit), 3rd Rx(Receive), and 5th GND pin need to be
connected.
• To meet the standard baud rates generally crystal with 11.0592 MHz is used.
• As we know, 8051 divides crystal frequency by 12 to get a machine cycle
frequency of 921.6 kHz.
• The internal UART block of 8051 divides this machine cycle frequency by 32,
which gives the frequency of 28800 Hz which is used by UART.
• To achieve a baud rate of 9600, again 28800 Hz frequency should be divided
by 3.
• This is achieved by using Timer1 in mode-2 (auto-reload mode) by putting
253 in TH1 (8-bit reg.)
• So 28800 Hz will get divided by 3 as the timer will overflow after every 3
cycles.
• we can achieve different baud rates by putting the division factor in the TH1
register.
9600 FD
4800 FA
2400 F4
1200 E8
This is the serial communication data register used to transmit or receive data
through it.
SCON: Serial Control Register
Serial control register SCON is used to set serial communication operation modes.
Also it is used to control transmit and receive operations.
Normally mode-1 (SM0 =0, SM1=1) is used with 8 data bits, 1 start bit, and 1 stop
bit.
1 = Receive enable
0 = Receive disable
This is the 9th bit which is to be transmitted in mode 2 & 3 (9-bit mode)
This is the 9th received bit in mode 2 & 3 (9-bit mode), whereas in mode 1 if SM2 =
0 then RB8 hold the stop bit that received
This bit indicates the transmission is complete and gets set after transmitting the
byte from the buffer. Normally TI (Transmit Interrupt Flag) is set by hardware at the
end of the 8th bit in mode 0 and at the beginning of stop bit in other modes.
Bit 0 – RI: Receive Interrupt Flag
This bit indicates reception is complete and gets set after receiving the complete
byte in the buffer. Normally RI (Receive Interrupt Flag) is set by hardware in receiving
mode at the end of the 8th bit in mode 0 and at the stop bit receive time in other
modes.
Programming steps
Example
Let's Program 8051 (here AT89C51) to send character data “test” serially at 9600
baud rate in mode 1
void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}
void main()
{
UART_Init(); /* UART initialize function */
String("test"); /* Transmit 'test' */
while(1);
}
8051 serial interrupt has a vector address (0023H) where it can jump to serve ISR
(Interrupt service routine) if the global and serial interrupt is enabled.
Let's see how the serial interrupt routine will be used in serial communication
programming.
Programming steps
Note: For transmission and reception interrupt, the same interrupt vector address is
assigned, so when the controller jumps to the ISR, we have to check whether it is Tx
interrupt or Rx interrupt by TI and RI bits status.
Example
Let's Program 8051 (here AT89C51) to receive character data serially at 9600 baud
rate in mode 1 and send received data on port 1 using a serial interrupt.
• In this example, after a byte is received, the RI flag is set which will generate a
serial interrupt.
• After the interrupt, 8051 will jump to the serial ISR routine.
• In the ISR routine, we will send data to port 1.
void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
ES = 1; /* Enable serial interrupt */
}
void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}
void main()
{
P1 = 0x00; /* Make P1 output */
Ext_int_Init(); /* Call Ext. interrupt initialize */
UART_Init();
while(1);
}