Categories: Firmware Guide-8051

Push Button Interfacing with 8051 microcontrollers

A microcontroller, being an integrated circuit with a processor, contains support devices like program memory, data memory, I/O ports and serial communication interface integrated together. Since all the required support devices are available with the microcontroller, external interfacing of devices is not required. One of the most popular microcontrollers is Intel 8051. It belongs to the MCS-51 family of Intel microcontrollers. Many companies later adopted the MCS-51 core to develop their own microcontrollers and all these devices could be operated with the MCS-51 instruction sets. The basic difference between these devices is in the size of the memory or in the presence of an ADC or DAC. AT89S51 is such a microcontroller developed by Atmel. This chapter talks about how push-button interfacing with AT89S51 is carried out.

Push Buttons

Push buttons are used for switch mechanisms in embedded systems. A switch connected to an IO pin when pressed can change the state of the pin to low or high depending on its initial state. The change can be identified in firmware and can make the controller work accordingly. This tutorial includes a simple example of interfacing a push button with the AT89S51 controller so as to glow an LED.

The push button is connected to P0.0 (0th pin of port0). The pin is connected externally with a pull-up resistor since there is no internal pull-up resistor for port 0. The other end of the pushbutton is connected to ground. The voltage at the pin will be VCC when the button is not pushed. It will get grounded on pressing the button. The LED connected to the P2.0 pin is connected with a series resistor to limit the current.

Firmware Interfacing

The initial part of the code includes the library files and defines the identifiers.

#include<reg51.h>
#define LED P2_0
#define switch_pin P0_0

The reg51.h is general header file for 8051 microcontrollers which define all the identifiers. P2.0 pin with LED connected is the macro defined as LED and the pin P0.0 with push button is a macro defined as switch_pin.

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

A delay function is defined before the main to keep a small delay for blinking the LED.
The whole process of scanning for the input at port 0 and configure for the blinking of LED is done inside the main function.

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
    }
  }
}

The port pin with push button connected should be configured as input pin by loading a 1 in the corresponding port bit. The LED is made off initially by loading a 1 in the P2.0. Inside an infinite loop, the pin with push button connected is scanned continuously for a low input. If we press the button the input pin will get grounded and voltage will be zero. LED blinks as long as the button is pressed.

Share