8051 Programming in C
8051 Programming in C
EX-OR Inverter 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 toggle all the bits of P0 and P2
continuously with a 250 ms delay. Using the inverting and Ex-OR
operators, respectively.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0;
P2=P2^0xFF;
MSDelay(250);
} }
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 } }
Write an 8051 C program to read the P1.0 and P1.1 bits and issue an
ASCII character to P0 according to the following table.
P1.1 P1.0
0 0 send 0 to P0
0 1 send 1 to P0
1 0 send 2 to P0
1 1 send 3 to P0
Solution:
#include <reg51.h>
void main(void)
{
unsignbed char z;
z=P1;
z=z&0x3;
switch (z)
{case(0):
{P0=0; break; }
case(1):
{ P0=1; break; }
case(2):
{ P0=2; break; }case(3):
{ P0=3; break; } } }
Write an 8051 C program to convert packed BCD 0x29 to ASCII and
display the bytes on P1 and P2.
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;
}
Write an 8051 C program to convert ASCII digits of 4 and 7 to
packed BCD and display them on P1.
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;
}
Write an 8051 C program to calculate the checksum byte for the data
25H, 62H, 3FH, and 52H.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mydata[]={0x25,0x62,0x3F,0x52};
unsigned char sum=0;
unsigned char x;
unsigned char chksumbyte;
for (x=0;x<4;x++)
{
P2=mydata[x];
sum=sum+mydata[x];
P1=sum;
}
chksumbyte=~sum+1;
P1=chksumbyte; }Department of Computer Science and Information Engineeri
Write an 8051 C program to perform the checksum operation to
ensure data integrity. If data is good, send ASCII character G to P0.
Otherwise send B to P0.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mydata[]
={0x25,0x62,0x3F,0x52,0xE8};
unsigned char shksum=0;
unsigned char x;
for (x=0;x<5;x++)
chksum=chksum+mydata[x];
if (chksum==0)
P0=G;
else
P0=B;
}
Write an 8051 C program to convert 11111101 (FD hex) 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;
}
The 8051 C compiler allocates RAM
locations
Bank 0 addresses 0 7
Individual variables addresses 08 and
beyond
Array elements addresses right after
variables
Array elements need contiguous RAM locations
and that limits the size of the array due to the
fact that we have only 128 bytes of RAM for
everything
Stack addresses right after array
elements
Compile and single-step the following program on your 8051
simulator. Examine the contents of the 128-byte RAM space to locate
the ASCII values.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=ABCDEF; //RAM space
unsigned char z;
for (z=0;z<=6;z++)
P1=mynum[z];
}
Department of Computer Science and Information Engineering
Write, compile and single-step the following program on your 8051
simulator. Examine the contents of the code space to locate the values.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mydata[100]; //RAM space
unsigned char x,z=0;
for (x=0;x<100;x++)
{
z--;
mydata[x]=z;
P1=z;
}
}
Department of Computer Science and Information Engineeri
One of the new features of the 8052
was an extra 128 bytes of RAM space
The extra 128 bytes of RAM helps the
8051/52 C compiler to manage its
registers and resources much more
effectively
We compile the C programs for the
8052 microcontroller
Use the reg52.h header file
Choose the8052 option when compiling
the program
Department of Computer Science and Information Engineering
N
Compile and single-step the following program on your 8051
simulator. Examine the contents of the code space to locate the ASCII
values.
Solution:
#include <reg51.h>
void main(void)
{
code unsigned char mynum[]=ABCDEF;
unsigned char z;
for (z=0;z<=6;z++)
P1=mynum[z]; }
Compare and contrast the following programs and discuss the
advantages and disadvantages of each one.
(a)
#include <reg51.h>
void main(void)
{
P1=H;
P1=E;
P1=L;
P1=L;
P1=O;
}
...
(b)
#include <reg51.h>
void main(void)
{
unsigned char mydata[]=HELLO;
unsigned char z;
for (z=0;z<=5;z++)
P1=mydata[z];
}
(c)
#include <reg51.h>
void main(void)
{
code unsigned char mydata[]=HELLO;
unsigned char z;
for (z=0;z<=5;z++)
P1=mydata[z];
}
Use the RAM data space to stoSERIALIZATION
Serializing data is a way of sending a
byte of data one bit at a time through
a single pin of microcontroller
Using the serial port (Chap. 10)
Transfer data one bit a time and control
the sequence of data and spaces in
between them
In many new generations of devices such as
LCD, ADC, and ROM the serial versions are
becoming popular since they take less space on
a PCB
Department of Computer Science and Information Engineering
NaWrite a C program to send out the value 44H serially one bit at a time
via P1.0. The LSB should go out first.
Solution:
#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;
}
}
Department of Computer Science and Information EnNational Cheng Kung U
Write a C program to send out the value 44H serially one bit at a time
via P1.0. 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;
} }
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;
}
Department of Computer Science and Information Engineering
NWrite a C program to bring in a byte of data serially one bit at a time
via P1.0. The MSB should come in first.
Solution:
#include <reg51.h>
sbit P1b0=P1^0;
sbit regALSB=ACC^0;
bit membit;
void main(void)
{
unsigned char x;
for (x=0;x<8;x++)
{
membit=P1b0;
ACC=ACC<<1;
regALSB=membit;
}
P2=ACC;
}
Home Automation, Networking, and Entertainment