Header Ads

LED and Switch Interfacing with 8051

Using Push Button Switch with 8051 and Keil C – AT89C51

This tutorial is for beginners in the field of microcontroller. In this case the microcontroller is AT89C51, a reprogrammable derivative of 8051. This purpose of this tutorial is to familiarize with the use of push button switch with the microcontroller. It will be useful whenever a decision is to be made according to the press of a switch. If you are not yet started with 8051 with Keil C.
LED and Switch Interfacing with AT89C51 8051 Microcontroller
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: 
#include<reg52.h> /* special function register declarations */
/* for the intended 8051 derivative */

sbit LED_pin = P2^0; //Defining LED PIN
sbit switch_pin = P0^0; //Defining Switch PIN

void Delay(int); //Function prototype declaration

void main (void)
{
  switch_pin = 1; // Making Switch PIN input
  LED_pin=1; //LED off initially

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

void Delay(int k)
{
  int j;
  int i;
  for(i=0;i<k;i++)
  {
    for(j=0;j<100;j++)
    {
    }
  }
}

No comments