ABC of Electronics cosycom.com
EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 9

Interrupts

Letting hardware tap the CPU on the shoulder.

Polling means checking an input over and over in a loop. An interrupt reverses this: when an event happens, such as a pin change or a finished timer, the CPU pauses its work, runs a short handler, then resumes exactly where it stopped.

Keep handlers tiny. Set a flag or copy one value, then let the main loop do the slow work. Never wait or print inside a handler.

Data shared with a handler must be volatile, and multi-byte values may need interrupts briefly disabled while you read them.

EXAMPLE
volatile uint8_t tick = 0;

void timer_isr(void) {   /* hypothetical handler name */
    tick = 1;            /* just raise a flag */
}

for (;;) {
    if (tick) { tick = 0; update_display(); }
}