top of page

8051 UART

Updated: Mar 9

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.


Two methods of serial communication are


  • Synchronous Communication: Transfer of bulk data in the framed structure at a time

  • Asynchronous Communication: Transfer of a byte data in the framed structure at a time

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.

Frame structure in Asynchronous communication:


  • START bit: It is a bit with which serial communication starts and it is always low.

  • Data bits packet: Data bits can be 5 to 9 bits packet. Normally we use 8 data bit packet, which is always sent after the START bit.

  • STOP bit: This is one or two bits. It is sent after the data bits packet to indicate the end of the frame. The stop bit is always logic high.

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.



Data transmission rate


The data transmission rate is measured in bits per second (bps). In the binary system, it is also called a baud rate (number of signal changes per second). Standard baud rates supported are 1200, 2400, 4800, 19200, 38400, 57600, and 115200. Normally most of the time 9600 bps is used when speed is not a big issue.


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.

        

8051 Serial Interface Diagram




8051 UART Programming


Baud Rate calculation:

  • 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.



Division factor to achieve different baud rates

Baud Rate

TH1 (Hex)

9600

FD

4800

FA

2400

F4

1200

E8

 8051 Serial communication Registers


SBUF: Serial Buffer Register

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.



Bit 7:6 - SM0:SM1: Serial Mode Specifier


Mode

SM0

SM1

Mode

0

0

0

1/12 of Osc frequency shift register mode fixed baud rate

1

0

1

8-bit UART with timer 1 determined baud rate

2

1

0

9-bit UART with 1/32 of Osc fixed baud rate

3

1

1

9-bit UART with timer 1 determined baud rate

Normally mode-1 (SM0 =0, SM1=1) is used with 8 data bits, 1 start bit, and 1 stop bit.


Bit 5 - SM2: for Multiprocessor Communication


        This bit enables a multiprocessor communication feature in mode 2 & 3.


Bit 4 - REN: Receive Enable


        1 = Receive enable

        0 = Receive disable


Bit 3 - TB8: 9th Transmit Bit


        This is the 9th bit which is to be transmitted in mode 2 & 3 (9-bit mode)


Bit 2 - RB8: 9th Receive Bit


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


Bit 1 - TI: Transmit Interrupt Flag


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.

 

8051 Microcontroller Programming steps


  1. Configure Timer 1 in auto-reload mode.

  2. Load TH1 with value as per required baud rate e.g. for 9600 baud rate load 0xFD. (-3 in decimal)

  3. Load SCON with serial mode and control bits. e.g. for mode 1 and enable reception, load 0x50.

  4. Start timer1 by setting TR1 bit to 1.

  5. Load transmitting data in the SBUF register.

  6. Wait until loaded data is completely transmitted by polling the TI flag.

  7. When the TI flag is set, clear it, and repeat from step 5 to transmit more data.

 

Example


Let's Program 8051 (here AT89C51) to send character data “test” serially at 9600 baud rate in mode 1


8051 Serial Program for serial data transmit


/*
 * 8051_Serial_UART
 * http://www.electronicwings.com
 */

#include <reg51.h>		/* Include x51 header file */

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 Transmit_data(char tx_data)
{
	SBUF = tx_data;		/* Load char in SBUF register */
	while (TI==0);		/* Wait until stop bit transmit */
	TI = 0;			/* Clear TI flag */
}

void String(char *str)
{
	int i;
	for(i=0;str[i]!=0;i++)	/* Send each char of string till the NULL */
	{
		Transmit_data(str[i]);	/* Call transmit data function */
	}
}	

void main()
{
	UART_Init();		/* UART initialize function */
	String("test");		/* Transmit 'test' */
	while(1);
}

8051 Serial Interrupt


8051 UART has a serial interrupt. Whenever data is transmitted or received, serial interrupt flags TI and RI are activated respectively.


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


  1. Set timer 1 in auto-reload mode.

  2. Load TH1 with value as per required baud rate e.g. for 9600 baud rate load 0xFD.

  3. Load SCON with serial mode and control bits. e.g. for mode 1 and enable reception load 0x50.

  4. Start timer1 by setting TR1 bit to 1.

  5. Enable Global and serial interrupt bit, i.e. EA = 1 and ES = 1.

  6. Now whenever data is received or transmitted, the interrupt flag will set and the controller will jump to serial ISR.

  7. Note that, TI/RI flag must be cleared by software in ISR.

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.

 

8051 Serial Communication Interrupt Code


/*
 * 8051_Serial_Interrupt
 * http://www.electronicwings.com
 */

#include <reg51.h>		/* Include x51 header file */

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 Serial_ISR() interrupt 4    
{
	P1 = SBUF;		/* Give received data on port 1 */
	RI = 0;			/* Clear RI flag */
} 

void main()
{
	P1 = 0x00;		/* Make P1 output */
	Ext_int_Init();  	/* Call Ext. interrupt initialize */
	UART_Init();
	while(1);
}

 

Components Used






7 views0 comments

Comments


bottom of page