Serial Communication in 8051 Microcontroller

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
serial-communication-in-8051-microcontroller
Serial Communication using 8051

Microcontrollers need to communicate with external devices such as sensors, computers and so on to collect data for further processing. Data communication generally done by means of two methods : Parallel and Serial mode. In the parallel mode data bits are transferred in a fast manner using more number of lines. But when comes to a Microcontroller system, we cannot afford to dedicate many lines for data transfer. So UART or Serial communication in 8051 microcontroller will allow the controlller to send and receive data’s just byusing two pins

Serial Communication uses only two data lines to establish communication between Microcontroller and external devices. In this communication data bits are transferred one bit at a time, so the process will be slow. This article describes the Interfacing of  8051 with  PC to establish communication through a serial port RS232.

RS232 AND MAX232:

To establish communication between a controller and PC, we must use a serial I/O standard RS-232 which was widely used in PC and several devices. PC works on the RS-232 standards which operates at a logic level of -25V to +25V. But Microcontrollers use TTL logic which works on 0-5V which is not compatible with the RS-232 standards.

MAX232 is a specialized IC which offers intermediate link between the Microcontroller and PC. The transmitters of this IC will convert the TTL input level to the RS-232 Voltage standards. Meanwhile the receivers of this IC will convert RS-232 input to 5V TTL logic levels. Read the complete working of  MAX232 IC.

SCON REGISTER:

SCON-register-8051-microcontroller
SCON Register

SCON is a bit addressable register is used to set the mode in which the serial communication takes in the controller. The above figure shows the configuration of the SCON register. The functions of each bit are listed below.

Serial-communication-modes-8051-microcontroller
Serial Mode of 8051

SM0, SM1:  Serial Mode control Bits

SM2: Multiprocessor mode control bit, logic 1 enables Multi processor mode and 0 for normal mode.

REN: Enables Serial reception. If set it enables the reception, otherwise the reception is disabled.

TB8: It is the 9th bit of the data that is to be transmitted.

RB8: It is used in modes 2 and 3, it is the 9th bit received by the microcontroller.

TI: It is known as Transmit Interrupt flag which is set by the hardware to indicate the end of a transmission. It has to be cleared by the software.

RI: It is  known as Receive Interrupt flag which is set by the hardware to indicate the end of a reception. It has to be cleared by the software.

BAUD RATE:

It is defined as number of bits transmitted or received per second and usually expressed in Bits per second bps. For mode 0 and mode 2 the baud rate is determined by means of 1/12, 1/32 or 1/64 of the crystal frequency whereas for mode 1 and 3 it is determined by means of timer 1.

Baud-rate-table-timer1-8051
Baud Rate by Timer1

Learn how to configure Timers in 8051.

SBUF REGISTER:

It is a 8 bit register that holds the data needed to be transmitted or the data that is received. The serial port of the 8051 is full duplex so the microcontroller manages to transmit and receive data into the register simultaneously.

CODE:

#include<reg51.h>
void initialize()     // Initialize Timer 1 for serial communication
    {
     TMOD=0x20;    //Timer1, mode 2, baud rate 9600 bps
     TH1=0XFD;      //Baud rate 9600 bps
     SCON=0x50;   
     TR1=1;             //Start timer 1
    }
void receive()        //Function to receive serial data
   {
    unsigned char value;
    while(RI==0);       //wait till RI flag is set
    value=SBUF;     
    P1=value;
    RI=0;                    //Clear the RI flag
   }
void transmit()       // Funtion to transmit serial data
   {
    SBUF='o';            //Load  'o' in SBUF to transmit
    while(TI==0);      //Wait till TI flag is set or data transmission ends
    TI=0;                  //Clear TI flag
    SBUF='k';
    while(TI==0);
    TI=0;
   }
void main()
{
 while(1)
  {
   initalize();
   receive();
   transmit();
  }
}

Initialize – Initialize timer for baud rate and set mode for serial transmissionThe above program is coded in such a way to receive a character from PC and in return transmits “ok” to it. This code have three parts initialize,receive and transmit to perform the process of serial communication. This can be tested using Hyperterminal software after connecting your controller to PC through RS232.

  • Receive – Enable controller to receive data bit from PC.
  • Transmit – Enable transmission of data from 8051 to PC.  

Related Designs and Schematics

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
The following two tabs change content below.

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who is very passionate about his subject. Reading, Surfing and Blogging are the things he loves.

Ask your query

3 Comments on "Serial Communication in 8051 Microcontroller"

Notify of
avatar
Sort by:   newest | oldest | most voted
himanshu kathuria
Guest

How can I use 8051 in shIFT register serial communication mode

Bonham
Guest

thank you for the code

wpDiscuz