Today I want to talk about interrupts, what are they, and some examples of them.

Imagin you have a push button connected to a microcontroller, you program it as follows: when push button is pressed turn on LED, otherwise turn off LED (I know that you can do this without microcontroller but just for the sake of explaining here). You could set it up to check the push button constantly and act accordingly, this wastes CPU resources to check something that rarely happens. However, with interrupt you can do the same task without constant checking the push button.

How can this be done that’s what I am going to explain next.

What is Microcontroller Interrupt?

Microcontroller interrupt is a signal that temporarily halt the main program and cause a specific function to be executed. Interrupt functions are sometimes called Interrupt Service Routine (ISR).

Interrupts are used to handle time-critical events such as real-time data acquisition, communication protocols, and sensor readings. In the example above, you can use interrupt to set the push button as a trigger.  

How do Interrupts work?

When an interrupt is triggered, the microcontroller saves the current state of the program and jumps to the interrupt service routine (ISR), then it returns to the main program and continue where it left off.

Since there is only one CPU that executes the main program and the interrupt (meaning they share the same hardware resources), the main program state needs to be saved somewhere else first, so it can return to where it left off. Otherwise the main program will be deleted and the microcontroller will not be able to go back to the main program.

When we say microcontroller saves the current state of the main program it means microcontroller saves things like: the program counter, the registers, the stack pointer, and link register. If you don’t know these just ignore them for now.

Also interrupts can be triggered by sensor input, timers, communication protocols (such as UART which I talked about here) and even software.

Examples of Interrupts

The examples here are applicable to ARM Cortex-M4 system, it might be different from other microcontrollers

Reset: This interrupt is generated when the microcontroller is reset. It is used to initialize the microcontroller and start the main program.

NMI (Non-Maskable Interrupt): This interrupt cannot be disabled by software. It is typically used for events that require immediate attention, such as a power failure or a hardware fault.

Hard Fault: This interrupt is triggered when a fault occurs during exception processing or when an exception cannot be handled. It is used to handle errors such as divide-by-zero or invalid memory access.

IRQ (Interrupt Request): This is a general-purpose interrupt that can be used for any event that needs to be handled by the microcontroller.

Not all interrupts are treated equally. Reset, NMI, Hard Fault are some examples of fixed interrupt and all ARM Cortex-M4 have them. They have the highest priority and are executed before anything else. The priority of these cannot change by programmer.

On the other hand, IRQs are programmable by software and their priority can be modified. You can set the priority for each IRQ depending on your application needs.

Conclusion

Today I talked briefly about microcontroller interrupts and explained what they are and how they work. Lastly I gave some examples of interrupts in ARM Cortex-M4.