0% found this document useful (0 votes)
33 views

MIC Unit 04 Program

notes

Uploaded by

kheldarpranav03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

MIC Unit 04 Program

notes

Uploaded by

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

8051 Programming in C

Unit 04 :8051 Programming in C


Program 1: Write an 8051 C program to get a byte of data from Port P1. If it is less than
100, send it to P0; otherwise, send it to P2.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
P1 = 0xFF; //make P0 input port
while (1)
{
mybyte=P1; //get a byte from P0
if (mybyte<100)
P0 = mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}
Program 2: Write an 8051 C program to toggle all the bits of P0 and P2 continuously with a
250 ms delay. Use the inverting operator
Solution :-
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0;
P2=~P2;
MSDelay(250);

November 8, 2023 1
8051 Programming in C

}
}
Program 3: Write an 8051 C program to toggle only bit P2.4 continuously without
disturbing the rest of the bits of P2.
Solution :-
#include <reg51.h>
sbit mybit=P2^4;
void main(void)
{
while (1)
{
mybit=1; //turn on P2.4
mybit=0; //turn off P2.4
}
}
Program 4: Write an 8051c program to convert packed BCD 0x29 to ASCII and display the
bytes on Port 1 and Port 2.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char x,y,z;
unsigned char mybyte=0x29;
x=mybyte&0x0F;
P1=x|0x30;
y=mybyte&0xF0;
y=y>>4;
P2=y|0x30;
}

Program 5: Write an 8051C program to convert ASCII digits “4” and “7” to packed BCD
and display it on Port 1.

November 8, 2023 2
8051 Programming in C

Solution :-
#include <reg51.h>
void main(void)
{
unsigned char bcdbyte;
unsigned char w = ‘4’;
unsigned char z = ‘7’;
w = w&0x0F;
w = w<<4;
z = z&0x0F;
bcdbyte = w|z;
P1 = bcdbyte;
}
Program 6: Write an 8051 C program to convert 11111101(FD) Hex data to decimal and
display the digits on P0, P1, and P2.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char x, binbyte, d1, d2, d3;
binbyte=0xFD;
x=binbyte/10;
d1=binbyte%10;
d2=x%10;
d3=x/10;
P0=d1;
P1=d2;
P2=d3;
}
Program 7: .Write a C Program to send out a the value 44H serially one bit at a time through
any port pin. The LSB should go out first
Solution :-

November 8, 2023 3
8051 Programming in C

#include <reg51.h>
sbit P1b0=P1^0;
sbit regALSB=ACC^0;
void main(void)
{
unsigned char conbyte=0x44;
unsigned char x;
ACC=conbyte;
for (x=0;x<8;x++)
{
P1b0=regALSB;
ACC=ACC>>1;
}
}

Program 8: Write a C Program to send out a data serially one bit at a time through any port
pin. The MSB should go out first.
Solution:-
#include <reg51.h>
sbit P1b0=P1^0;
sbit regAMSB=ACC^7;
void main(void)
{
unsigned char conbyte=0x44;
unsigned char x;
ACC=conbyte;
for (x=0;x<8;x++)
{
P1b0=regAMSB;
ACC=ACC<<1;
}

November 8, 2023 4
8051 Programming in C

}
Program 9: Write a C Program to bring in a byte of data serially one bit at a time via P1.0.
The LSB should come in first.
Solution:
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCMSB=ACC^7;
bit membit;
void main(void)
{
unsigned char x;
for (x=0;x<8;x++)
{
membit=P1b0;
ACC=ACC>>1;
ACCMSB=membit;
}
P2=ACC;
}

November 8, 2023 5

You might also like