Arduino Projects    |   Raspberry Pi  |     Electronic Circuits   |   Electronics Reference Design Library  |   AVR    |    PIC    |    8051    |    Electronic Projects

Interrupts & Programming 8051 Hardware Interrupts

Written By: 
Himanshu Choudhary
Programming Interrupts
While programming interrupts, first thing to do is to specify the microcontroller which interrupts must be served. This is done by configuring the Interrupt Enable (IE) register which enables or disables the various available interrupts. The Interrupt Enable register has following bits to enable/disable the hardware interrupts of the 8051 controller.
 
 
To enable any of the interrupts, first the EA bit must be set to 1. After that the bits corresponding to the desired interrupts are enabled. ET0, ET1 and ET2 bits are used to enable the Timer Interrupts 0, 1 and 2, respectively. In AT89C51, there are only two timers, so ET2 is not used. EX0 and EX1 are used to enable the external interrupts 0 and 1. ES is used for serial interrupt.
 
EA bit acts as a lock bit. If any of the interrupt bits are enabled but EA bit is not set, the interrupt will not function. By default all the interrupts are in disabled mode.
 
Note that the IE register is bit addressable and individual interrupt bits can also be accessed.
For example –
IE = 0x81; enables External Interrupt0 (EX0)
IE = 0x88; enables Serial Interrupt
 
Setting the bits of IE register is necessary and sufficient to enable the interrupts. Next step is to specify the controller what to do when an interrupt occurs. This is done by writing a subroutine or function for the interrupt. This is the ISR and gets automatically called when an interrupt occurs. It is not required to call the Interrupt Subroutine explicitly in the code.
 
An important thing is that the definition of a subroutine must have the keyword interrupt followed by the interrupt number. A subroutine for a particular interrupt is identified by this number. These subroutine numbers corresponding to different interrupts are tabulated below.
 
Number
Interrupt
Symbol
0
External0
EX0
1
Timer0
IT0
2
External1
EX1
3
Timer1
IT1
4
Serial
ES
5
Timer2
ET2
 
For example : Interrupt routine for Timer1
void ISR_timer1(void) interrupt 3
{
<Body of ISR>
}
For example : Interrupt routine for External Interrupt0 (EX0)
void ISR_ex0(void) interrupt 0
{
<Body of ISR>
}
Note that the interrupt subroutines always have void return type. They never return a value.
 
You are here