8051 Interrupts
8051 Interrupts
8051 Interrupts
Dinesh Sharma
February 3, 2021
Since the call and execution of the handler program can take
place at any time, the handler function should not alter any flag or
register which is used by the main program.
The start address of the handler function may have to be loaded
at locations designated by the processor architecture, so that it
can be looked up when the handler function is called. (These are
called interrupt vectors).
The time between signaling an event and starting of its handler is
called interrupt latency. The hardware and software associated
with the interrupt mechanism should make the interrupt latency as
short as possible.
The task of saving registers can be simplified by bank switching.
Port Line P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0
Function RD WR T1 in T0 in INT1 INT0 TxD RxD
Half of the special function register TCON is used for setting the
conditions for causing interrupts from external sources.
This register is bit addressable.
Bit No. 7 6 5 4 3 2 1 0
Bit Name TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Bit Addr 8F 8E 8D 8C 8B 8A 89 88
Bits 0 and 2 set the interrupt type (level or edge). Bits 1 and 3 indicate
the current status of the external interrupts.
Bit No. 7 6 5 4 3 2 1 0
Bit Name TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Bit Addr 8F 8E 8D 8C 8B 8A 89 88
IT1 and IT0 are the “Interrupt Type” flags for external sources 1
and 0 respectively. These decide whether a negative going edge
or a ‘LOW’ level will cause an interrupt.
If the bit is set, the corresponding interrupt is edge sensitive. If it is
cleared, the interrupt is level sensitive.
IE1 and IE0 are the status flags for the two external interrupt lines.
If the flag is 1, the selected type of event (edge or level) has
occurred on the corresponding interrupt line.
Dinesh Sharma (Department Of Electrical Engineering, IIT Bombay)
8051 Interrupts February 3, 2021 13 / 34
Internal Interrupts
Bit No. 7 6 5 4 3 2 1 0
Bit Name TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Bit Addr 8F 8E 8D 8C 8B 8A 89 88
8051 timers always count up. When their count rolls over from the
maximum count to 0000, they set the corresponding timer flag
TF1 or TF0 in TCON.
Counters run only while their run flag (TR1 or TR0) is set by the
user program. When the run flag is cleared, the count stops
incrementing.
The 8051 can be setup so that an interrupt occurs whenever TF1
or TF0 is set.
Bit No. 7 6 5 4 3 2 1 0
Function IE U U SI TF1 Ex1 TF0 Ex0
Bit Addr AF AE AD AC AB AA A9 A8
Bit Name EA - - ES ET1 EX1 ET0 EX0
Bit No. 7 6 5 4 3 2 1 0
Function IE U U SI TF1 Ex1 TF0 Ex0
Bit Addr AF AE AD AC AB AA A9 A8
Bit Name EA - - ES ET1 EX1 ET0 EX0
Bit No. 7 6 5 4 3 2 1 0
Function IE U U SI TF1 Ex1 TF0 Ex0
Bit Addr AF AE AD AC AB AA A9 A8
Bit Name EA - - ES ET1 EX1 ET0 EX0
Bit No. 7 6 5 4 3 2 1 0
Bit Addr BF BE BD BC BB BA B9 B8
Bit Name U U U PS PT1 PX1 PT0 PX0
Bit No. 7 6 5 4 3 2 1 0
Bit Addr BF BE BD BC BB BA B9 B8
Bit Name U U U PS PT1 PX1 PT0 PX0
Need Resource
The handler also needs the resource.
It checks the flag and finds that it is not set
yes to busy. It sets the flag as busy and starts
Busy? Wait
using the resource.
no
Interrupt may occur here Now when the main program runs, it starts
running from the point where it was
Set Busy Flag
interrupted (after checking, before setting):
Use Resource It now sets the flag to busy (unaware that
it is already set!) and starts writing to the
Clear Busy Flag
buffer, destroying the work done by the
handler!
Need Resource