8051 in C
8051 in C
8051 in C
7. Sfr
To alot SFR byte a meaningful name.
e.g sfr p1 = 0x80; // 0x80 is address of
port 1 register in RAM
Write an 8051 C program to send values 00 FF to port P1.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
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.
SOLUTION
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=012345ABCD ;
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}
Write an 8051 C program to toggle all the bits of P1 continuously.
Solution:
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
p1=0x55;
p1=0xAA;
}
}
Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
{
P1=00; //clear P1
LED=0; //clear P2
for (;;) //repeat forever
{
P1++; //increment P1
LED++; //increment P2
}
}
Write an 8051 C program to get a byte of data form P0. If it is less than 100, send it to P1;
otherwise, send it to P2.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
P0=0xff; //make P0 input port
while (1)
{
mybyte=P0; //get a byte from P0
if (mybyte<100)
P1=mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}
Write an 8051 C program to toggle only bit P2.4 continuously without
disturbing the rest of the bits of P2.
Solution:
Ports P0 P3 are bitaddressable
//Toggling an individual bit and we use sbit data type to access
#include <reg51.h> a single bit of P0 - P3
sbit mybit=P2^4;
void main(void) Use the Px^y format, where x is the
port 0, 1, 2, or 3 and y is the bit 0 7
{ of that port
while (1)
{
mybit=1; //turn on P2.4
mybit=0; //turn off P2.4
}
}
Write an 8051 C program to monitor bit P1.5. If it is high, send 55H
to P0; otherwise, send AAH to P2.
Solution:
#include <reg51.h>
sbit mybit=P1^5;
void main(void)
{
mybit=1; //make mybit an input
while (1)
{
if (mybit==1)
P0=0x55;
else
P2=0xAA;
}
}
Write an 8051 C program to toggle only bit P2.4 continuously without
disturbing the rest of the bits of P2.
Solution:
//Toggling an individual bit
#include <reg51.h>
sbit mybit=P2^4;
void main(void)
{
while (1)
{
mybit=1; //turn on P2.4
mybit=0; //turn off P2.4
}
}
Write an 8051 C program to send values of 4 to +4 to port P1.
Solution:
//Singed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.
Solution:
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
Write an 8051 C program to get the status of bit P1.0, save it, and send it to P2.7
continuously.
Solution:
#include <reg51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit; //use bit to declare
//We use bit data type to access data in a bit-addressable section of the data RAM
space 20 2FH //bit- addressable memory
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=membit; //send it to P2.7
}
}
Operators
Logical operators
AND (&&), OR (||), and NOT (!)
Bit-wise operators
AND (&), OR (|), EX-OR (^), Inverter (~), Shift Right (>>),
and Shift Left (<<)
Run the following program on your simulator and examine the results.
Solution:
#include <reg51.h>
void main(void)
{
P0=0x35 & 0x0F; //ANDing
P1=0x04 | 0x68; //ORing
P2=0x54 ^ 0x78; //XORing
P0=~0x55; //inversing
P1=0x9A >> 3; //shifting right 3
P2=0x77 >> 4; //shifting right 4
P0=0x6 << 4; //shifting left 4
}
Write an 8051 C program to get bit P1.0 and send it to P2.7 after inverting it.
Solution:
#include <reg51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit;
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=~membit; //invert it and send it to P2.7
}
}
A door sensor is connected to the P1.1 pin, and a buzzer is connected to P1.7. Write an 8051 C program to monitor the door
sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few hundred Hz.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor=P1^1;
sbit Buzzer=P1^7;
void main(void)
{
Dsensor=1; //make P1.1 an input
while (1)
{
while (Dsensor==1)//while it opens means switch is pressed
{
Buzzer=0;
MSDelay(200);
Buzzer=1;
MSDelay(200);
}
}
}
Write an 8051 C program to read the P1.0 and P1.1 bits
switch (z)
and issue an ASCII character to P0 according to the following table. {
P1.1 P1.0 case(0):
00 send 0 to P0 {
P0=0;
01 send 1 to P0
break;
10 send 2 to P0 }
11 send 3 to P0 case(1):
{
P0=1;
Sol break;
#include <reg51.h> }
case(2):
void main(void) {
{ P0=2;
break;
unsigned char z; }
case(3):
z=P1;
{
z=z&0x3; P0=3;
break;
}
}
}
Assignment Try to understand these codes!
Write an 8051 C program to convert ASCII digits of 4 and 7 to
packed BCD and display them on P1.