Skip to main content

How to transmit multiple character(string) data serially (UART) interfacing with 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 send a string serially using at89c51. Before proceeding further read basic points of UART communiction.

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.

my_data[15]="Mandeep Singh";
for(i=0;my_data[i];i++)
{
    SBUF=my_data[i] ;  // loading SBUF (serial buffer) with a character one by one
    while(TI==0);           // after sending data when SBUF becomes empty, it will toggle TI flag to high
    TI=0;                         //  setting TI flag to low .
}

So in the above 3 lines data has been sent to slave 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:     int i;   
11:     my_data[15]="Mandeep Singh";  
12:     SCON=0x50;   
13:     TMOD=0X20;  
14:     TH1=253;  
15:     TR1=1;  
16:     for(i=0;my_data[i];i++)  
17:     {  
18:       SBUF='m' ;  
19:       while(TI==0);  
20:       TI=0;  
21:      }  
22:  }  




The above program will continuously send all data of string to other device and stops after sending it once.


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.