Thursday, 23 October 2014

Serial Communication in 8051 Microcontroller

SERIAL COMMUNICATION IN 8051

Hello readers, today I am writing about serial communication of data in one of the most widely used microcontroller (8051). Lot of applications require microcontrollers to either accept the data in serial form or o/p the data in serial form. 

Microcontrollers can communicate data in either parallel form or serial form. In parallel communication, data is transferred over more than one wire for example if 8 wires of one microcontroller are connected to any other peripheral device or another microcontroller then at a particular time 8 data bits are transferred. On the other hand in serial communication, data is transferred in bit by bit manner over a single wire. For example if a microcontroller 1 is transferring data to another microcontroller 2 in serial form then TXD pin of microcontroller 1 will be connected to RXD pin of microcontroller 2 and data is transferred from microcontroller 1 to microcontroller 2 in bit by bit manner over a single wire. If microcontroller 2 wants to send some data to microcontroller 1 then  for that TXD pin of microcontroller 2 must be connected to RXD pin of microcontroller 1 and again data bits will be transferred over a single wire. So it is clear that for full duplex communication of data two wires are involved and for simplex communication only one wire will be involved.

Parallel communication requires more number of wires than serial communication but it has higher speed of data transfer as more than one bit is transferred at a given time. Serial communication is preferred when the distance b/w transmitter and receiver is large and it is required to save the cabling cost and reduce h/w complexity but definitely this comes at the cost of reduced speed of data transfer.


Fig. 1 below shows parallel data transmission-


                                                            Fig.1 Parallel data transmission

Fig.2 below shows serial data transmission-

                                                           Fig.2 serial data transmission

In serial communication parallel to serial converter and serial to parallel converter module is required. In 8051 in built UART (Universal Asynchronous Receiver Transmitter) module performs the job of serial communication of data.

Data communication process can be synchronous or asynchronous. The following points clearly discriminates between these two modes of data transfer-

  • Synchronous data transfer: sender and receiver use the same clock signal
    • supports high data transfer rate
    • needs clock signal between the sender and the receiver
    • requires master/slave configuration
  • Asynchronous data transfer: sender provides a synchronization signal to the receiver before starting the transfer of each message
    • does not need clock signal between the sender and the receiver
    • slower data transfer rate
   
In 8051 microcontroller serial communication of data is performed with the help of following special purpose registers-

1.      SBUF (Serial buffer register)
2.      SCON (serial control register)
3.      TMOD (Timer mode register)
4.      TCON (Timer control register)
5.      TH1 (Timer1 register higher byte)

Before going further it is extremely important to first understand these registers and their importance with reference to serial communication.

1. SBUF (Serial buffer register)

It is an 8-bit register and is used for serial communication of data in 8051 microcontroller. Whatever data is required to be transmitted via TXD line must be placed in the SBUF register. Similarly the received data via RXD line is saved in SBUF register. When data is written to SBUF register then it is framed in b/w start and stop bit before it is transmitted via TXD line and similarly during reception of data start and stop bits are removed and actual data bits are extracted from the received frame and then it is placed in the SBUF register.

2.   SCON (Serial control register)







SM0, SM1  Serial port mode specifier

REN           (Receive enable) set/cleared by software to enable/disable reception.

TI               Transmit interrupt flag.

RI               Receive interrupt flag.

SM2 = TB8 = RB8 =0 (not widely used)

SM1 and SM0 determine the framing of data.
Only mode 1 is compatible with COM port of PC.






3.    TMOD (Timer mode register)

Both timer 0 and timer 1 use the same register, called TMOD, to set the various timer modes. TMOD is an 8-bit register in which the lower 4-bits are set aside for timer 0 and the upper 4-bits are set aside for timer 1. In each case, the lower 2 bits are used to set the timer mode and the upper two bits are used to specify the operation. 



C/T-

If this bit is set then counter operation is performed (for counting applications) and if this bit is clear then timer operation is performed (for delay generation ). The clock source for timer is the crystal oscillator that is connected to the 8051 micro-controller. Crystal oscillator also determines the speed at which the timer ticks.



       4. TCON (Timer Control Register)






5.    TH1 (Timer 1 register high byte)


Timer 1 high byte register is loaded with appropriate value for baud rate generation. Timer 1 in mode 2 is used for baud rate generation.   


Concept of Baud Rate Generation in 8051 Micro-controller




8051 divides the crystal oscillator frequency by 12 to get the machine cycle frequency. UART divides the machine cycle frequency by 32 and sends it to Timer 1 to set the baud rate.

Timer 1, mode 2 (8-bit, auto-reload)

Define TH1 to set the baud rate.


XTAL = 11.0592 MHz

The system frequency = 11.0592 MHz / 12 = 921.6 kHz

Timer 1 has 921.6 kHz/ 32 = 28,800 Hz as source

Baud rate = 28,800/3= 9,600 bps

So TH1 must be loaded with value of FDH (-3) for the generation of baud rate of 9600 bps.


For the crystal oscillator frequency of 11.0592MHz the below given table can be used as a reference for getting the value that is required to be loaded into TH1 register for the desired baud rate generation.



Baud Rate
TH1 (Decimal)
TH1 (Hex)
9600
-3
FD
4800
-6
FA
2400
-12
F4
1200
-24
E8


What is the significance/Importance of TI and RI flags?

When 8051 is transmitting serial data then TI flag (Transmit Interrupt Flag) plays a very important role. This flag must be monitored to check whether data transfer operation is executed successfully or not. This bit is set by the hardware at the beginning of the stop bit in mode 1. This flag bit must be cleared by software. In embedded C the instructions that can be used to monitor the status of TI flag is-

SBUF = ’U’;      // load SBUF with the data that is required to be transmitted
While (TI ==0);       //stay on this line till TI=0 (wait until transmitted)
TI=0;                // clear TI flag

Similarly during reception RI flag (Receive Interrupt Flag) plays a very important role. This flag must be monitored to check whether any data is received or not. This flag bit is set in half way through the stop bit in mode 1 and must be cleared by the software. The instructions that can be used to monitor the status of RI flag in embedded C are-

While (RI==0); // stay on this line till RI=0 (wait to receive)
temp = SBUF; //read SBUF and store it in some temp variable
P2 = temp;  // put the received data on port 2
RI=0;   // clear the RI flag   

In simple terms TI = 1 indicates that data is transferred successfully and RI = 1 indicates that data byte is received successfully.


Programming Steps for Serial data transmission-


Step 1- Load TMOD register with 0X20 (timer 1 in mode2)

        In embedded C-
          
            TMOD = 0X20;
          
            In Assembly Language-
  
            MOV TMOD, #20H

     
Step 2- Load TH1 with a value for the required baud rate generation (refer to baud rate table)
        
            TH1=0XFD (for 9600bps baud rate).

            In embedded C-

            TH1 = 0XFD;

            In Assemble Language-

            MOV TH1, #0FDH

Step 3- Load SCON with 0X50 (serial mode 1 with REN = 1), receive operation is enabled.

            In Embedded C-

            SCON=0X50;

            In Assembly Language-

            MOV SCON, #50H

    
Step 4- Set TR1 bit to start timer 1.

             In Embedded C-
    
            TR1=1;
           
            In Assembly Language-

            SETB TR1

Step 5- Clear the transmit interrupt flag before transmitting any data.
     
 This is just to make sure that UART is ready to transfer a new byte of data.
         
            In Embedded C-
    
            TI=0;

            In Assembly Language-

            CLR TI


Step 6- Load the data to be transmitted in SBUF register

        In Embedded C-

        SBUF = ‘U’; // example ‘U’ us loaded in SBUF

        In Assembly Language-

        MOV SBUF, #’U’

Step 7- Monitor the TI flag to check whether the data has been transmitted successfully or not.

        In embedded C-

        while (TI==0);

        In Assembly Language-

        Here: JNB TI, Here

Step 8-  To transfer the next data byte, go to step 5, and start again.
           
It does not mean that one should repeat all the steps for transmitting 1 byte of data.
In embedded C you can create a function for doing steps 5 to 7 and pass data to be transmitted 
dynamically to the function. In assembly program one can make a subroutine and it can be called 
any time whenever data is required to be transmitted.

    Programming Steps for Serial data reception-


Step1- Load TMOD register with 0X20 (timer 1 in mode2)


     In embedded C-


     TMOD = 0X20;


     In Assembly Language-


     MOV TMOD, #20H


            Step 2-Load TH1 with a value for the required baud rate generation (refer to baud rate table)

     
     TH1=0XFD (for 9600bps baud rate).


     In embedded C-


     TH1 = 0XFD;


     In Assemble Language-


     MOV TH1, #0FDH


            Step 3- Load SCON with 0X50 (serial mode 1 with REN = 1), receive operation is enabled.

    
     In Embedded C-


     SCON=0X50;


     In Assembly Language-


     MOV SCON, #50H


           Step 4- Set TR1 bit to start timer 1.


     In Embedded C-


     TR1=1;


     In Assembly Language-


     SETB TR1


           Step 5- Clear the receive interrupt flag before receiving any data. This is just to make sure that    
                       UART is ready to receive a new byte of data.


     In Embedded C-


     RI=0;


     In Assembly Language-


     CLR RI


          Step 6- Monitor the RI flag to check whether the data has been received successfully or not.


    In embedded C-


    while (RI==0);


    In Assembly Language-


    Here: JNB RI, Here


          Step 7- When RI becomes 1 it means that a data byte is received and so the received data byte must
                      be stored in some temporary variable for further processing.


    temp =  SBUF;

    Storing the SBUF value in a temporary variable is one way to save the SBUF value you 
    can use your own way to make sure that the data byte received is moved to the safe place.


       
     Step 8- To receive the next data byte, go to step 5.

      Again note that you can make your program modular with the use of function in embedded for performing the repetitive job of step 5 to 7. In Assembly language you can make subroutine for performing steps 5 to 7.
                   
      


I hope that these concepts will be useful for the readers to start the    
programming of real time applications which involve
serial data communication using 8051 micro-controller. In the
upcoming articles I will share such applications along with
proteus simulations for better understanding.

 THANKS FOR READING......................
                           
                             UMESH DUTTA 



No comments:

Post a Comment