The programs we have written so far stop and wait whenever they need to mark off time. delay_ms(1000) produces an accurate one-second delay, but for that entire second the CPU can do nothing else, not read a sensor, not update a display, not notice a button. This chapter introduces the two mechanisms that end the waiting. A hardware timer is a circuit that counts on its own, in parallel with whatever the CPU is doing. An interrupt is the hardware’s way of tapping the CPU on the shoulder when something (a timer reaching its period, a button changing state) needs attention, without the CPU ever having to ask. Together they are how embedded software becomes responsive and efficient at the same time.
Convert a polled timer into an interrupt-driven one: enable the update interrupt, enable the timer’s line in the NVIC (Nested Vectored Interrupt Controller), and write an Interrupt Service Routine under its exact vector-table name.
Share state between an ISR and main() through a volatile flag, and apply the three ISR rules: the name is exact, get in and out fast, and do the minimum.