Q: How to implement accurate time measurement using STM32 timer input capture?
Answer
Use a timer in input capture mode to measure pulse width period or frequency. Connect the signal to a timer channel with TIx pin. Configure the prescaler to divide APB clock to get desired resolution. Read CCR (capture/compare register) at each rising/falling edge to compute period. For >16-bit range use two timers in cascade (one as prescaler). For encoder interfaces configure the timer in encoder mode with TI1 and TI2 inputs. X2 encoding doubles the resolution; X4 is available. For ultrasonic distance sensors use input capture with timer overflow interrupt to detect echoes >65 ms. Enable DMA on the capture channel to store multiple readings without CPU overhead.
Filed under: FAQ
