Timers are the heartbeat of an embedded system. You use them to:
- Generate PWM (motor drive, LED dim, audio).
- Periodically interrupt the CPU (RTOS tick, sensor sampling).
- Measure pulse widths (input capture: tachometer, ultrasonic sensor).
- Count external events (encoder pulses, frequency counter).
- Time stamp events with sub-microsecond resolution.
A typical Cortex-M chip has 8-15 timers, each 16- or 32-bit, with multiple compare and capture channels.
Free-running timer. Counts up, rolls over, repeat. Reading the counter gives a time stamp. STM32's TIM2 32-bit at 84 MHz overflows in s, which makes it a great microsecond clock.
Counter mode. Same timer driven by an external pin instead of the system clock. Connect it to a wheel encoder, read counts.
PWM mode. Counter compares against a duty register; output toggles. Already covered in section 2.3.
Capture mode. A pin event (rising or falling edge) latches the current counter value into a capture register. You read it from the ISR. Used to measure PWM pulse widths from an RC receiver or the time between gas-pump dispenser pulses.
4.1 Watchdog timer
Imagine the firmware enters an infinite loop because of a bug, a glitch, or a stack corruption. There is nobody around to press reset. The system is bricked.
The watchdog timer prevents that. It is a separate hardware timer that, if it ever expires, resets the CPU. The firmware must "kick" or "feed" the watchdog regularly (write a magic value to its register) to prove it is still alive. If the firmware hangs, kicks stop, the watchdog overflows, the chip resets, and the system recovers.
while (1) {
do_work();
IWDG->KR = 0xAAAA; // kick the independent watchdog on STM32
}Discipline rule: never kick the watchdog from inside an ISR or from a thread that does not actually verify "the system is healthy." A common bug is to kick from a 1 kHz timer ISR: the timer keeps firing even when the main loop is dead, and the watchdog never trips. The right pattern is to require all tasks to set a flag, and a single supervisor function checks all flags before kicking. That way, a stuck task is detected.
Window watchdog. A stronger variant: must be kicked between a min and max time. Kicking too early or too late triggers reset. Defeats stuck loops that fire fast.
Real-world. Every automotive ECU has a watchdog. Pacemakers have multiple, redundant. Industrial PLCs use them. NASA's Curiosity rover firmware has a watchdog.
4.2 Real-time clock (RTC)
The watchdog and free-running timers stop when the chip powers off or sleeps. To track wall-clock time across power cycles you need an RTC: a low-power oscillator (32.768 kHz crystal is the standard, because Hz makes one second from a counter), a counter, and a tiny battery (often a CR2032 lasting 5-10 years).
RTCs commonly speak I2C: DS1307 (cheap, drifts), DS3231 (TCXO, very accurate), MCP7940. Many MCUs (STM32, ESP32, MSP430) integrate an RTC; you only add the crystal and a backup capacitor or coin cell.
Use cases: data logging (timestamp every sample), alarm clocks, scheduled wake-ups in low-power systems (RTC alarm wakes CPU from standby once an hour to take a reading).