How do timers work in MCUs?


Timers in microcontrollers (MCUs) are hardware peripherals that count clock pulses to measure time intervals or generate precise time-based events. They’re fundamental for tasks like delays, PWM generation, event counting, and real-time control.
How Timers Work
A timer counts up (or down) based on a clock source. When the counter reaches a specific value (overflow or match), it can trigger an interrupt or an output event.
🔧 Core Components of a Timer
Counter Register
Stores the current count value.
Can be 8-bit, 16-bit, or 32-bit depending on the MCU.
Prescaler
Divides the input clock to slow down counting (e.g., divide 16 MHz by 16 = 1 MHz).
Allows adjusting the time base.
Auto-Reload or Compare Register
- Sets the value at which the timer resets or triggers an interrupt.
Interrupt Logic
- Fires an interrupt when the counter overflows or matches a value.
Control Registers
- Configure mode (up/down count, one-shot, continuous), enable/disable, etc.
Common Timer Modes
Mode | Description |
Basic Timer | Simple counting, delay generation |
Periodic Timer | Repeats at fixed intervals (e.g., 1ms tick for RTOS) |
PWM Mode | Generates Pulse Width Modulated signals |
Input Capture | Captures timer value on an external event (e.g., signal edge) |
Output Compare | Triggers an action when timer reaches a set value |
Encoder Interface | Reads position data from rotary encoders (used in motor control) |
Example: Delay Generation
Assume:
MCU clock = 16 MHz
Prescaler = 16000 → Timer ticks at 1 kHz (1 ms per tick)
Auto-reload = 1000 → Timer overflows every 1000 ms (1 second)
So the timer interrupt occurs once per second.
Use Cases
Delays & timeouts
PWM for motor control, LEDs
Capturing signal duration (ultrasonic sensors)
Scheduling tasks in RTOS
Frequency and pulse measurement
Subscribe to my newsletter
Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
