Professional Documents
Culture Documents
UNIT 7:
Interrupts Programming: 8051 Interrupts, Programming Timer Interrupts,
Programming External Hardware Interrupts, Programming the Serial
Communication Interrupts, Interrupt Priority in the 8051/52, Interrupt
programming in C
The 8051 provides five interrupt sources. These are listed below.
1. Timer 0 (TFO) and timer 1 (TF1) interrupt.
2. External hardware interrupts, INTO and INT1.
Veena Hegde, BMSCE, Bangalore
Page 1
Microcontroller
3. Serial communication interrupt TI and RI
Vector location
0003H
00OBH
Page 2
Microcontroller
External hardware Interrupt 1 (INT1)
0013H
001BH
0023H
Symbol
EA
ET2
ES
ET1
EX1
ET0
EX0
IE.6
(Reserved)
ET2
IE.5
(Reserved)
ES
IE.4
Page 3
Microcontroller
ET1
IE.3
EX1 IE.2
ETO IE.1
EXO IE.0
Page 4
Microcontroller
interrupt is generated, the flag that generated it is cleared by the hardware when the service
routine is vectored to only if the interrupt was transition-activated. If the interrupt was levelactivated, then the external requesting source is what controls the request flag, rather than the onchip hardware.
If ITx = 0, external interrupt x is triggered by a detected low at the INTx pin. If ITx = 1,
external interrupt x is edge-triggered. In this mode if successive samples of the INTx pin show a
high in one cycle and a low in the next cycle, interrupt request flag IEx in TCON is set. Flag bit
IEx then requests the interrupt.
Since the external interrupt pins are sampled once each machine cycle, an input high or low
should hold for at least 12 oscillator periods to ensure sampling. If the external interrupt is
transition-activated, the external source has to hold the request pin high for at least one machine
cycle, and then hold it low for at least one machine cycle to ensure that the transition is seen so
that interrupt request flag IEx will be set. IEx will be automatically cleared by the CPU when the
service routine is called.
If the external interrupt is level-activated, the external source has to hold the request active until
the requested interrupt is actually generated. Then it has to deactivate the request before the
interrupt service routine is completed, or else another interrupt will be generated.
Example: Write an 8051 ALP to glow LED for a fraction of second when external interrupt
INTO is activated.
Solution
ORG 0003H
SETB P1.0
Turn ON LED
; Load count
DJNZ BACK
CLR P1.0
RET I
SJMP HERE
Veena Hegde, BMSCE, Bangalore
Page 5
Microcontroller
END
Serial Communication Interrupts and Programming
The Serial port Interrupt is generated by the logical OR of RI and TI. Neither of these flags
is cleared by hardware when the service routine is vectored to. In fact, the service routine will
normally have to determine whether it was RI or TI that generated the interrupt, and the bit will
have to be cleared in software.
In this case, the 8051 can perform other tasks in addition to serial communication, i.e. sending
and receiving data from serial communication port.
We know that transmit interrupt (TI) flag is set (=1) when the last bit of the framed data
(stop bit) is transmitted. This indicates that the SBUF register is ready to transmit the next byte.
The receive interrupt (RI) flag is set (=1) when the complete frame of data (with stop bit) is
received. RI indicates that the received byte needs to be picked up before it is lost by new
incoming serial data.
All the above concepts are applied equally using polling or an interrupt. Only difference is in
serving the serial communication needs. In polling method, the flag (TI or RI) is monitored. The
8051 cannot do anything else until this flag is set to high. This problem is solved using interrupt
method. When 8051 has received a byte or is ready to send the next byte, the RI or TI flag
respectively is set. Any other work can be performed while the serial communication needs are
served. There is a single interrupt set aside for serial communication. If IE register (IE.4) is
enabled, when RI or TI is set (= 1), the 8051 is interrupted. When interrupted, the ISR written at
0023h is executed by 8051. In ISR, the TI and RI flags must be examined to check which one
caused the interrupt and according to flag the response is given.
Interrupt priority
Each interrupt source can also be individually programmed to one of two priority levels by
setting or clearing a bit in Special Function Register IP. A low-priority interrupt can itself be
interrupted by a high-priority interrupt, but not by another low priority interrupt. A high-priority
interrupt cant be interrupted by another interrupt source.
Page 6
Microcontroller
Bit
IP.7
IP.6
IP.5
IP.4
IP.3
IP.2
IP.1
IP.0
Page 7
Microcontroller
Processing Interrupts
When an interrupt occurs and is accepted by the CPU, the main program is interrupted. The
following actions occur:
The current instruction completes execution.
The PC is saved on the stack.
The current interrupt status is saved internally.
Interrupts are blocked at the level of the interrupt.
The PC is loaded with the vector address of the ISR
The ISR executes.
The ISR finishes with an RETI instruction, which retrieves the old value of PC from the
stack and restores the old interrupt status. Execution of the main program continues where it
left off.
0
Main
000BH
T0ISR
001BH
T1ISR
0030H
Page 8
Microcontroller
RETI
T1ISR:CLR
TR1
;stop T1
MOV TH1,#HIGH(-1000)
MOV TL1,#LOW(-1000) ;1 ms for T1
SETB TR1
; START TIMER 1
CPL P1.6
;500-Hz on P1.6
RETI
Page 9