Interrupts 8051 Microcontroller How To Use External Interrupt
Interrupts 8051 Microcontroller How To Use External Interrupt
1. RESET INTERRUPT:
When reset pin is activated, controller jumps to execute code from 0000H memory
location. Mostly it is not used. It is also known as power on reset.
2. TIMER INTERRUPTS:
Two timers (T0 and T1) are present in 8051 microcontroller which is responsible for
Timer interrupt. A timer interrupt informs the microcontroller that corresponding Timer
has finished the counting.Memory locations 000BH and 001BH in the interrupt vector
table belongto Timer0 and Timer1 respectively.
3. EXTERNAL INTERRUPTS:
There are two external interrupts (INT0 and INT1) to serve external devices.
Pinnumbers 12 and 13 in port 3 are for the external hardware interrupts.Both these
interrupts are active low. An external interrupt informs the microcontroller that an
external device needs its routine service.Memory locations 0003H and 0013H in the
interrupt vector table belong to INT0 and INT1 respectively.
4. SERIAL INTERRUPT:
This interrupt is used for serial communication. It has a single interrupt that belongs to
both receive andtransmit. When enabled, it notifies the controller whether a byte has
been received or transmitted.The interrupt vector table location 0023H belongs to this
interrupt.
REGISTER CONFIGURATION (for interrupt selection):
Now we have to specify the microcontroller that which interrupts must be served. All of
the above interrupts can be used by configuringsome bit in special function register
known as Interrupt Enabled (IE) register. This registers enable or disable the various
available interrupts.
INTERRUPTS 8051 microcontroller ENABLE (IE)
REGISTER
EA: Enable Interrupt
EA bit must be set to 1 to enable any of the interrupts. By default all the interrupts are in
disabled mode.
EA = 1 Enable Interrupt
EA = 0 Disable Interrupt
ET2: Timer2 interrupt enable bit
Enable or disable Timer2 overflow or capture interrupt only in 8052. In AT89C51, there
are only two timers, so ET2 is not used.
ES: Serial port interrupt enable bit
Enable or disable Serial port interrupt.
ET1: Timer1 interrupt enable bit
ET0 = 1, Enable Timer1 overflow interrupt
ET0 = 0, Disable Timer1 overflow interrupt.
EX1: External interrupt INT1 enable bit
EX1 = 1, Enable INT1
EX1 = 0, Disable INT1
ET0: Timer0 interrupt enable bit
ET0 = 1, Enable Timer0 overflow interrupt
ET0 = 0, Disable Timer0 overflow interrupt
EX1: External interrupt INT0 enable bit
EX1 = 1, Enable INT0
EX1 = 0, Disable INT0
INTERRUPT SERVICE ROUTINE:
After the selection of specific interrupt, next step is to specify the microcontroller that
what to do when an interrupt occurs. We write a subroutine or function for the interrupt
which is the ISR. It is automatically called when an interrupt occurs. The definition of
subroutine must have the keyword interrupt with the interrupt number. Each interrupt
has its specific subroutine number.
EXTERNAL INTERRUPTS:
Since we already discussed how to use timers in 8051, so in this article we will just
learn how to use external interrupts in 8051. External interrupts are received from the
external interfaced devices at INTx pins of the microcontroller. These can be level
triggered or edge triggered which is decided by TCON register.
For level triggered: interrupt is enabled for a low at INTx pin.
For edge triggered: interrupt is enabled for a high to low transition at INTx pin.
TCON REGISTER:
Setting the IT0 and IT1 bits make the external interrupt 0 and 1 edge triggered
respectively. By default these bits are set to 0, so external interrupt is level triggered.
IT1: External interrupt1 signal type control bit.
IT1 = 1, to enable external interrupt 1 to be triggered by a falling edge signal
IT1 = 0, to enable a low level signal on external interrupt 1 to generate an interrupt
IT0: External interrupt 0 signal type control bit, same as IT1.
IT0 = 1, to enable external interrupt 0 to be triggered by a falling edge signal
IT0 = 0, to enable a low level signal on external interrupt 1 to generate an interrupt
PROGRAMMING STEPS:
1. Enable external interrupt 0 or external interrupt 1 by configuring IE register. For
this we have to give command as:
For INT0: IE=0x81;
For INT1: IE=0x84;
2. Now we have to write routine for external interrupt.For EX0 (INT0), the interrupt
number is 0 and for EX1 (INT1) interrupt number is 1.
For using external interrupt INT0 we have to write routine as:
void ISR_ex0(void) interrupt 0
For using external interrupt INT1 we have to write routine as:
void ISR_ex1(void) interrupt 2
3. For edge triggered external interrupt, IT0 or IT1 is set to 1.
For INT0: IT0=1;
For INT1: IT1=1;
We used the external interrupt INT0 of 8051 microcontroller. Port 2 is used to monitor
the output. In beginning, alternate values are passed to P2 LEDs. When external
interrupt is given through a push button, Interrupt service routine will start to execute
and LEDs output will get toggle for 1 sec. This 1 sec delay is given through timer 0
when it is in mode 1.
void ISR_ex0(void);
void Delay();
void main()
{
P0 = 0x00; // Make all pins zero
while(1)
led1= 1;
led2= 0;
led3= 1;
led4= 0;
led1=~led1;
led2=~led2;
led3=~led3;
led4=~led4;
Delay();
void Delay()
int i;
for(i=0;i<200;i++)
TL0=0xCC;