Title: C: Programming For 8051 Family

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Tutorial- 1 TITLE: C Programming for 8051 family

BATCH : ROLL NO : DATE:

Program 1: Write a 8051 C Program to send values of -4 to 4 at port


p1.
#include<reg51.h> void main() { char num[]={+1,-1,+2,-2,+3,-3,+4,-4}; unsigned char z; for(z=0;z<8;z++) P1=num[z]; }

Output:

Program 2: Write a 8051 C Program to toggle bit p2.3 thousand


times.
#include<reg51.h> sbit kk=P2^3; void main() { //char num[]={1,-1,+2,-2,+3,-3,+4,-4}; unsigned char z; for(z=0;z<1000;z++) { kk=~kk; } }

Output:

Program 3: Write a 8051 C Program to get a byte of data from P0,if it


is Less than 100,send it to p1,else send to P2.
#include<reg51.h> void main() { int I; I=P0; if(I<100) P1=P0; else P2=P0; }

Output:

Program 4: Write a 8051 C Program to get a byte of data from P1,


wait half second & then send it to P2.
#include<reg51.h> void delay(unsigned int); void main() { unsigned char Data; P1=0Xff; while(1) { Data=P1; delay(500); P2=Data; } } void delay(unsigned int itime) { unsigned int i, j; for(i=0; i<itime; i++) { for(j=0; j<360; j++) { } } } // BREAK POINT

Output:

Program 5: Write a 8051 C Program to move GOOD MORNING


RAJKOT to LCD display connected to P1, when P3.5 goes Low.
#include<reg51.h> sbit kk=P3^5; void main() { unsigned char message[]="GOOD MORNING RAJKOT"; unsigned char z; for(;;) { if (kk==0) { for(z=0;z<19;z++) { P1=message[z]; } } } }

Output:
G O O

SPACE

SPACE

Program 6 : Write a 8051 C Program to compare two no.


at port2 and port 3 then store the largest no. to port 2

and smallest no. in port 3 and if both no. are equal then store at port 1.

#include<reg51.h> void main() { unsigned char a,b; a=P2; b=P3; if(a>b) { P2=a; P3=b; } else { if(a<b) { P2=b; P3=a; } P1=P2; } }

OUTPUT :

If p2<p3

If p2=p3

CONCLUSION :

You might also like