Polling and Interrupts in Microcontrollers

Consider a state where all input/output transfers of data are controlled by the program. When peripherals require attention, they signal the microprocessor by changing the voltage level of an input line. The microprocessor can then respond by jumping to a program service routine for the device. On completion of the routine, a return to the main program occurs. Program control of inputs/outputs is therefore a loop to read inputs and update outputs continuously, with jumps to service routines as needed. This process of repeatedly checking each peripheral device to see if it is ready to send or accept a new byte is referred to as polling.

An option to program control is interrupt control. An interrupt involves a peripheral device activating a separate interrupt request line. The reception of an interrupt results in the microprocessor suspending execution of its main program and jumping to the service routine for the peripheral. The interrupt must not lead to a loss of data and an interrupt handling routine has to be incorporated in the software so that the state of processor registers and the last address accessed in the main program are stored in dedicated locations in memory. Following the interrupt service routine, the contents of the memory are restored and the microprocessor can continue executing the main program from where it was interrupted.

During an interrupt, the following process takes place:

  1. The CPU waits until the end of the instruction it is currently executing before dealing with the interrupt.
  2. All the CPU registers are pushed on the stack and a bit set to stop further interrupts occurring during interruption. Note that the stack is a special area of memory in which program counter values can be stored when a subroutine is to be executed. The program counter gives the address of the next program instruction in a program and thus storing this value enables the program to be resumed at the place where it broke off to execute the interrupt.
  3. The CPU then establishes the address of the interrupt service routine to be executed. Some microprocessors have dedicated interrupt pins and the pin that is selected determines which address is to be utilized. Other microprocessors have only one interrupt pin and the interrupting device must then supply data that tells the microprocessor where the interrupt service routine is located. Some microprocessors have both kinds of interrupt inputs. The starting address of an interrupt service routine is called an interrupt vector. The block of memory assigned to store the vectors is known as the vector table. Vector addresses are fixed by the chip manufacturer.
  4. The CPU branches to the interrupt service routine.
  5. After completion of this routine, the CPU registers are returned from the stack and the main program continues from the point it left off.
Interrupt signal to microprocessor
Fig 1.0: Interrupt signal to microprocessor

The block diagram below illustrates the Interrupt Control Process:

Interrupt Control
Fig 1.1: Interrupt Control

Unlike a subroutine call, which is located at a specific point in a program, an interrupt can be called from point in the program. The program does not control when an interrupt occurs; the control lies with the interrupting event. For some interrupts it is possible to program the microprocessor to ignore the interrupt request signal unless an enable bit has been set. Such interrupts are referred to as maskable. As an example, with Intel 8051, interrupt sources are individually enabled or disabled through the bit-addressable register IE (interrupt enable) at address 0A8H, a 0 disabling an interrupt and a 1 enabling it. Furthermore, there is a global enable/disable bit in the IE register that is set to enable all external interrupts or cleared to disable all external interrupts.

Related: Beginner’s Step-by-Step Coding Course: Learn Computer Programming the Easy Way 

Please follow us and share:

Author: John Mulindi

John Mulindi has a background in a technical field and he writes on topics ranging from automation, computer systems, embedded systems, mechatronics to measurement and control.

Leave a Reply