EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 46
Project: Blink an LED with Timer Interrupts
Your first complete firmware.
Wire an LED and a resistor to one pin and ground. Configure the pin as an output, set a timer to fire every 500 ms, and toggle the pin inside its handler.
Compared with a delay loop, the CPU is free between blinks to do other work or sleep.
Check the result with a stopwatch: ten blinks should take about ten seconds.
EXAMPLE
volatile uint8_t flag = 0;
void timer_isr(void) { flag = 1; } /* hypothetical, fires every 500 ms */
int main(void) {
pin_output(3);
timer_start_ms(500);
for (;;) { if (flag) { flag = 0; pin_toggle(3); } }
}