8051 LED On OFF control with Pushbutton

Hardware Connections:




Push button switch is connected to the first bit of PORT 0 (P0.0) which is configured as an input pin. Which is connected to a pull up resistor as there is NO INTERNAL PULL UP RESISTORS FOR PORT P0. Thus P0.0 pin is at Vcc potential when the switch is not pressed. When the switch is pressed this pin P0.0 will be grounded. The LED is connected to the first bit of PORT 2 (P2.2) and a resistor is connected in series with it to limit the current.


Program:



//Program by UdayaPrakashJayaraman

#include<reg52.h>                                     /* special function register declarations */
int LedPin;
int SwitchPin:

void delay(int k)                                       //function for delay
{
  unsigned int i, j;
  for(i=0;i<k;i++)
  {
    for(j=0;j<100;j++);
  }
 
 
sbit LedPin = P2^0;                                   //Defining LED PIN
sbit SwitchPin = P0^0                              //Defining Switch PIN

int main()
{
  SwitchPin = 1;                                        // Making Switch PIN input
  LedPin=1;                                             //LED off initially

  while(1)                                               //infinite loop
  {
    if(SwitchPin == 0 )                           //If switch pressed
    {
      LedPin = 0;                                    //LED ON
      delay(2000);                                 //Delay
      LedPin = 1;                                 //LED OFF
    }
  }
}

return 0;
}


Note: You can do this project without 10KΩ PULL UP resistor by connecting switch to any other port.