Software Delay in 8051

Let us now get started with 8051 programming. We will First write a code for LED Blink.
Blinking of LED can be done by incorporating an delay between on and off of the port.
we will first discuss the ports in 8051. There are 4 ports in 8051 namely P0, P1, P2, P3.
each port is of 8 bit i.e we can say that each port has 8 pins.
8051 is byte addressable as well as bit addressable, we will discuss regarding he same in later section.
the point to be covered here is hot to create delay and assign 1's and 0's to pins.
each port can be set as output device by writing 0 to its bit eg. P0=0x00 makes port 0 as an output port and then P0=0xff makes the all 8 pins on the port as high whereas P0=0x00 makes all pins on port zero to ground.
Now let us talk about delay, delay can me created by 2 types 
1. Software Delay
2.Hardware Delay
In this article we will be discussing about software delay, we will cover hardware delay in later section.
In software delay delay is created simply by null loop
we will see the code which will make understanding clear

Code:-

#include <reg51.h>    // header file for 8051
void msdelay(unsigned int);  // function declaration for delay
void main(void)  // main function
{
                P0=0x00;   // declaring all port as output
P1=0x00;
P2=0x00;
P3=0x00;
while(1)  // this loop will repeat infinitely
{
P0=0xff;  // all pins on all port will be high
P1=0xff;
P2=0xff;
P3=0xff;
msdelay(300);  // delay of 300ms
P0=0x00;  // all pins on all port will be low
P1=0x00;
P2=0x00;
P3=0x00;
msdelay(300);
}
}
void msdelay(unsigned int itime) // main function for delay
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}



Hope this help
Feel free to contact if any difficulties
happy coding!

projectbandya

No comments:

Post a Comment