Open-Source Firmware Technician • Intermediate • Estimated study time: 45 minutes.
Timer input capture lets a microcontroller record an incoming signal’s edge timing in hardware. Unlike the output waveform generation taught in MCU timer and PWM diagnostics, input capture measures a signal supplied to the timer. The distinction matters when diagnosing fan tachometers, sensor pulses and digital timing faults.
Prerequisites and learning objectives
Review GPIO thresholds and open-drain outputs, clock trees and basic C arithmetic. Learners will identify capture registers and edge selection, calculate timer tick duration, frequency and duty cycle, handle rollover, distinguish missed captures from missing input signals, and validate a capture routine with safe bench tests.
Capture hardware and clock configuration
A timer counts using a peripheral clock. An accepted rising or falling edge copies its count into a capture register; the device may signal an interrupt or use DMA or a FIFO to deliver the value. Capture hardware reduces the influence of software interrupt latency on timestamps, but unread captures can be overwritten or lost. See STMicroelectronics’ input-capture guide and Microchip’s input capture mode description for concrete examples of different peripheral implementations.
Determine the actual timer clock from the device clock tree; it need not equal the CPU clock or peripheral bus clock. In the common STM32 prescaler convention, f_tick = f_timer / (PSC + 1). A 48 MHz timer clock and PSC = 47 produce 1 MHz, or a 1 µs tick. Consult the target reference manual for alternate-function pin mapping, prescaler semantics and digital input filter settings. The clock source diagnostics lesson explains why oscillator and PLL settings matter.
Period and duty cycle
Capture two successive rising edges with timestamps A and C; capture the intervening falling edge B using hardware that supports the required edge configuration. The period in ticks is the elapsed count from A to C, and high time is the elapsed count from A to B. At 1 MHz, 1,000 period ticks correspond to 1 ms and 1,000 Hz. A 250-tick high interval corresponds to 250 µs and 25% duty cycle. Reject impossible or stale samples and do not divide by zero. A frequency estimate based on one tick has limited resolution; averaging valid periods may reduce quantization noise but cannot correct a wrong clock frequency.
Counter rollover and measurement range
A full-range 16-bit up-counter repeats every 65,536 ticks. For an interval shorter than one complete count cycle, elapsed ticks equal the modular difference of the two counts. For example, 65,000 followed by 464 yields 1,000 ticks. A configurable auto-reload register changes the modulus to ARR + 1; the 16-bit cast below is valid only when ARR is 65,535. Equal timestamps are ambiguous without extra overflow information. Longer intervals require reliable overflow accounting or a wider or slower timer. At 1 MHz the largest unambiguous positive interval is 65.535 ms, corresponding to a minimum single-period frequency near 15.26 Hz. At 2 MHz the interval is 32.7675 ms; at 4 MHz it is 16.38375 ms. Faster ticks improve nominal time resolution but shorten the unambiguous interval.

The following portable C17 example tests modular arithmetic for a full-range 16-bit counter; it does not configure real hardware:
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
/* Full-range 16-bit up-counter; interval must be 1..65535 ticks. */
static uint16_t elapsed(uint16_t first, uint16_t second) {
return (uint16_t)((uint32_t)second – (uint32_t)first);
}
int main(void) {
assert(elapsed(1000, 2000) == 1000);
assert(elapsed(65000, 464) == 1000);
assert(elapsed(65535, 0) == 1);
assert(elapsed(42, 42) == 0); /* ambiguous/invalid measurement */
printf(“4/4 capture arithmetic checks passed\n”);
return 0;
}
Expected output: 4/4 capture arithmetic checks passed. Host execution was tested with GCC 14.2.0 and Clang 17.0.0, each with C17 and strict warnings enabled. Target MCU register access, interrupts, overflow accounting and live timing are outside the host test.
Safe bench validation and fault isolation
Use an authorized low-voltage development board, a signal generator within the input’s specified voltage and frequency range, and a correctly rated oscilloscope. Confirm common reference potential before connecting a grounded bench probe; never attach a ground clip to an unknown energized node. Power down before rewiring when required. Configure the correct pin and capture edge, verify the input waveform independently, then compare captured ticks with the expected period at a known timer clock. Inspect filter and prescaler settings if the measured frequency is wrong. Check overcapture status or FIFO/DMA errors, interrupt enablement and software data handoff if events disappear. For an open-drain input verify pull-up voltage, current and rise time. Keep a timeout and invalidate stale readings rather than reporting an old frequency as current. Review interrupt latency and jitter for the distinction between recorded timestamps and software servicing.
Exercises and answer key
A. What does the capture register store? B. Calculate tick frequency for a 48 MHz clock and PSC = 95. C. At 1 MHz, calculate elapsed ticks and frequency for successive rising timestamps 65,000 and 464. D. What is the duty cycle for a 250-tick high interval and a 1,000-tick period? E. Why is a zero modular difference not a valid frequency measurement? F. What would distinguish missing input edges from missed software servicing?
Answers: A. Counter value at the accepted input edge. B. 500 kHz. C. 1,000 ticks and 1,000 Hz, assuming less than a complete cycle. D. 25%. E. It may represent no elapsed ticks or an exact multiple of the modulus; division by zero is invalid. F. Compare the signal on a scope with hardware capture/overcapture status and software logs. Supervised target testing is required for field competency.
Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.
BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply