MIC Unit 04 Program
MIC Unit 04 Program
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