cancel
Showing results for 
Search instead for 
Did you mean: 

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

NChal.1
Associate II

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!

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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".

View solution in original post

5 REPLIES 5
S.Ma
Principal

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

berendi
Principal

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
Associate II

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
Guru

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
Associate II

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