This guide will demonstrate how to interface an external switch with an 8051 microcontroller and utilize interrupts to detect button presses. Upon a button press, an LED connected to the 8051 will turn off for a specific duration. This project showcases the capabilities of GPIO interrupts and their application in real-time event handling
An interrupt is an event that causes the microcontroller to stop what it is doing and execute a special routine called an interrupt service routine (ISR). Interrupts are used to handle events that happen outside the control of the microcontroller, such as a button press or a timer overflow.
The 8051 has five interrupt sources:
Reset - This interrupt is triggered when the power is turned on or when the reset button is pressed.
Timer 0 overflow - This interrupt is triggered when the timer 0 overflows.
Timer 1 overflow - This interrupt is triggered when timer 1 overflows.
Serial port interrupt - This interrupt is triggered when data is received or transmitted on the serial port.
External interrupt 0 - This interrupt is triggered when a low level is detected on the INT0 pin.
External interrupt 1 - This interrupt is triggered when a low level is detected on the INT1 pin.
The objective is to create a circuit in which when a push button is pressed, the interrupt is called and services the ISR (Interrupt Service Routine). In the code when ISR is called the LED gets the complement state of current for a particular period of time.
P89V51RD2
Breadboard
LED
Push button
Resistor (220-330 ohms)
Jumper wires
Circuit Diagram
Copy and paste the following code into main.c or download it from here:
#include <reg51.h>
void delay(void);
void ISR_ex0(void);
sbit led=P1^1;
void main()
{
P1 = 0x00; //setting as output port
P3 = 0x04; //setting P3.2 as input pin for interrupt0
IE=0x81;
IT0=1;
while(1)
{
led=1;
}
}
void ISR_ex0(void) //ISR(interrupt service routine) for external interrupt 0
{
led=~led;
delay();
}
void delay()
{
int i,j;
for(i=0;i<500;i++)
{
for(j=0;j<1000;j++)
{
}
}
}
Led is in a high state initially. When the push button is pressed it activates the interrupt and serves the ISR(interrupt service routine) in which it toggles the LED for 1 second. Whenever the push button is pressed the LED state toggles for one second and goes back to the initial state.
By interfacing an external switch with the 8051 microcontroller, a wide range of applications can be implemented. Examples include button-based menus, control panels, input selection, and various user interface scenarios. It enables the development of interactive systems that respond to user input, enhancing the versatility, usability, and functionality of embedded systems.