81. High-Frequency Pulses Counter

Problem Understanding

We need to:

  • Count incoming pulses up to a maximum of 8 MHz frequency.
  • Display the pulse count on a serial terminal (e.g., PuTTY) only when a push button is pressed.
  • Reset the count to 0 after displaying.
  • Pulse generation can be from any source (function generator, another MCU, etc.).

Key Technical Challenges

  • High-frequency handling (8 MHz is far beyond CPU polling capability).
  • Count accuracy without missing pulses.
  • Debouncing the push button press detection without affecting pulse counting.
  • Synchronizing high-speed counter reads with low-speed UART printing.

Recommended Approach

We use:

  • Any MCU with a high-speed hardware timer configured in External Clock Mode, directly clocked by the pulse signal — this ensures counting at hardware speed without CPU intervention.
  • GPIO for push button press detection.
  • UART for sending the count to the serial terminal.
     

Why External Clock Mode?
Because it allows the timer’s counter to increment on each incoming pulse directly from the pin’s input capture hardware, bypassing CPU limitations.

System Architecture

ComponentFunction
TIMx (32-bit or overflow logic)Counts pulses at hardware speed up to 8 MHz
Push ButtonTrigger data read and reset count
UARTSends pulse count to serial terminal
EXTIDetects push button press
Software ISRReads count, sends via UART, clears counter

Implementation Steps

Step 1 — Configure Timer in External Clock Mode

  • Select any high-speed capable timer.
  • Set Clock Source = External Trigger.
  • Edge detection = Rising edge (or falling, depending on signal).
  • No prescaler (PSC = 0).
  • Maximum Count Value = 0xFFFFFFFF (max for 32-bit count).

Effect: Each incoming pulse increments TIM->CNT in hardware at up to 8 MHz.


Step 2 — Configure Push Button Input

  • Set the button pin as a GPIO Input with a pull-up.
  • Add software debounce (~50 ms delay or state check).
     

Step 3 — Configure UART

  • Baud rate: e.g., 115200.
  • Send data in printf style for readability on a serial terminal.
     

Step 4 — Main Code Flow

  • Start the hardware timer counter.
  • Poll for button press detection.

Advantages of This Approach

  • No CPU overhead for counting — hardware timer handles 8 MHz easily.
  • High accuracy — no missed pulses.
  • Scalable — can handle even higher frequencies depending on timer input specs.
     

So, by considering the above points, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!