Quick Answer
STM32 timers are versatile peripherals used for PWM generation (motor control, LED dimming), input capture (measuring pulse widths, frequency), and encoder interfaces (reading quadrature encoders for position feedback). Use advanced-control timers (TIM1, TIM8) for motor control with dead-time insertion, general-purpose timers (TIM2-TIM5) for PWM and input capture, and dedicated encoder mode for position sensing.
Introduction
Timers are among the most powerful and flexible peripherals in STM32 microcontrollers. They enable precise timing control, signal generation, and measurement—essential capabilities for motor control, power conversion, sensor interfacing, and many other embedded applications.
Core Content
1. PWM Generation
PWM Basics
PWM Frequency = Timer Clock / (PSC + 1) / (ARR + 1) Duty Cycle = CCR / (ARR + 1) × 100% Where: - PSC = Prescaler value - ARR = Auto-reload register (period) - CCR = Capture/compare register (duty cycle)
Applications
- Motor Control: Vary motor speed by changing duty cycle
- LED Dimming: Control LED brightness smoothly
- Switching Power Supplies: Generate gate drive signals
- Audio Generation: Produce tones and simple audio
2. Input Capture
How It Works
- Capture Event: Timer latches current count value on signal edge
- Pulse Width: Measure time between rising and falling edges
- Frequency: Measure period between consecutive edges
- Resolution: Timer clock frequency determines measurement resolution
Applications
- Ultrasonic Distance Sensors: Measure echo pulse width
- IR Remote Decoding: Capture timing of IR pulses
- Frequency Measurement: Measure signal frequency
- Tachometer: Measure RPM from sensor pulses
3. Encoder Interface
Quadrature Encoder Mode
- Two Inputs: TI1 and TI2 (encoder A and B channels)
- Direction: Determined by phase relationship between A and B
- Resolution: 1x, 2x, or 4x counting modes (count on one or both edges)
- Index: Optional third input for absolute position reference
Applications
- Motor Position Feedback: Precise position control
- Knobs and Dials: User input with rotation sensing
- CNC Machines: Position feedback for axes
- Robotics: Wheel and joint position sensing
4. Timer Selection Guide
| Timer Type | Examples | Features | Use Cases |
|---|---|---|---|
| Advanced-Control | TIM1, TIM8 | Dead-time, complementary outputs | Motor control, power conversion |
| General-Purpose | TIM2-TIM5 | PWM, input capture, encoder | General timing tasks |
| Basic | TIM6, TIM7 | Simple timing, DAC trigger | Time base, interrupts |
| Low-Power | LPTIM1 | Operates in Stop mode | Wake-up from low-power |
FAQ
How do I calculate PWM frequency and resolution?
Example: 84 MHz timer clock, target 20 kHz PWM. Set PSC = 0, ARR = 4200-1. Resolution = 1/4200 = 0.024%. Higher ARR = better resolution, lower frequency.
Can I use timers simultaneously?
Yes! STM32 has multiple independent timers that can run simultaneously. You can generate multiple PWM signals, perform input capture on different channels, and use encoder mode—all at the same time.
What’s the maximum PWM frequency?
Depends on timer clock. For 168 MHz timer clock, minimum ARR = 1, so maximum PWM frequency ≈ 84 MHz. However, practical limits are lower due to peripheral characteristics and output driver speed.
Conclusion
STM32 timers provide essential functionality:
- PWM: Variable duty cycle for motor control, dimming, power conversion
- Input Capture: Precise timing measurements, pulse width, frequency
- Encoder: Position and direction sensing from quadrature encoders
- Selection: Choose timer type based on required features
Need Help with Timer Applications?
InnovChip provides embedded development services for motor control, sensor interfacing, and precision timing applications. Contact us today for assistance with timer configuration and firmware development.
