2015-07-07 09:18 AM
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.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 - 0Is 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 enablingcapture
andcapture_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 bothcapture
andcapture_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.2015-07-07 09:25 AM
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;
2015-07-07 09:29 AM
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.