Programming Interrupts in 8051
Programming Interrupts in 8051
The timer interrupts IT0 and IT1 are related to Timers 0 and 1, respectively. The interrupt
programming for timers involves following steps :
2. Load initial values in THx and TLx for mode 0 and 1; or in THx only for mode 2.
5. Write subroutine for Timer Interrupt. The interrupt number is 1 for Timer0 and 3 for
Timer1.
6. To stop the timer, clear TRx in the end of subroutine. Otherwise, it will restart from 0000H
in case of modes 0 or 1 and from initial values in case of mode 2.
7. If the Timer has to run again and again, it is required to reload initial values within the
routine itself (in case of mode 0 and 1). Otherwise after one cycle timer will start counting from
0000H.
Example code:
Timer interrupt to blink an LED; Time delay in mode1 using interrupt method
#include<reg51.h>
TL0=0x66;
main()
TL0 = 0x66;
while(1); // do nothing
The external interrupts are the interrupts received from the (external) devices interfaced with the
microcontroller. They are received at INTx pins of the controller. These can be level triggered or edge
triggered. In level triggered, interrupt is enabled for a low at INTx pin; while in case of edge
triggering, interrupt is enabled for a high to low transition at INTx pin. The edge or level trigger is
decided by the TCON register. The TCON register has following bits:
Setting the IT0 and IT1 bits make the external interrupt 0 and 1 edge triggered respectively. By
default these bits are cleared and so external interrupt is level triggered.
Note : For a level trigger interrupt, the INTx pin must remain low until the start of the ISR and should
return to high before the end of ISR. If the low at INTx pin goes high before the start of ISR, interrupt
will not be generated. Also if the INTx pin remains low even after the end of ISR, the interrupt will be
generated once again. This is the reason why level trigger interrupt (low) at INTx pin must be four
machine cycles long and not greater than or smaller than this.
2. Write routine for external interrupt. The interrupt number is 0 for EX0 and 2 for EX1
respectively.
Example code:
//Level trigger external interrupt
void main()
IE = 0x81;
while(1);
{
<body of interrupt>
Example code:
//Edge trigger external interrupt
void main()
IE = 0x84;
IT1 = 1;
while(1);
<body of interrupt>
To use the serial, interrupt the ES bit along with the EA bit is set. Whenever one byte of data is sent
or received, the serial interrupt is generated and the TI or RI flag goes high. Here, the TI or RI flag
needs to be cleared explicitly in the interrupt routine (written for the Serial Interrupt).
3. Write routine or function for the Serial Interrupt. The interrupt number is 4.
Example code:
Send ‘A’ from serial port with the use of interrupt
void main()
TMOD = 0x20;
TH1 = -1;
SCON = 0x50;
TR1 = 1;
IE = 0x90;
while(1);
if(TI==1)
SBUF = ‘A’;
TI = 0;
else
RI = 0;
Example code:
// Receive data from serial port through interrupt
void main()
TMOD = 0x20;
TH1 = -1;
SCON = 0x50;
TR1 = 1;
IE = 0x90;
while(1);
if(TI==1)
TI = 0;
}
else
val = SBUF;
RI = 0;