0
0
Power-electronicsHow-ToBeginner · 3 min read

Count External Events Using Timer in Embedded C: Simple Guide

To count external events using a timer in Embedded C, configure the timer in external event counting mode by setting the timer input pin and enabling the counter mode. Then, read the timer's count register to get the number of external pulses detected.
📐

Syntax

To count external events using a timer, you typically:

  • Configure the timer input pin to receive external pulses.
  • Set the timer to count mode (external clock source).
  • Enable the timer counter.
  • Read the timer count register to get the event count.
c
/* Example syntax for configuring timer to count external events */
// 1. Configure timer input pin as input
// 2. Set timer clock source to external pin
// 3. Enable timer counter
// 4. Read timer count register

void Timer_Init_ExternalCount(void) {
    // Configure timer input pin as input
    // Set timer clock source to external pin
    // Enable timer counter
}

unsigned int Timer_GetCount(void) {
    return TIMER_COUNT_REGISTER; // Read count register
}
💻

Example

This example shows how to configure a generic 16-bit timer to count external pulses on a microcontroller pin and then read the count.

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

// Simulated hardware registers
volatile uint16_t TIMER_COUNT_REGISTER = 0;
volatile uint8_t TIMER_CONTROL_REGISTER = 0;

#define TIMER_ENABLE (1 << 0)
#define TIMER_EXTERNAL_CLOCK (1 << 1)

// Initialize timer to count external events on input pin
void Timer_Init_ExternalCount(void) {
    // Configure timer control register:
    // Enable timer and select external clock source
    TIMER_CONTROL_REGISTER = TIMER_ENABLE | TIMER_EXTERNAL_CLOCK;
    // Reset count register
    TIMER_COUNT_REGISTER = 0;
}

// Simulate external event (pulse) arriving
void External_Event(void) {
    // Increment count register to simulate pulse count
    TIMER_COUNT_REGISTER++;
}

// Get current count of external events
uint16_t Timer_GetCount(void) {
    return TIMER_COUNT_REGISTER;
}

int main(void) {
    Timer_Init_ExternalCount();

    // Simulate 5 external pulses
    for (int i = 0; i < 5; i++) {
        External_Event();
    }

    // Read and print count
    printf("External events counted: %u\n", Timer_GetCount());
    return 0;
}
Output
External events counted: 5
⚠️

Common Pitfalls

  • Not configuring the timer input pin correctly: The pin must be set as input and connected to the timer's external clock input.
  • Forgetting to enable the timer counter: The timer must be started to count events.
  • Reading the count register too early: Ensure events have occurred before reading.
  • Ignoring timer overflow: For long counting, handle timer overflow or use a wider timer.
c
/* Wrong: Timer not enabled, so count stays zero */
void Timer_Init_Wrong(void) {
    // Missing TIMER_ENABLE bit
    TIMER_CONTROL_REGISTER = TIMER_EXTERNAL_CLOCK;
    TIMER_COUNT_REGISTER = 0;
}

/* Correct: Timer enabled and external clock selected */
void Timer_Init_Correct(void) {
    TIMER_CONTROL_REGISTER = TIMER_ENABLE | TIMER_EXTERNAL_CLOCK;
    TIMER_COUNT_REGISTER = 0;
}
📊

Quick Reference

Steps to count external events using a timer:

  • Set timer input pin as input.
  • Select external clock source for timer.
  • Enable timer counter.
  • Reset count register before counting.
  • Read count register to get event count.
  • Handle timer overflow if needed.

Key Takeaways

Configure the timer to use an external clock source to count pulses from an input pin.
Always enable the timer counter to start counting external events.
Read the timer count register to get the number of external events detected.
Ensure the input pin is properly configured as input and connected to the timer.
Handle timer overflow for long event counting to avoid incorrect counts.