Timer Systems in Modern MCUs
Modern microcontrollers (MCUs) include versatile timer/counter modules that provide precise timing, waveform generation, and event capture functionality.
Timers are used for:
- Generating delays and scheduling tasks
- Producing PWM signals for motor drives and LED dimming
- Measuring frequency, period, and pulse width of input signals
- Quadrature decoding for rotary encoders
- Event triggering and inter-peripheral synchronization
Prescaler
A prescaler divides the MCU clock frequency before it reaches the timer, allowing the timer to count at a slower, configurable rate.
For example, a 72 MHz MCU clock with a prescaler of 72 will make the timer increment at 1 MHz.
Prescalers are critical for:
- Adjusting timing resolution
- Extending maximum count period for long delays
- Generating specific output frequencies
Basic Timer / One-Shot Mode
Basic timers increment or decrement a counter based on the prescaled clock.
- One-Shot Mode: Timer counts to a specified value and stops, generating a single event or interrupt.
- Auto-Reload Mode: Timer resets automatically for periodic events.
Pulse Width Modulation (PWM)
PWM allows generation of a digital waveform with configurable duty cycle:
- Used for motor speed control, LED dimming, and audio signals
- Timer compares its counter to a capture/compare register
- When counter < compare value → output HIGH; else → output LOW
- Frequency determined by timer period, resolution by counter bits
Differential PWM / Dead-Time Control
For motor drives and H-bridge circuits, timers can output complementary PWM signals:
- Two outputs are 180° out-of-phase
- Dead time inserted to prevent both high- and low-side switches from conducting simultaneously
- Essential for safe operation in brushless DC and stepper motors
Input Capture
Input Capture allows a timer to record the counter value when an external event occurs:
- Measures pulse width, frequency, or period of external signals
- Can trigger interrupts on rising, falling, or both edges
- Used in sensors, encoders, and event timing applications
Quadrature / Encoder Timer
Timers can interface with quadrature encoders to measure rotation:
- Two-phase signals (A & B) indicate direction and position
- Timer counts increments/decrements based on signal edges
- Used in robotics, motor position sensing, and CNC systems
Advanced Timer Features
Modern MCU timers also support:
- Trigger outputs to synchronize other peripherals (ADC, DAC, PWM)
- External clock sources for specialized timing
- Master/Slave timer chaining for extended period
- Dead-time insertion for complementary outputs
- Gate control to enable/disable counting based on signals
- Event capture and compare match outputs for complex waveforms