0
0
Power-electronicsHow-ToBeginner · 3 min read

Measure Time Between Events Using Timer in Embedded C

To measure time between events in embedded C, use a hardware timer configured to count clock ticks. Start the timer at the first event, read the timer value at the second event, and calculate the elapsed time by converting ticks to time units using the timer's clock frequency.
📐

Syntax

To measure time between events, you typically use a hardware timer with these steps:

  • Timer_Init(): Configure the timer's clock source and mode.
  • Timer_Start(): Begin counting ticks at the first event.
  • Timer_Read(): Read the current timer count at the second event.
  • Timer_Stop(): Stop the timer if needed.
  • Calculate elapsed time = (timer count difference) / (timer frequency).
c
void Timer_Init(void);
void Timer_Start(void);
unsigned int Timer_Read(void);
void Timer_Stop(void);
💻

Example

This example shows how to measure time between two button presses using a simple timer that counts milliseconds.

c
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

volatile uint32_t timer_count = 0;
bool event1_occurred = false;
bool event2_occurred = false;

// Simulate timer interrupt handler incrementing every 1 ms
void Timer_ISR(void) {
    timer_count++;
}

// Simulate starting the timer
void Timer_Start(void) {
    timer_count = 0;
}

// Simulate reading the timer count
uint32_t Timer_Read(void) {
    return timer_count;
}

int main(void) {
    uint32_t start_time = 0;
    uint32_t end_time = 0;

    // Event 1 occurs
    Timer_Start();
    event1_occurred = true;

    // Simulate time passing with timer interrupts
    for (int i = 0; i < 1500; i++) {
        Timer_ISR(); // 1500 ms pass
    }

    // Event 2 occurs
    event2_occurred = true;

    if (event1_occurred && event2_occurred) {
        start_time = 0; // Timer started at 0
        end_time = Timer_Read();
        printf("Time between events: %u ms\n", end_time - start_time);
    }

    return 0;
}
Output
Time between events: 1500 ms
⚠️

Common Pitfalls

  • Not resetting the timer: Always reset or start the timer at the first event to avoid incorrect elapsed time.
  • Timer overflow: If the timer count exceeds its max value, the measurement will be wrong. Use a timer with sufficient range or handle overflow.
  • Incorrect clock frequency: Use the correct timer clock frequency to convert ticks to real time.
  • Interrupts disabled: Timer increments often rely on interrupts; disabling them can stop the timer.
c
/* Wrong: Not resetting timer before event 1 */
Timer_Start();
// some delay
uint32_t time1 = Timer_Read();
// event 1 occurs here, but timer not reset
// event 2 occurs later
uint32_t time2 = Timer_Read();
uint32_t elapsed = time2 - time1; // May be incorrect

/* Correct: Reset timer at event 1 */
Timer_Start(); // reset and start
// event 1 occurs
// delay
uint32_t elapsed_correct = Timer_Read(); // elapsed time since event 1
📊

Quick Reference

Tips for measuring time between events using timers in embedded C:

  • Initialize and configure the timer before use.
  • Reset or start the timer exactly at the first event.
  • Read the timer count at the second event.
  • Convert timer ticks to time using the timer clock frequency.
  • Handle timer overflow if measurement duration is long.

Key Takeaways

Always start or reset the timer at the first event to get accurate timing.
Convert timer counts to real time using the timer's clock frequency.
Watch out for timer overflow and handle it properly.
Ensure timer interrupts are enabled so the timer increments correctly.
Use a timer with sufficient resolution and range for your measurement.