Skip to main content
Visitor II
July 24, 2025
Question

Measuring frequency - TIM2 value wrong

  • July 24, 2025
  • 2 replies
  • 2177 views

Hello, 

I have a project to measure the frequency (10Mhz) on an PA5 input and PWM output on PA6, with the counting triggered on PA0 (1PPS), CPU STM32F411, my problem is that the value TIM2 is always wrong, I must have around 10Mhz, I think that the configuration of my Timer 2 is not good but I don't see where the problem is.

File attached.

Best regards.

Sébastien..

    This topic has been closed for replies.

    2 replies

    Super User
    July 24, 2025

    > my problem is that the value TIM2 is always wrong,

    How wrong?

    JW

    Super User
    July 24, 2025

    Use input capture to measure the frequency of an incoming signal of high frequency. No chance of doing it accurately by setting a flag and then reading the timer's counter.

    PSEBA.1Author
    Visitor II
    July 25, 2025

    Hello,

    Thanks for the reply. I'm new to STM32 and learning capture mode.

    Is it possible to check my code using capture mode because it still isn't working?

    Pwm no variation.

    new File attached.

    Best regards.

    Sébastien.

    Super User
    July 25, 2025

    You're still relying on interrupts on every edge in order to capture. The chip simply can't interrupt at 10 MHz, it doesn't have the computational speed to do that.

    Use DMA, put the captured IC value into an array and, after you receive at least 2 samples, post-process that array to find the frequency. Don't use circular DMA since the data will be overwritten before you check it.