0% found this document useful (1 vote)
704 views3 pages

Serial Communication C Program For 8051

This document contains 3 C programs for the 8051 microcontroller to perform serial communication tasks. The first program continuously transmits the letter 'A' at 4800 baud. The second program continuously transmits the message "YES" at 9600 baud by calling a function to transmit each character. The third program receives serial bytes and stores them in port P1. It sets the baud rate to 4800 and transmits either the message "Normal Speed" or "High Speed" depending on the state of an input switch.

Uploaded by

Ayushi Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
704 views3 pages

Serial Communication C Program For 8051

This document contains 3 C programs for the 8051 microcontroller to perform serial communication tasks. The first program continuously transmits the letter 'A' at 4800 baud. The second program continuously transmits the message "YES" at 9600 baud by calling a function to transmit each character. The third program receives serial bytes and stores them in port P1. It sets the baud rate to 4800 and transmits either the message "Normal Speed" or "High Speed" depending on the state of an input switch.

Uploaded by

Ayushi Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Micro Controller program

Serial communication programs


1) Write a C program for 8051 to transfer the letter “A” serially at 4800 baud continuously. Use
8-bit data and 1 stop bit.

#include <reg51.h>
void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1) {
SBUF='A'; //place value in buffer
while (TI==0);
TI=0;
}
}

2) Write an 8051 C program to transfer the message “YES” serially at 9600 baud, 8-bit data, 1
stop bit. Do this continuously.

#include <reg51.h>
void SerTx(unsigned char x);
void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFD; //9600 baud rate
SCON=0x50;
TR1=1; //start timer
while (1) {
SerTx('Y');
SerTx('E');
SerTx('S');
}}

void SerTx(unsigned char x)


{
SBUF=x; //place value in buffer
while (TI==0); //wait until transmitted
TI=0;
}

3) Program the 8051 in C 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>
sbit MYSW=P2^0; //input switch
void main(void){
unsigned char z;
unsigned char Mess1[ ]="Normal Speed";
unsigned char Mess2[ ]="High Speed";
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFF; //28800 for normal
SCON=0x50;
TR1=1; //start timer
if(MYSW==0) {
for (z=0;z<12;z++) {
SBUF=Mess1[z]; //place value in buffer
while(TI==0); //wait for transmit
TI=0;
}
}
else {
PCON=PCON|0x80; //for high speed of 56K
for (z=0;z<10;z++) {
SBUF=Mess2[z]; //place value in buffer
while(TI==0); //wait for transmit
TI=0;
}
}
}

You might also like