cancel
Showing results for 
Search instead for 
Did you mean: 

Counting pulses over a certain time period

llmg343
Associate II
Posted on November 30, 2015 at 17:00

Hi,

I am using STM32F1 and want to count the number of times a digital input is high, over a period of 5 seconds. Can anyone please help me with this query? Thanks in advance.
5 REPLIES 5
Posted on November 30, 2015 at 17:53

You'd want to pick a timer, and external pins for either CH1 or CH2, and configure the timer in external counter mode. Set the timebase up for maximal mode, and then use another timer to handle the 5 second periodicity you need.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
llmg343
Associate II
Posted on December 03, 2015 at 11:37

Thanks a lot clive1 for replying. Can you please tell me what you mean by time base in maximal mode? Can you give me an example code of how to count over a 5 second period?

Posted on December 03, 2015 at 13:18

It means that the timer/counter are set to count to there maximum count, be it 16-bit or 32-bit.

The timer is configured to count on ''External Counter'' mode, I've posted dozen of examples to the forum, please use Search/Google.

First hit for ''STM32 External Count Mode''

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentview...

On the first page, an App Note for timers

http://www.st.com/web/en/resource/technical/document/application_note/DM000425pdf

Configure another timer to interrupt/sample on a 5 second period, and delta the starting and ending counts to see the count over the period.

I'm not looking to do your homework/job here.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
llmg343
Associate II
Posted on December 06, 2015 at 13:22

Thanks for your guidance clive.

llmg343
Associate II
Posted on December 10, 2015 at 21:19

Hi clive,

I configured timer2 and interupts to count over a 5 second period. Even  though I am able to count with the interrupts, I am not being able to reset the count after 5 second period. I am also not sure if I have got the period and prescaler values right. Can you please take a look and let me know? Thanks in advance.

    /* Configure the basic Timer TIM2  */

    TIM_TimeBaseStructure.TIM_Period = 5000;                     // Timer overflow value    (overflow after 5s)

  TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1;              // TIMClk/36000= 36MHz/36000= 1kHz

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

     TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_Cmd(TIM2, ENABLE);