Interrupts are the events that temporarily suspend the main program, pass the control to other functions or sources and execute their task. It then passes the control to the main program where it had left off.
As code size increases and your application handles multiple modules, sequential coding would be too long and too complex. The interrupt mechanism helps to embed your software with hardware in a much simpler and efficient manner. In this topic, we will discuss the interrupts in 8051 using AT89S52 microcontroller.
When an interrupt is received, the controller stops after executing the current instruction. It transfers the content of the program counter into the stack.
Interrupts in 8051 includes :
Each interrupt can be enabled or disabled by setting bits of the IE register and the whole interrupt system can be disabled by clearing the EA bit of the same register.
When the RESET interrupt is received, the controller restarts executing code from 0000H location. This interrupt is not available to the programmer.
Each Timer is associated with a Timer interrupt. When a timer has finished counting, the Timer interrupt will notify the microcontroller by setting the required flag bit.
8051 based AT89S52 microcontroller has two active-low external interrupts, INT0 and INT1.
See more information here: External Interrupt in 8051 microcontroller
This interrupt is used for serial communication. When enabled, it notifies the controller when a byte has been received or transmitted according to how the interrupt is configured.
Now let’s go through the registers used for configuring interrupt in AT89S52 microcontroller. All the above-mentioned interrupts except reset interrupt can be individually enabled or disabled by setting or clearing a bit in Special Function Register IE. IE also contains a global disable bit, EA, which disables all interrupts at once. The priority of each interrupt can be configured using the IP register.
EA bit enables or disables all interrupt sources (globally):
EX0 bit enables or disables External interrupt 0:
ET0 bit enables or disables Timer T0 interrupt:
EX1 bit enables or disables External interrupt 1:
ET1 bit enables or disables Timer 1 overflow interrupt:
ES bit enables or disables serial port interrupt :
ET2 bit enables or disables Timer 2 overflow interrupt :
TCON register is also one of the registers whose bits are directly in control of timer operation. The 4 bits in LSB is used for interrupt control.
Once all the configurations are done, the next step is to write the functions to execute when an interrupt occurs. And this is done by writing ISR functions. This function gets called automatically when an interrupt occurs.
While writing the ISR, The function definition must have the keyword ‘Interrupt’ followed by the interrupt number. Interrupt number is unique for each interrupt signal and a subroutine for a particular interrupt is identified by this number.
Void ISR(void) interrupt 0 { <Body of ISR> }
Void timer0 (void) interrupt 1 { <Body of ISR> }
Void ex0 (void) interrupt 2 { <Body of ISR> }
Void timer1_ISR (void) interrupt 3 { <Body of ISR> }
Void serial (void) interrupt 4 { <Body of ISR> }
#include<reg51.h> #define LED P1; Void main() { TMOD = 0x01; TH0 = 0xFC; TL0 = 0x66; IE = 0x82; // enable interrupt TR0 = 1; //start timer while(1); } void timer0(void) interrupt 1 //interrupt number 1 for Timer 0 { LED = ~LED; //Toggles the LED on interrupt TH0=0xFC; TL0=0x66; }
Visit: External Interrupt in 8051 microcontroller
The below example will display the ASCII value of character received serially using LEDs connected in port 0.