How does interrupts works exactly in microcontrollers?
Interrupts are how a microcontroller stops what it’s doing (briefly) to handle an important event right now , then returns to exactly where it left off. Here’s how it works “under the hood”, step by step. The core idea Your main code runs in a loop (or an RTOS task). Hardware events happen asynchronously: a timer hits zero, a UART byte arrives, a GPIO edge occurs, ADC completes, etc. Instead of polling (“are we there yet?”), the MCU uses an interrupt : a hardware signal that asks the CPU to run a specific function called an ISR (Interrupt Service Routine). What happens when an interrupt occurs (exact sequence) 1) An event sets an interrupt flag Example: a timer overflows → the timer peripheral sets a status bit like TIMERx_IF = 1 . 2) The interrupt controller decides if it should fire An interrupt triggers the CPU only if: The peripheral’s interrupt is enabled (local enable bit), The interrupt is unmasked/enabled in the interrupt controller (e.g., N...