- An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. Whenever an interrupt occurs, the controller completes the execution of the current instruction and starts the execution of an Interrupt Service Routine (ISR) or Interrupt Handler.
- Hardware Interrupts
- Software Interrupts
- A hardware interrupt is an electronic alerting signal sent to the processor from an external device, like a disk controller or an external peripheral.
- For example, when we press a key on the keyboard or move the mouse, they trigger hardware interrupts which cause the processor to read the keystroke or mouse position.
- A software interrupt is caused either by an exceptional condition or a special instruction in the instruction set which causes an interrupt when it is executed by the processor.
- For example, if the processor’s arithmetic logic unit runs a command to divide a number by zero, it causes a divide-by-zero exception, thus causing the computer to abandon the calculation or display an error message.
Execution of Interrupt:
Steps to Execute an Interrupt
- The microcontroller closes the currently executing instruction and saves the address of the next instruction (PC) on the stack.
- It also saves the current status of all the interrupts internally (i.e., not on the stack).
- It jumps to the memory location of the interrupt vector table that holds the address of the interrupts service routine.
- The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it. It starts to execute the interrupt service subroutine, which is RETI (return from interrupt).
- Upon executing the RETI instruction, the microcontroller returns to the location where it was interrupted.
- First, it gets the program counter (PC) address from the stack by popping the top bytes of the stack into the PC. Then, it starts to execute from that address.
- A level-triggered interrupt module always generates an interrupt whenever the level of the interrupt source is asserted.
- Level-triggered interrupts force firmware engineers to take into account what is generating the interrupt source. If the interrupt source is just a pulse from a state machine, then the device drivers do not need to do any additional work.
- An edge-triggered interrupt module generates an interrupt only when it detects an asserting edge of the interrupt source.
- The edge may be detected when the interrupt source level changes, or it may be detected by periodic sampling and detecting an asserted level when the previous sample was deserted.
Interrupts can be generated from several sources:
- The timer interrupts one of the Arduino timers.
- External Interrupts from a change in the state of one of the external interrupt pins.
- Pin-change interrupts a change in the state of any one of a group of pins.