8051 Programming in C: The 8051 Microcontroller and Embedded Systems: Using Assembly and C
8051 Programming in C: The 8051 Microcontroller and Embedded Systems: Using Assembly and C
HANEL 12
TIME DELAY Write an 8051 C program to toggle bits of P1 ports continuously with
(cont’) 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);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
13
I/O LEDs are connected to bits P1 and P2. Write an 8051 C program that
PROGRAMMING shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary)
on the LEDs.
void main(void)
{
unsigned char message[]
=“The Earth is but One Country”;
unsigned char z;
for (z=0;z<28;z++) //send 28 characters
{
LCDData=message[z];
En=1; //a high-
En=0; //-to-low pulse to latch data
}
}
I/O Write an 8051 C program to turn bit P1.5 on and off 50,000 times.
PROGRAMMING Solution: We can access a single bit of any
sbit MYBIT=0x95; SFR if we specify the bit address
Accessing SFR
void main(void)
Addresses {
80 - FFH unsigned int z;
(cont’) for (z=0;z<50000;z++)
{
MYBIT=1;
MYBIT=0;
}
}
Bit-wise Solution:
#include <reg51.h>
Operators in C void MSDelay(unsigned int);
(cont’)
void main(void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0;
P2=P2^0xFF;
MSDelay(250);
}
}
LOGIC Write an 8051 C program to get bit P1.0 and send it to P2.7 after
OPERATIONS inverting it.
Solution:
Bit-wise #include <reg51.h>
sbit inbit=P1^0;
Operators in C sbit outbit=P2^7;
(cont’) 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
}
}
LOGIC Write an 8051 C program to read the P1.0 and P1.1 bits and issue an
OPERATIONS ASCII character to P0 according to the following table.
P1.1 P1.0
0 0 send „0‟ to P0
Bit-wise 1 1 send „1‟ to P0
Operators in C 2 0 send „2‟ to P0
1 1 send „3‟ to P0
(cont’)
Solution:
#include <reg51.h>
void main(void)
{
unsignbed char z;
z=P1;
z=z&0x3;
...
...
switch (z)
LOGIC {
OPERATIONS case(0):
{
P0=„0‟;
break;
Bit-wise }
Operators in C case(1):
{
(cont’) 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.
DATA
CONVERSION Solution:
#include <reg51.h>
Packed BCD to
ASCII void main(void)
Conversion {
unsigned char
x,y,z; unsigned
char mybyte=0x29;
x=mybyte&0x0F;
P1=x|0x30;
y=mybyte&0
xF0;
y=y>>4;
P2=y|0x30;
}
Serializing data is a way of sending a
DATA
SERIALIZATION
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
DATA Write a C program to send out the value 44H serially one bit at a time
SERIALIZATION via P1.0. The LSB should go out first.
(cont’) 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;
}
}
DATA Write a C program to send out the value 44H serially one bit at a time
SERIALIZATION via P1.0. The MSB should go out first.
(cont’) 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;
}
}
DATA Write a C program to bring in a byte of data serially one bit at a time
SERIALIZATION via P1.0. The LSB should come in first.
(cont’) 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;
}
DATA Write a C program to bring in a byte of data serially one bit at a time
SERIALIZATION via P1.0. The MSB should come in first.
(cont’) 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;
}