LAB 13 8051 Serial Programming in C
LAB 13 8051 Serial Programming in C
PROGRAMMING IN C
INTRODUCTION
#include <reg51.h>
void main(void)
{
TMOD=0x20; //timer 1, mode 2 (auto reload)
TH1 = -6; //4800 baud rate
SCON=0x40; //8-bit, 1 stop
TR1 = 1; //start timer 1
while(1)
{
SBUF = ‘A’; //letter "A" to be transferred
while(TI==0); //wait for the last bit
TI = 0; //clear TI for next char
} //keep sending A
}
EXAMPLES
A program to receive bytes of data serially, and put them in P1. Set the
baud rate at 4800, 8-bit data and 1 stop bit.
#include <reg51.h>
void main(void)
{
char x;
TMOD=0x20; //timer 1, mode 2 (auto reload)
TH1 = -6; //4800 baud rate
SCON=0x50; //8-bit, 1 stop, REN enabled
TR1 = 1; //start timer 1
while(1)
{
while(RI==0); //wait for character to receive
x = SBUF; //save incoming byte in x
RI = 0; //clear RI for next char
P1 = x; //send data to port 1
} //keep sending A
}
EXERCISES
Write an 8051 C program to transfer serially the
letter “Z” continuously at a 1200 baud rate.
EXERCISES
Write an 8051 C program to transfer serially the
message “We love our country” continuously at a
57,600 baud rate.
EXERCISES
Write a C program to simultaneously transmit and
receive data at 9.6kbps baud rate.