Interrupts are powerful features in microcontrollers that allow them to respond to events efficiently. The 8051 microcontroller supports several types of interrupts, enabling it to handle multiple tasks concurrently. In this tutorial, we’ll explore how to use interrupts in the 8051, including their configuration and usage, complete with an example code in C using Keil C51.
Overview of 8051 Interrupts
The 8051 microcontroller has five interrupt sources:
- External Interrupt 0 (INT0): Triggered by a signal on pin P3.2.
- Timer 0 Overflow Interrupt (TF0): Triggered when Timer 0 overflows.
- External Interrupt 1 (INT1): Triggered by a signal on pin P3.3.
- Timer 1 Overflow Interrupt (TF1): Triggered when Timer 1 overflows.
- Serial Communication Interrupt (RI/TI): Triggered by the completion of data reception or transmission.
Interrupt Vector Table
Each interrupt has a specific location in the memory where its service routine begins. This is called the interrupt vector address.
Interrupt Source | Vector Address |
External Interrupt 0 (INT0) | 0x0003 |
Timer 0 Overflow Interrupt | 0x000B |
External Interrupt 1 (INT1) | 0x0013 |
Timer 1 Overflow Interrupt | 0x001B |
Serial Communication Interrupt | 0x0023 |
Enabling Interrupts
The 8051 uses the Interrupt Enable (IE) and Interrupt Priority (IP) registers to control interrupt behavior.
- IE Register: Enables or disables specific interrupts.
- EA: Global enable/disable.
- ET0: Enable Timer 0 interrupt.
- EX0: Enable External Interrupt 0.
- ET1: Enable Timer 1 interrupt.
- EX1: Enable External Interrupt 1.
- ES: Enable Serial interrupt.
- IP Register: Sets priority levels for interrupts.
- PT0: Priority for Timer 0.
- PX0: Priority for External Interrupt 0.
- PT1: Priority for Timer 1.
- PX1: Priority for External Interrupt 1.
- PS: Priority for Serial interrupt.
Writing an Interrupt Service Routine (ISR)
An ISR is a special function that executes when an interrupt occurs. In C, the interrupt keyword is used to define an ISR, followed by the interrupt vector number.
void timer0_ISR(void) interrupt 1 {
// Code to execute when Timer 0 overflows
}
Example: Blinking an LED Using Timer 0 Interrupt
In this example, we use Timer 0’s interrupt to toggle an LED every 50 milliseconds.
#include <reg51.h> // Include 8051 header file
void timer0_ISR(void) interrupt 1;
void main() {
TMOD = 0x01; // Timer 0 in Mode 1 (16-bit mode)
TH0 = 0xFC; // Load high byte for 50ms delay at 12 MHz
TL0 = 0x66; // Load low byte for 50ms delay at 12 MHz
ET0 = 1; // Enable Timer 0 interrupt
EA = 1; // Enable global interrupts
TR0 = 1; // Start Timer 0
while (1) {
// Main loop doing other tasks
}
}
void timer0_ISR(void) interrupt 1 {
P1 = ~P1; // Toggle Port 1
TH0 = 0xFC; // Reload high byte
TL0 = 0x66; // Reload low byte
}
Explanation of the Code
- Timer Initialization: Timer 0 is configured in Mode 1 (16-bit timer mode) using the TMOD register.
- Interrupt Enabling: The ET0 bit enables the Timer 0 interrupt, and the EA bit enables global interrupts.
- ISR: The timer0_ISR function toggles Port 1 every time Timer 0 overflows.
- Reloading Timer: The timer registers (TH0 and TL0) are reloaded in the ISR to maintain consistent timing.
External Interrupt Example
Here’s an example of using External Interrupt 0 to turn an LED on or off based on a button press.
#include <REGX51.H>
void external0_ISR(void) interrupt 0;
void main() {
IT0 = 1; // Configure INT0 for edge-triggered mode
EX0 = 1; // Enable External Interrupt 0
EA = 1; // Enable global interrupts
P1 = 0x00; // Initialize Port 1 to OFF
while (1) {
// Main loop doing other tasks
}
}
void external0_ISR(void) interrupt 0 {
P1 = ~P1; // Toggle Port 1
}
Applications of Interrupts
Interrupts allow microcontrollers to respond to real-time events efficiently. Common applications include:
- Real-Time Clock: Generating periodic interrupts for timekeeping.
- Input Event Handling: Responding to button presses or sensor signals.
- Communication: Handling data transmission and reception.
- Multitasking: Managing concurrent tasks in embedded systems.
Conclusion
Interrupt programming is a cornerstone of embedded system development. By understanding how to configure and use interrupts in the 8051, you can create responsive and efficient applications. Practice using different interrupt sources and experiment with priority settings to master this essential skill.