12 Interrupt Handling
Dr K. Vani
1. Interrupt
An interrupt is the occurrence of a condition–an event — that cause a temporary suspension of a program while the event is serviced by another program (Interrupt Service Routine ISR or Interrupt Handler).
Figure.1 shows ISR. In which first part of the figure shows main program is interrupted. The next part shows main program is paused and ISR is providing service to interrupt and when it finishes main program is restored.
1.1 Interrupt Vs Polling
Interrupt – is an external or internal event that interrupts the microcontroller to inform it that a device needs its service.
Serving a device can be done in two ways-
1) Interrupt
2) Polling
Interrupt: Here, a device needs service, and the device notifies the microcontroller by sending it an interrupt signal. After receiving an interrupt signal, the microcontroller stops whatever it is doing and serves the device. Program associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
Polling: In this, the microcontroller continuously monitors the status of the given devices. When the conditions are met, (that is, if the device needs some service) it performs the service. Next, it moves on to monitor the next device. It continues in this fashion until everyone is serviced.
Interrupt is preferred over polling for the following reasons:
● Polling is not efficient, because it wastes much of the microcontroller’s time by checking devices that do not need service.
● Interrupt can serve many devices; and each device can get the service of microcontroller based on priority.
● In polling there is no priority, only on round robin basis they can get the attention of the microcontroller.
● In interrupt, a microcontroller can also ignore a device’s request for service, this is not possible in the polling method.
2. Interrupt Service Routine(ISR):
Each interrupt has an interrupt service routine (ISR), or is called, the microcontroller runs the interrupt service location in memory that holds the address of the ISR.
interrupt handler. When an interrupt routine. Every interrupt has a fixed
In the above table, 8051 interrupts are listed. When the reset interrupt is given through 9th pin system is reset. At that time the service starts from 0000 location. The two port pins P3.2 and P3.3 are external hardware interrupts.
2.1 Steps followed when interrupt occurs
The group of memory locations that hold the addresses of ISRs is called an interrupt vector table.
1. Microcontroller completes the instruction it is executing and saves the address of the next instruction (PC) on the stack.
2. Saves the current status of the system internally in stack.
3. System starts executing ISR routine, where as the starting address of that ISR routine is referred from interrupt vector table
4. The microcontroller gets the location of the ISR from the interrupt vector table and control is transferred to that location. It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt).
5. After executing the RETI instruction, the microcontroller returns to the place where it was interrupted.
6. First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack into the PC.
7. Restores the system previous program status.
8. Then it starts to execute from that address.
2.2 Six Interrupts in the 8051
Six interrupts are allocated as follows
• Reset – power-up reset- It is nothing but restarting the system. When the reset is pressed the content of the system is stored and again the memory starts from 0000H.
• Two interrupts are set aside for the timers:
• One for timer 0 and one for timer 1
• Two interrupts are set aside for hardware external interrupts
• P3.2 and P3.3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2). For any application, interrupts are needed from outside so that the service can be done based on the input given from outside.
• Serial communication has a single interrupt that belongs to both receive and transfer
Interrupt service is ended by noticing return statement (RETI) in ISR routine. There are two returns in instruction set namely RET and RETI. Though it looks similar, there are some differences in them.
2.2 Differences between RET and RETI:
• RETI performs an additional task of clearing the interrupt-in-service flag.
• RET instead of RETI as the last instruction of the interrupt service routine, it blocks any new interrupt on that pin after the first interrupt, since the pin status would indicate that the interrupt is still being serviced.
• RETI instruction clears TF0, TF1, TCON.1, and TCON.3
3.1 Example code segment for 8051 Interrupts
The first line of code indicates that the program starts from the origin 0000H. Then LJMP MAIN means Long jump to main program. Next the Interrupt Service Routine starts from 30H memory location.
3.2 Enabling and Disabling an Interrupt
• Upon reset, all interrupts are disabled (masked)
▪ None will be responded to by the microcontroller if they are activated.
▪ The interrupts must be enabled by software in order for the microcontroller to respond to them
▪ There is a register called IE (interrupt enable) that is responsible for enabling (unmasking) and disabling (masking) the interrupts. In IE register, there are 8 bits. Based on those 8 bits, an interrupt can be enabled/disabled.
3.3 Steps in Enabling an interrupt:
• Bit D7 of the IE register (EA) must be set to high to allow the rest of register to take effect.
• The value of EA
• If EA = 1, interrupts are enabled and will be responded to if their corresponding bits in IE are high
• If EA = 0, no interrupt will be responded to, even if the associated bit in the IE register is high EA=1 enable all interrupts, EA=0 disable all interrupts
Both the EA pin and the corresponding interrupt pin must be 1 for enabling the particular interrupt. Different methods for enabling interrupts is listed below
Method-1
MOV IE,#10011001 ;enable serial, ;timer 1, EX0
Method-2 to perform the same task is
SETB IE.7 ;EA=1, global enable
SETB IE.4 ;enable serial interrupt
SETB IE.3 ;enable Timer 1 interrupt
SETB IE.0 ;enable EX0
To disable the interrupts Clear command can be used as shown below
CLR IE.3 ;mask (disable) timer 1 ;interrupt only
CLR IE.7 ;disable all interrupts
3.4 Timer Interrupt
For any application which needs timer for a specific purpose and runs for a particular period of time, timer interrupt can be used. The timer interrupt starts and stops the application automatically. The timer flag (TF) is raised when the timer rolls over. Interrupt handles Timer control flag effectively. In polling, System is held up till TF is raised but in Timer Interrupt, System is not locked.
In Timer Interrupts,
● If the timer interrupt in the IE register is enabled, whenever the timer rolls over, TF is raised.
▪ The microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR.
▪ In this way, the microcontroller can do other work until it is notified that the timer has rolled over.
Initial count starts from TH and TL register. When the TH and TL register gets FF overflow occurs, then TF0 is set to 1 and the interrupt is automatically enabled.
• if the timer interrupt is enabled, whenever TF=1, the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR
• In this way, the microcontroller can do other things until it is notified that the timer has rolled over.
3.4.1 Timer Interrupt Programming
Write a program that continuously gets 8-bit data from P0 and sends it to P1 while simultaneously creating a square wave of 200 ms period on pin P2.1. Use Timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz.
The first line of code indicates that the program is started from 0000H. LJMP indicates that the program has to go for long jump to reach the main loop. The next line indicates that the ISR Routine starts from 000BH.This ISR routine generates a square. The main program is written at position 0030H. In order to set the timer mode, timer 0 mode 2 is set. P0 is used as the input port. Here the square wave generation is for 200 ms, hence 100 ms is for on state and 100 ms for off state. For generating the delay of 100 ms, 92 is used as the count. when the port P0 reaches 0FFH overflow occurs, it means the delay of 92 count is over and the timer is set again. Once the data is stored in P0, it is moved to accumulator and then again the data is moved to port P1. Hence the data is continuously transferred from the Port P0 to P1.
4. Summary
In this lecture different concept of Interrupts and difference between interrupt and polling methods are discussed. The steps for executing the interrupts in 8051 are also discussed. The assembly code for 8051 Timer Interrupt Programming has been explained.
5. References
1. Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, “The 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition”.