How to Measure Frequency Using Timer in Embedded C
To measure frequency using a
timer in embedded C, configure the timer to count clock pulses or capture input signal edges, then calculate frequency as the inverse of the measured period. Use input capture mode or count timer overflows between signal edges to find the signal period, then compute frequency with frequency = 1 / period.Syntax
To measure frequency, you typically use a timer in input capture mode or count timer ticks between signal edges. The main steps are:
- Configure Timer: Set timer prescaler and mode.
- Enable Input Capture: Capture timer value on signal edge.
- Calculate Period: Subtract captured timer values.
- Compute Frequency: Frequency = Clock / (Timer ticks between edges).
c
void Timer_Init(void) { // Configure timer prescaler and mode TIMER_PRESCALER = 8; // Example prescaler TIMER_MODE = INPUT_CAPTURE; ENABLE_TIMER(); } uint32_t Get_Frequency(uint32_t capture1, uint32_t capture2, uint32_t timer_clock) { uint32_t period_ticks = capture2 - capture1; float period_sec = (float)period_ticks / timer_clock; return (uint32_t)(1.0f / period_sec); }
Example
This example shows how to measure frequency of a signal using a timer input capture on a microcontroller. It captures two consecutive rising edges, calculates the period, and then computes the frequency.
c
#include <stdio.h> #include <stdint.h> // Simulated timer clock frequency in Hz #define TIMER_CLOCK 1000000UL // 1 MHz timer clock // Simulated captured timer values (ticks) uint32_t capture1 = 100000; // First edge at 100000 ticks uint32_t capture2 = 150000; // Second edge at 150000 ticks uint32_t measure_frequency(uint32_t cap1, uint32_t cap2, uint32_t timer_clk) { uint32_t period_ticks = cap2 - cap1; if (period_ticks == 0) return 0; // Avoid division by zero float period_sec = (float)period_ticks / timer_clk; return (uint32_t)(1.0f / period_sec); } int main() { uint32_t freq = measure_frequency(capture1, capture2, TIMER_CLOCK); printf("Measured Frequency: %u Hz\n", freq); return 0; }
Output
Measured Frequency: 20 Hz
Common Pitfalls
- Timer Overflow: If the timer overflows between captures, the period calculation will be wrong. Handle overflow by checking timer max value.
- Incorrect Prescaler: Using wrong prescaler can cause inaccurate frequency.
- Signal Noise: Noisy signals can cause false captures; use filtering if needed.
- Division by Zero: Ensure two captures are not equal to avoid division by zero.
c
/* Wrong: Not handling timer overflow */ uint32_t period = capture2 - capture1; // May be incorrect if overflow /* Right: Handle overflow assuming 16-bit timer */ uint32_t period_correct = (capture2 >= capture1) ? (capture2 - capture1) : (0xFFFF - capture1 + capture2 + 1);
Quick Reference
| Step | Description |
|---|---|
| Configure Timer | Set prescaler and input capture mode |
| Capture Edges | Record timer value on signal edges |
| Calculate Period | Subtract captured timer values, handle overflow |
| Compute Frequency | Frequency = Timer Clock / Period ticks |
Key Takeaways
Use timer input capture to record signal edges for frequency measurement.
Calculate period by subtracting consecutive captured timer values, handling overflow.
Frequency is the inverse of the period calculated from timer ticks and clock.
Choose timer prescaler carefully to match expected signal frequency range.
Avoid division by zero and filter noisy signals for accurate measurement.