STM32 Timer Input Capture: PWM Frequency and Duty Cycle Measurement

Added:

DMA-Backed GPIO Oversampling and Software-Defined Filtering

While hardware-based Timer Input Capture is highly efficient for clean, moderate-frequency signals, it suffers from significant limitations when dealing with noisy environments or high-frequency thresholds. Traditional input capture triggers an interrupt on every signal edge, which can quickly saturate the CPU at high frequencies, while high-frequency noise can cause false edge triggering. An alternative, more robust paradigm is DMA-backed GPIO oversampling. Instead of relying on hardware edge-detection, a system timer triggers a Direct Memory Access (DMA) controller to sample the state of the GPIO port into a circular buffer at a fixed, high rate. The firmware then analyzes the buffered digital pattern in software. This approach completely decouples signal measurement from CPU interrupt latency, inherently filters out high-frequency glitch noise, and allows for the simultaneous analysis of multiple complex signals without exhausting scarce hardware timer capture channels.

Basic understanding of STM32 MCU architecture and register-level (bare-metal) C programming.
Fundamentals of hardware timers, including prescalers, auto-reload registers (ARR), and counter directions.
Core concepts of Pulse Width Modulation (PWM), including frequency, period, active-high/active-low polarities, and duty cycle.
Familiarity with microcontroller interrupt handling and writing basic Interrupt Service Routines (ISRs).
Implementing Direct Memory Access (DMA) with input capture to measure high-frequency signals without CPU overhead.
Utilizing STM32 Timer Encoder Mode to interface with rotary encoders for velocity and position tracking.
Decoding complex sensor protocols (such as DHT11/DHT22 temperature sensors or infrared remote controls) using input capture pulse width analysis.
Mastering advanced timer synchronization techniques, including master/slave modes and triggering external peripherals.
1.3K views19likes10:48@GettobyteOriginal Release: 2022-05-27

The STM32 timer peripheral input capture mode captures external signals by storing the counter value at the capture register when an edge detector detects a transition (rising or falling edge), while PWM input mode captures both rising and falling edges using two capture registers to calculate frequency, duty cycle, and time period of square wave signals.