Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 280 of 9364. Peripheral Interfacing and ControlFree lesson

Measuring External Signal Frequency with Input Capture

Learners will configure timer capture registers to record precise timestamp values upon detecting external signal edges for frequency analysis.

In this lesson, you will learn how to use the Input Capture peripheral to measure the frequency of an incoming signal by recording precise hardware timestamps at the moment edges occur.

The Logic of Time-Stamping

Until now, you have used timers to push signals out (like PWM) or trigger events at set intervals. Input Capture works in reverse: it lets the timer "watch" an external signal. When the hardware detects a specific edge (a rising or falling signal transition), it instantly copies the current value of the free-running timer counter into a capture register.

Think of it like a stopwatch at a track meet: the timer keeps running, but the capture register "freezes" the current time the moment a runner (the external signal) hits the finish line (the GPIO edge).

Calculating Frequency

To calculate frequency, we capture two consecutive rising edges. If the first edge is captured at Time_A and the second at Time_B, the period of the signal is the difference between the two timestamps.

// Concept of calculating period from capture registers
uint32_t period = capture_register_2 - capture_register_1;
uint32_t frequency = timer_clock_frequency / (prescaler * period);

Configuring the Hardware

To set this up, you must configure three components:

  1. Edge Detection: Configure the input pin to trigger on a rising edge (0 to 1 transition).
  2. Timer Prescaler: Set a prescaler that allows the timer to run fast enough to be precise, but slow enough that it doesn't overflow between two signal pulses.
  3. Interrupt Handling: Enable the Input Capture Interrupt. When the capture event occurs, the hardware automatically stores the timestamp and fires an interrupt. Inside your Interrupt Service Routine (ISR), you retrieve the value from the capture register before the next edge overwrites it.

By comparing the captured "snapshot" of the timer with the known clock speed of your microcontroller, you can determine exactly how many CPU cycles passed between two signal pulses, allowing for high-precision frequency measurement regardless of software latency.

Key Takeaway: Input Capture provides a hardware-level snapshot of a timer's value upon an external signal transition, allowing you to calculate signal frequency by measuring the duration between consecutive captured timestamps.