8051 embedded c program
8051 embedded c program
Write a program for make led ON when if switch is pressed, make it OFF when the switch is released (use push button ).
#include<reg51.h>
sbit c=P1^1; // assigning port p1 pin 1 for use switch
void main()
{
P1=0x0ff; // making p1 port is input
P2=0x00; // making p2 port is output
P1=0x00; // initially make it port 1 is OFF condition
while(1) // infinite loop
{
if(c==1) // when the input is HIGH (switch is pressed)
{
P2=0x0ff; // p2 will go to high(led ON)
}
else
{
P2=0x00; // p2 will go to low(led OFF)
}
}
Explanation:
Here we are going to make a lid on and off when the button is pressed. IN 8051 is having a 4 port, generally the 4 port inbuiled GPIO pins. This pins are using as an either input or output pin. We assign the port pin as an input pin for the switch. In main function, we should assign which is the input and output port. Here we are using the port 1 for input and the port 2 is for output. Then made one while loop for continuous checking. When the input goes to high the LED will ON otherwise it will be OFF.