LED INTERFACING WITH 8051
LEARNING EMBEDDED C PROGRAMMING FROM SCRATCH
I just started with learning Embedded C programming so , I just decided to blog every single practice projects I code to upload it directly here to make sure i can easily memorized how i did it last time and to keep a track on my progress.
Let's Begin
Supplies
- 8051 Microcontroller
- LED
Steps and Circuit Connects
- first connects Anode of the LED's to the Port P2 pin of 8051 (You can change Output pin port of 8051 as per your desires to any other Port )
- Connect Cathode Pin of LED's to the Ground
- Connect the 5V power supply to the 8051 and ground it
- Check all the connects properly as per the above connects
- Now, dump the Hex file generated into 8051
C Program
#include<reg52.h>
void Delay(void); // Function prototype declaration
void main (void) //main
{
while(1) // infinite loop
{
P2 = 0x00; // LED ON
Delay(); //Function call
P2 = 0xff; // LED OFF
Delay();
}
}
void Delay(void)
{
int j;
int i;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
Detail Explaination to Program 8051
8051 is microprocessor while programming 8051 we need to include reg.52.h library
Any port of 8051 can be defined by just typing P0,P1,P2,P3
declaring P2 =1 it actually means we are initializing all pins in PORT2 as HIGH
To initialize the port either as LOW or HIGH we just need to declare port as 0 or 1 . eg - P2=0 or P2= 1
To Define any single pin from PORT we just need to say P2^0 where 0 points to pin 0 in port 2
or similarly to define port 2 pin4 we can write as P2^4
Lets me show how we can just use single pin instead of all
PROGRAM TO DEFINE PORT 2 PIN 0
#include <reg52.h>
sbit LED1 = P2^0;
void Delay(void);
void main (void)
{
while(1) // infinite loop
{
LED1 = 0; // LED ON
Delay();
LED1 = 1; // LED OFF
Delay();
}
}
void Delay(void)
{
int j;
int i;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}