Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (3 votes)
7K views

Embedded C 8051 Example Programs

The document contains several C programs for 8051 microcontrollers that demonstrate different functions including: 1. Sending values from 00-FF to port P1 continuously. 2. Sending ASCII character hex values to port P1. 3. Toggling all bits of port P1 continuously. 4. Toggling a single bit of port P1 50,000 times. 5. Toggling port P1 bits continuously with a 250ms delay. It also includes programs for timer programming, serial port programming, interrupt programming, and LCD programming. The LCD programs demonstrate initializing and writing text to an LCD display.

Uploaded by

prakash_neo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
7K views

Embedded C 8051 Example Programs

The document contains several C programs for 8051 microcontrollers that demonstrate different functions including: 1. Sending values from 00-FF to port P1 continuously. 2. Sending ASCII character hex values to port P1. 3. Toggling all bits of port P1 continuously. 4. Toggling a single bit of port P1 50,000 times. 5. Toggling port P1 bits continuously with a 250ms delay. It also includes programs for timer programming, serial port programming, interrupt programming, and LCD programming. The LCD programs demonstrate initializing and writing text to an LCD display.

Uploaded by

prakash_neo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

8051 Example programs in Embedded C using Keil

1. Write an 8051 C program to send values 00 FF to port P1.


#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
2. Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3,
4, 5, A, B, C, and D to port P1.
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=012345ABCD;
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}
3. Write an 8051 C program to toggle all the bits of P1 continuously.
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
P1=0x55;
P1=0xAA;
}
}
4. Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}

5. Write an 8051 C program to toggle bits of P1 ports continuously with a 250


ms.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
P1=0x55;
MSDelay(250);
P1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
TIMER PROGRAMMING
#include <reg51.h>
void T0M1Delay(void);
sbit mybit=P1^5;
sbit SW=P1^7;
void main(void)
{
SW=0;
while(1)
{
if(SW==1)
{
mybit=~mybit;
T0M1Delay();
}
}
}
void T0M1Delay(void)
{
TMOD=0x01;
TL0=0xFD;
TH0=0x4B;
TR0=1;
while(TF0==0);
TR0=0;

TF0=0;
}
SERIAL PORT PROGRAMMING
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 a C program to send two different strings to the serial port.
Assuming that switch is connected to pin P2.0, monitor its status and
make a decision as follows:
SW=0: send first name
SW=1: send last name
#include <reg51.h>
#include <string.h>
sbit MYSW = P2^0;
void main(void)
{
unsigned char z;
unsigned char fname[]="PRAKASH";
unsigned char lname[]="JOTHI";
while(1)
{
TMOD=0x20;
TH1=0x50;
TR1=1;
if(MYSW == 0)
{
for(z=0;z < strlen(fname);z++)
{
SBUF=fname[z];
while(TI==0);
TI=0;
}
}

else
{
for(z=0;z<strlen(lname);z++)
{
SBUF=lname[z];
while(TI==0);
TI=0;
}
}
}
}
INTERRUPT PROGRAMMING
1. Write a C program that continuously gets a single bit of data from P1.7
and sends it to P1.0, while simultaneously creating a square wave of
200 us period on pin P2.5. Use timer 0 to create the square wave.
#include <reg51.h>
sbit SW = P1^7;
sbit IND = P1^0;
sbit WAVE = P2^5;
void timer0(void) interrupt 1
{
WAVE = ~WAVE;
}
void main()
{
SW=1;
TMOD = 0x02;
TH0 = 0xA4;
IE = 0x82;
TR0 =1;
while(1)
{
IND = SW;
}
}
2. Write a C program that continuously gets a single bit of data from P1.7
and sends it to P1.0 in the main while simultanueously (a) creating a
square wave of 200 us period on pin P2.5 and (b) sending letter A to
the serial port. Use Timer 0 to create the square wave. Assume that
XTAL = 11.0592 MHz. Use the 9600 baud rate.
#include <reg51.h>
sbit SW = P1^7;
sbit IND = P1^0;
sbit WAVE = P2^5;

void timer0(void) interrupt 1


{
WAVE = ~WAVE;
}
void serial0() interrupt 4
{
if(TI==1)
{
SBUF='A';
TI=0;
}
else
{
RI=0;
}
}
void main()
{
SW=1;
TH1=-3;
TMOD = 0x22;
TH0 = 0xA4;
SCON=0x50;
TR0 =1;
TR1=1;
IE = 0x92;
SBUF='A';
while(1)
{
IND = SW;
}
}
LCD PROGRAMMING
Pgm 1:
#include <reg51.h>
#include <string.h>
sbit
sbit
sbit
sbit

rs = P2^7;
en = P2^5;
rw = P2^6;
b = P0^7;

// declare P2.7 as rs pin


// declare p2.5 as enable pin
// declare p2.6 as read/write pin
// busy flag

void writecmd(unsigned char a); // function to send command to LCD


void writedat(unsigned char b); // function to send data to LCD
void busy(); // function to check LCD is busy or not

void writestr(unsigned char *s); // function to write string on LCD


void writecmd(unsigned char a)
{
busy(); // check for LCD is busy or not
rs = 0; // clear rs pin for command
rw = 0; // clear rw pin to write
P0 = a; // send command character
en = 1; // strob LCD
en = 0;
}
void writedat(unsigned char b)
{
busy(); // check for LCD is busy or not
rs = 1; // set rs pin for data
rw = 0; // clear rw pin to write
P0 = b; // send data character
en = 1; // strob LCD
en = 0;
}
void busy()
{
en = 0; // disable display
P0 = 0xFF; // configur P0 as input
rs = 0; // clear rs pin for command
rw = 1; // set rw pin to read
while(b==1)
{
en=0; // strob LCD till P0.7 is 1
en=1;
}
en=0;
}
void writestr(unsigned char *s)
{
unsigned char l,i;
l = strlen(s);
// get the length of string
for(i=1;i<l;i++)
{
writedat(*s);
// write every char one by one
s++;
}
}
main()
{
while(1)
{
P0=0x00;
P2=0x00;

// P0 and P0 as output ports

writecmd(0x3C);
// initialize LCD
writecmd(0x0E);
writecmd(0x01);
// clear memory and home cursor
writestr("Wel-Come to LCD"); // write message in first line
writecmd(0xC4);
// move cursor to second line 4th pos
writestr("Program");
}
}
Pgm 2:
#include <reg51.h>
#include <string.h>
sfr ldata = 0x90;
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
char j;
char z;
char k;
void lcdcmd(unsigned char a);
void lcddata();
void Delay();
void main()
{
unsigned char x[]="LCD Program";
lcdcmd(0x38);
lcdcmd(0x0E);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
for(z=0;z<strlen(x)+1;z++)
{
lcddata();
ldata=x[z];
}
while(1)
{
lcdcmd(0x18);
Delay();
}
}
void lcdcmd(unsigned char a)
{
ldata=a;

rs=0;
rw=0;
en=1;
Delay();
en=0;
return;
}
void lcddata()
{
rs=1;
rw=0;
en=1;
Delay();
en=0;
return;
}
void Delay()
{
long int i=10000;
while(i--);
}

You might also like