To begin with UART communication you must have good command of timer of 8051.
So in this post we are going to send a single character 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.
SBUF='m' ; // loading SBUF (serial buffer) with a character ( example 'm')
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 other device via UART protocoal.
Now Lets combine the code.
The above program will continuously send 'm' to other device as we have mentioned it in while(1). If you want for a specific number of time you can use for loop in place of while statement.
Video:
So in this post we are going to send a single character 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.
SBUF='m' ; // loading SBUF (serial buffer) with a character ( example 'm')
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 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: SCON=0x50;
11: TMOD=0X20;
12: TH1=253;
13: TR1=1;
14: while(1)
15: {
16: SBUF='m' ;
17: while(TI==0);
18: TI=0;
19: }
20: }
The above program will continuously send 'm' to other device as we have mentioned it in while(1). If you want for a specific number of time you can use for loop in place of while statement.
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.
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
Post a Comment