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.


Related Posts:

  • Microcontrollers - Overview A microcontroller is a small and low-cost microcomputer, which is designed to perform the specific tasks of embedded systems like displaying microwave’s information, receiving remote signals, etc. The general mic… Read More
  • Microcontrollers - 8051 Interrupts Interrupts are the events that temporarily suspend the main program, pass the control to the external sources and execute their task. It then passes the control to the main program where it had left off. 8051 has 5 interru… Read More
  • Microcontrollers 8051 Input Output Ports 8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or output. Hence, total 32 input/output pins allow the microcontroller to be connected with the peripheral devices. Pin configuration,… Read More
  • Microcontrollers - 8051 Pin Description The pin diagram of 8051 microcontroller looks as follows − Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other functions. It is internally pulled up, bi-directional I/O port. Pin 9 … Read More
  • Microcontrollers - 8051 Architecture 8051 microcontroller is designed by Intel in 1981. It is an 8-bit microcontroller. It is built with 40 pins DIP (dual inline package), 4kb of ROM storage and 128 bytes of RAM storage, 2 16-bit timers. It consists of are fou… Read More