cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Timer External Pulse counting

piotrek700
Associate
Posted on May 18, 2013 at 00:23

Since 4 day I'm trying write code which will be able to count external pulses. I tried to use encoder mode but i supposed that this work only with 2 quadrature encoders. I have only one pulse signal and i have no idea how to do it. Could you help me or show examples how to count external pulses using timer?

#counter #timer #off-topic #pulce
35 REPLIES 35
Posted on October 26, 2015 at 15:59

I'd probably put one timer in External Count mode, and use a second one to DMA sample the TIMx->CNT with the 10ms periodicity, have a 200 sample buffer with HT/TC interrupts, process 100 elements by doing a delta computation on one vs the next capture and build an output list. This would decimate processor activity to once a second.

Have a foreground task take the processed data and have it write it to you FatFs implementation using an SD Card interfacing with SDIO or SPI depending on what's available on your hardware. You'd want to flush or close periodically. It would be somewhat similar to the ADC to SD Card example I posted for the STM32F4 Disco.

How many samples would be on a comma delimited line? Assume you can't do so endlessly. You're just printing ASCII data to a file, you could format it any way you think is suitable, and that you can process.

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

Thank you for your guidance. External input(~10Hz) is connected to 32bit TIM5_Ch1. TIM5 uses systemcore clock 32MHz in STM32L1. Can I prescale it down for power saving. If a timer uses system clock, it may not work in LP sleep mode, am I right?

For L1 HD devices, RTC wakeup interrupt can be configured from 122us to 32s using highest prescaler 16, hence, I thought to use RTC for 10ms iteruupt.

I need to count pulses for 2 min. Need to store it in a format for further MATLAB processing. I found your ADC_SDIO example here: https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Problem%20with%20ADC%20DMA%20and%20timer&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=3...

Posted on October 27, 2015 at 16:04

I'm mainly using F2/F4 device, you'll need to work out the specifics for your L1 implementation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
patrob680
Associate II
yuanchen
Associate II
Posted on November 17, 2015 at 22:19

Hello Clive1,

I'm also trying to count external pulses using the Timer peripheral. I'm confused between using different clock selections.  From my understanding, TIM_TIxExternalClockConfig() is used when I'm using a channel like TIM3_CHAN1/2 and  TIM_ETRConfig() is used for TIM3_ETR pin. Other than that, the rest of the implementation is the same. Is my understanding correct? 

Also I have this scenario: I'm using TIM4 to generate a PWM output at a 1Hz frequency (which is working properly). Wire a TIM3_ETR pin to my TIM4 PWM output pin. I then use TIM_ETRConfig(TIM3,...) to set up the external trigger. For timer3, I call SelectInputTrigger(TIM3,???) and SelectSlaveMode(TIM3, TIM_SlaveMode_Reset). In this case, TIM3 will clock only on a rising/falling edge of TIM4 PWM, and TIM3->CNT stores the number of pwm pulses. Am I on the right track? and what parameter should I use for ???

Thank you in advance.

Posted on November 17, 2015 at 22:43

Well TI1 and TI2 are filtered, down wind, versions of CH1 and CH2, you can also use TI1ED which is the edged version of CH1. The filter is controlled by the rate of ClockDivision specified in the Time Base settings.

Depending on the ITR wiring you may be able to pull the external clock from an internal node, via TIM_ITRxExternalClockConfig().

You may want to looks at TIM_ETRClockMode1Config(), and TIM_ETRClockMode2Config(), as they also use TIM_ETRConfig(). Can't say I've played with those myself.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..