Programming External Hardware Interrupts
Programming External Hardware Interrupts
Programming External Hardware Interrupts
In the level-triggered mode, INTO and INT1 pins are normally high (just like all I/O port pins) and if a low-level signal
is applied to them, it triggers the interrupt. Then the microcontroller stops whatever it is doing and jumps to the interrupt
vector table to service that interrupt. This is called a level-triggered or level-activated interrupt and is the default mode
upon reset of the 8051. The low-level signal at the INT pin must be removed before the execution of the last instruction
of the interrupt service routine, RETI; otherwise, another interrupt will be generated. In other words, if the low-level
interrupt signal is not removed before the ISR is finished it is interpreted as another interrupt and the 8051 jumps to the
vector table to execute the ISR again. Look at Example 11-5.
Example 11-5
Assume that the INT1 pin is connected to a switch that is normally high. Whenever it goes low, it should turn on an
LED. The LED is connected to PI .3 and is normally off. When it is turned on it should stay on for a fraction of a
second. As long as the switch is pressed low, the LED should stay on.
Solution:
In this program, the microcontroller,is looping continuously in the HERE loop. Whenever the switch on INT1
(pin P3.3) is activated, the microcontroller gets out of the loop and jumps to vector location 0013H. The ISR for
INT1 turns on the LED, keeps it on for a while, and turns it off before it returns. If by the time it executes the
RETI instruction, the INT1 pin is still low, the microcontroller initiates the interrupt again. Therefore, to end this
problem, the INT1 pin must be brought back to high by the time RETI is executed.
Figure 11-5. Minimum Duration of the Low Level-Triggered Interrupt (XTAL = 11.0592 MHz)
Edge-triggered interrupts
As stated before, upon reset the 8051 makes INTO and INT1 low-level triggered interrupts. To make them edge-
triggered interrupts, we must program the bits of the TCON register. The TCON register holds, among other bits, the
ITO and IT1 flag bits that determine level- or edge-triggered mode of the hardware interrupts. ITO and IT1 are bits DO
and D2 of the TCON register, respectively. They are also referred to as TCON.O and TCON.2 since the TCON register
is bit-addressable. Upon reset, TCON.O (ITO) and TCON.2 (III) are both Os, meaning that the external hardware
interrupts of INTO and INT1 pins are low-level triggered. By making the TCON.O and TCON.2 bits high with
instructions such as “SETB TCON. 0″ and “SETB TCON. 2″, the external hardware interrupts of INTO and INT1
become edge-triggered. For example, the instruction “SETB CON. 2″ makes INT1 what is called an edge-triggered
interrupt, in which, when a high-to-low signal is applied to pin P3.3, in this case, the controller will be interrupted and
forced to jump to location 0013H in the vector table to service the ISR (assuming that the interrupt bit is enabled in the
IE register).
Example 11-6
Assuming that pin 3.3 (INT1) is connected to a pulse generator, write a program in which the falling edge of the pulse
will send a high to PI.3, which is connected to an LED (or buzzer). In other words, the LED is turned on and off at the
same rate as the pulses are applied to the INT1 pin. This is an edge-triggered version of Example 11-5.
The falling edge is latched by the 8051 and is held by the TCON register. The TCON. 1 and TCON.3 bits hold the
latched falling edge of pins INTO and INT1, respectively. TCON.l and TCON.3 are also called IEO and IE1,
respectively, as shown in Figure 11-6. They function as interrupt-in-service flags. When an interrupt-in-service flag is
raised, it indicates to the external world that the interrupt is being serviced and no new interrupt on this INTw pin will
be responded to until this service is finished. This is just like the busy signal you get if calling a telephone number that
is in use. Regarding the ITO and IT1 bits in the TCON register, the following two points must be emphasized.
1. The first point is that when the ISRs are finished (that is, upon execution of
instruction RETI), these bits (TCON.l and TCON.3) are cleared, indicating
that the interrupt is finished and the 8051 is ready to respond to another inter
rupt on that pin. For another interrupt to be recognized, the pin must go back
to a logic high state and be brought back low to be considered an edge-trig
gered interrupt.
2. The second point is that while the interrupt service routine is being executed,
the INT« pin is ignored, no matter how many times it makes a high-to-low
transition. In reality one of the functions of the RETI instruction is to clear the
corresponding bit in the TCON register (TCON. 1 or TCON.3). This informs us
that the service routine is no longer in progress and has finished being serv
iced. For this reason, TCON. 1 and TCON.3 in the TCON register are called
interrupt-in-service flags. The interrupt-in-service flag goes high whenever a
falling edge is detected at the INT pin, and stays high during the entire execu
tion of the ISR. It is only cleared by RETI, the last instruction of the ISR.
Because of this, there is no need for an instruction such as “CLR TCON. 1″
(or “CLR TCON. 3″ for INT1) before the RETI in the ISR associated with the
hardware interrupt INTO. As we will see in the next section, this is not the case
for the serial interrupt.
Example 11-7
What is the difference between the RET and RETI instructions? Explain why we cannot use RET instead of RETI as
the last instruction of an ISR.
Solution:
Both perform the same actions of popping off the top two bytes of the stack into the program counter, and making the
8051 return to where it left off. However, RETI also performs an additional task of clearing the interrupt-in-service flag,
indicating that the servicing of the interrupt is over and the 8051 now can accept a new interrupt on that pin. If you use
RET instead of RETI as the last instruction of the interrupt service routine, you simply block any new interrupt on that
pin after the first interrupt, since the pin status would indicate that the interrupt is still being serviced. In the cases of
TFO, TF1, TCON.l, and TCON.3, they are cleared by the execution of RETI.
More about the TCON register
Next we look at the TCON register more closely to understand its role in handling interrupts. Figure 11-6 shows
the bits of the TCON register.
ITO and IT1
TCON.O and TCQN.2 are referred to as ITO and IT 1, respectively. These two bits set the low-level or edge-
triggered modes of the external hardware interrupts of the INTO and INT1 pins. They are both 0 upon reset,
which makes them low-level triggered. The programmer can make either of them high to make the external
hardware interrupt edge-triggered. In a given system based on the 8051, once they are set to 0 or 1 they will not
be altered again since the designer has fixed the interrupt as either edge- or level-triggered.
TCON.L and TCON.3 are referred to as IEO and IE1, respectively. These bits are used by the 8051 to keep track of the
edge-triggered interrupt only. In other words, if the ITO and IT1 are 0, meaning that the hardware interrupts are low-
level triggered, IEO and IE1 are not used at all. The IEO and IE1 bits are used by the 8051 only to latch the high-to-low
edge transition on the INTO and INT1 pins. Upon the edge transition pulse on the INT0 (or INT1) pin, the 8051 marks
(sets high) the IE* bit in the TCON register, jumps to the vector in the interrupt vector table, and starts to execute the
ISR. While it is executing the ISR, no H-to-L pulse transition on the INTO (or INT1) is recognized, thereby preventing
any interrupt inside the interrupt. Only the execution of the RETI instruction at the end of the ISR will clear the lEx bit,
indicating that a new H-to-L pulse will activate the interrupt again. From this discussion we can see that the IEO and
IE1 bits are used internally by the 8051 to indicate whether or not an interrupt is in use. In other words, the programmer
is not concerned with these bits since they are solely for internal use.
TRO and TR1
These are the D4 (TCON.4) and D6 (TCON.6) bits of the TCON register. We were introduced to these bits in Chapter 9.
They are used to start or stop timers 0 and 1, respectively. Although we have used syntax such as “SETB TRx” and
“CLR Trx”, we could have used instructions such as “SETB TCON.4″ and “CLR TCON. 4″ since TCON is a bit-
addressable register. TFO and TF1
These are the D5 (TCON.5) and D7 (TCON.7) bits of the TCON register. We were introduced to these bits in Chapter 9.
They are used by timers 0 and 1, respectively, to indicate if the timer has rolled over. Although we have used the syntax
“JNB TFx, target” and “CLR Trx”, we could have used instructions such as “JNB TCON. 5, target” and “CLR TCON.
5″ since TCON is bit-addressable.
Related Links
8051 Microcontroller
8051 MICROCONTROLLERS
MICROCONTROLLERS AND EMBEDDED PROCESSORS
OVERVIEW OF THE 8051 FAMILY
8051 ASSEMBLY LANGUAGE PROGRAMMING
INSIDE THE 8051
:: Search WWH ::
Search
Custom Search