cancel
Showing results for 
Search instead for 
Did you mean: 

Counting pulses with STM32G474

HaraldAnt
Associate

Hi,

 

In my STM32G474 project, I'm receiving a signal consisting of a burst of pulses, with a relatively long idle period before the next burst. I'm trying to setup a system where I use timers to count the pulses, and a slave timer to detect the idle period. How is the best way to do this?

In my setup I'm using TIM2 in ETR2 mode, and using this, I'm able to count the pulses coming in by setting regular counter mode. My plan was to setup TIM4 to count independently with a timeout of say 40mSec. This timer is then set to receive resets from TIM2 whenever there is a counter update.

This don't seem to work.

My TIM4 is using internal clock and trigger source is ITR1, and slave mode is set to reset mode.

Any tips and suggestions?

Best regards

2 REPLIES 2

> This timer [TIM4] is then set to receive resets from TIM2 whenever there is a counter update.

You probably assume that Update event means "counter update" (= increment). It's not the case; Update means overflow (or underflow if the counter is set to downcount).

I don't know what exactly do you mean by "TIM2 in ETR2 mode" and "count the pulses coming in by setting regular counter mode". Preferrably, describe your setup in terms of TIM registers/bits/bitfields. I guess you have the external signal connected to TIM2_ETR pin and you use the External clock mode 2 (i.e. TIM2_SMCR.ECE=1).

I'm not sure TIM2_ETR could used as clock source in TIM2 and simultaneously be redirected through TRGO-TRGI to TIM4; but you can try to swap the roles of TIM2 and TIM4: set TIM2 to be the timeout timer, i.e. run it from the internal clock, set TIM2_SMCR.TS=0b111 so that TIM2_ETR is source of Trigger to the Slave-mode controller, and set the Slave mode controller to Reset i.e. TIM2_SMCR.SMS=0b100. Then set TIM2_CR2.MMS=0b000 (which is the reset value so you don't need to set anything) so that Reset is source of TRGO. In TIM4, set TIM4_SMCR.TS=0b001 to select TIM2's TRGO as its TRGI, and set TIM4_SMCR.SMS=0b111 to set it to External counter mode 1, and then enable TIM4's counter by setting TIM4_CR1.CEN.

JW

 

danielbathtub
Associate III

Hi, 

Have you investigated the following method for your application. 

In my view it is better to use one Timer(Like Timer 2) as a Trigger input into another Timer.

To use Timer 2 in PWM Input Capture mode, can do the period measurement between the pulses, the rising edge of the pulses which will serve as a Trigger input into Timer 2 and update the counter much like the Sketch below which is in the reference manual. 

danielbathtub_1-1741337882223.png

 

danielbathtub_2-1741337994206.png

The red I highlighted will count your pulses and those red events is when the counter is RESET. 

So the reset counter can serve as a trigger input into another timer. The yellow is the time between your pulses. You can also set this up with DMA, and this will then DMA the value of your Counter at an IC1 Capture Period Measurement. 

 

I think this is easier.