Skip to main content

How to receive/collect data serially (UART) from other device to my controller 8051 (AT89C51/P89V51)

To begin with UART communication you must have good command of timer of 8051.

So in this post we are going to receive a character serially using at89c51. Before proceeding further read basic points of UART communication.

Here we will use TMOD register for timer mode and SCON register.

From SCON bit addressable register we need RI and TI bits to check weather some data received in serial buffer(SBUF) or data present in serial buffer(SBUF) is transmitted resp.


Lets create a simple delay function first

void delay(unsigned int time)
{
      int i,j;
      for(i=0;i<=time;i++)
            for(j=0;j<=1275;j++);
}

The above function create approx 1 second of delay.

Now lets begin the main part of UART and let set TMOD and SCON values.

SCON=0x50;     // selecting mode 1 and enabling reception.  Refer SCON
TMOD=0X20;   // selecting timer 1 mode 2(auto-reload) . Refer TMOD
TH1=253;
TR1=1;              // starting timer


After setting timer TMOD and SCON, now begins with the part of serial transmission.

    char my_data;          // declaring a char variable to collect data.
    while(RI==0);           // after receiving data in SBUF , it will toggle RI flag to high
    my_data=SBUF;      // collecting single data received in  SBUF (serial buffer)
    RI=0;                         //  setting RI flag to low .

So in the above 3 lines data has been received from other device via UART protocoal.

Now Lets combine the code.

1:  #include<reg51.h>  
2:  void delay(unsigned int time)  
3:  {  
4:     int i,j;  
5:     for(i=0;i<=time;i++)  
6:        for(j=0;j<=1275;j++);  
7:  }  
8:  void main()  
9:  {  
10:     char my_data;  
11:     SCON=0x50;   
12:     TMOD=0X20;  
13:     TH1=253;  
14:     TR1=1;  
15:    while(1)  
16:    {  
17:        while(RI==0);  
18:        my_data=SBUF;  
19:        RI=0;  
20:    }  
21:  }  


After collecting data now you can perform operations with that data as per your requirement, the above program is limited to receiving data only, not any further operations with that data.
You can print that data on LCD as well.

You can collect the data in string as well if you are working on string type data
just add few lines
   char my_data[15];          // declaring a char string to collect data.
   int i=0;
   while(1)
   {
        while(RI==0);   
        my_data[i]=SBUF;
        i++;
        RI=0
     }


The above program will receive data from other device and will always wait to receive data..


Video:








Hope you all like this post and I wish this proves useful to you.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.

Thanks :)
Please write comments if you find anything needs to be more clear, incorrect, or you want to share more information about the topic discussed above

Comments

Translate to your language

Popular posts from this blog

Different Stages of I2C communication

Different Stages Of I2C

How to import Pygame in pycharm

hi, how are you today

Working demonstration of UART communication using 2 8051 uController on Proteus with Embedded C code.

Here we are going to use 2 at89c51 uControllers just to understand UART communication in 8051.