What is the difference between input capture and output compare modes?
Input capture uses the timer to measure external signals. When an edge is detected on the timer's input pin (TIx), the timer's current count value is latched into the CCR register, preserving a precise timestamp of when the edge occurred. By capturing timestamps of successive edges, firmware can calculate signal frequency, period, pulse width, or duty cycle without any CPU timing involvement. For example, capturing two consecutive rising edges and computing the difference gives the period. Capturing a rising edge and the next falling edge gives the pulse width. This is the basis for tachometers, frequency counters, ultrasonic distance sensors, and IR remote decoders. Input capture can also trigger DMA to store a sequence of timestamps without CPU intervention — essential for decoding complex pulse trains.
Output compare uses the timer to generate precisely timed output events. When the counter matches the CCR value, the hardware can toggle, set, clear, or freeze the output pin — all in hardware with zero CPU latency. PWM generation is the most common output compare application. Outside of PWM, output compare is used for one-shot pulse generation, precise timing of external events (triggering an ADC conversion, firing a strobe), and generating waveforms with specific timing relationships. The fundamental difference is directionality: input capture reads external timing into the MCU, while output compare drives MCU timing out to the external world.
A common interview trap: candidates conflate input capture with simply reading a GPIO pin in an interrupt. The key difference is that input capture latches the counter value at the exact instant of the edge in hardware, giving sub-microsecond timing precision. A GPIO interrupt only tells you an edge happened — by the time the ISR reads the timer counter, interrupt latency (potentially several microseconds with higher-priority interrupts pending) has corrupted the measurement.
Source: Timers & PWM Q&A
