Skip to main content
NChal.1
Associate
July 6, 2020
Solved

I need to be able to count pulses that are incoming on a GPIO. What is the best way to do this? Timers?

  • July 6, 2020
  • 5 replies
  • 3592 views

I have an application where I need to count the total number of pulses coming in from a fuel flow sensor. I then take the total number of pulses to display a readout of the total amount of fuel that has flowed through the sensor since the counter began. What would be the best way to do this? Timer? Regular EXTI?

Thanks!

    This topic has been closed for replies.
    Best answer by TDK

    Looks fine to me. Make sure your period is set to max, if that's what you want.

    Proper way to read the timer counter is to read the TIMx->CNT register. TIM2->CNT in this instance. You can read this directly or use the __HAL_TIM_GET_COUNTER macro.

    5 replies

    S.Ma
    Principal
    July 6, 2020

    how about a tmer which clock input is the sensor's? no interrupts required.

    berendi
    Principal
    July 6, 2020

    Check out timer external clock modes in the reference manual. Use a 32 bit timer or handle overflows carefully if there going to be more than 65535 pulses.​

    NChal.1
    NChal.1Author
    Associate
    July 6, 2020

    Thanks for the help. I am trying to use the 32 bit timer 2 for my application. In the ioc application, this is the settings that I am using. Does this look alright? Also, what is the proper way to read the counter of the timer when I am in the main function? 0693W000001sGJaQAM.png

    TDK
    TDKBest answer
    July 6, 2020

    Looks fine to me. Make sure your period is set to max, if that's what you want.

    Proper way to read the timer counter is to read the TIMx->CNT register. TIM2->CNT in this instance. You can read this directly or use the __HAL_TIM_GET_COUNTER macro.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    NChal.1
    NChal.1Author
    Associate
    July 7, 2020

    I got the timer working the way that I wanted. Thanks for the help!!!