cancel
Showing results for 
Search instead for 
Did you mean: 

4 channel frequency measurement on a single timer

jackygip
Associate II
Posted on July 07, 2015 at 18:18

I've been testing a STM32F4 Discovery since I'm new to ST Microcontrollers and I wanted to use one timer to measure 4 different channel frequencies, as I want to measure more signals than there are timers. I do not care about PWM widths etc. For example, use TIM4 to measure the frequency of different digital signals on the TIM4 channels 1 to 4.

Using STM32CubeMX with the default settings with the following changes to TIM4, quickly managed to get the 4 capture/compare registers to copy the counter value on each active edge. I let the counter count to a value of 0xFFFF (ARR) before it resets to 0x0.

0690X00000605XPQAY.png

Although this is only good for first frequency measurement when the capture/compare registers (CCRx where x is channel) are their default value 0, since the period of the signal is the difference between the final and initial counter values: CCRx - 0

Is it possible to measure frequency using this method after the first active edge, i.e. once the CCRx registers are not their default value for the initial counter value?

On Figure 160 of the reference manual, it looks like it might be possible. I was thinking enabling 

capture

 and 

capture_transfer

 signals at exactly the same time so on each channels active edge, the value of the counter gets transferred to the shadow register and then on the next active edge, transfer into the preload register, like a 2 stage shift register. This shift register action happens continuously. Then when you want the frequency, disable both  

capture

 and 

capture_transfer

 signals of Figure 160 at exactly the same time so they no longer update continuously, and use the MCU to get the preload and shadow register values to calculate the difference in counter values, determining the period. Does that sound logical or possible for this microcontroller?

In all other examples they only measure the frequency of only one channel for a timer, as they reset the counter on each active edge of one channel, then read counter value for the period. 

0690X00000605JSQAY.png

0690X00000605XeQAI.png
2 REPLIES 2
Posted on July 07, 2015 at 18:25

The values in TIMx->CCRx are a latched version of TIMx->CNT at the event. I wouldn't presume default values. You'd qualify if you have a new value by checking the TIM_IT_CCx and clearing it after you've read the channel. You'd take the current measurement (B) and subtract the previous measurement (A) on the channel to get a delta (B-A) of the ticks at the time base clock rate. ie Freq = (TIMxCLK / (Prescaler + 1))/ Delta;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 07, 2015 at 18:29

Each channel functions independently, but there is a single counting element. You should be able to measure four different frequencies provided they fit within the wrapping span of the timer.

32-bit timers would generally be preferable.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..