cancel
Showing results for 
Search instead for 
Did you mean: 

Maximum reasonable Frequency for TIM2 Frequency counter measurement?

David Pekin
Senior

Hello,

We're looking at using a STM32F303RE for a project. One of the requirements is being able to measure the frequency of a square wave ranging from 1MHz - 40Mhz. I've been looking at the General-purpose timer cookbook (AN4776) and trying to get the frequency counter sample application working on the F303 platform but have so far been unsuccessful but that's another issue.

However, as I become more familiar with the way that code works, I'm concerned that it won't work at these higher frequencies. if the system clock is 72MHz, trying to count the CC2R clock counts latched at 40Mhz will only give counts of 1 or 2, right? I guess we could build a divisor circuit to condition the input signal down from 40Mhz by a factor of 16 or 32?

Any thoughts on this? Am I missing something?

Thanks

11 REPLIES 11

At high frequencies you generally count the pulses, not measure the period, integrate over a fixed/known time and compute frequency. ie External Count mode

Interrupts reach saturation at relatively low frequencies (few hundred KHz)

Don't use interrupts.

Consider using PWM Input Mode, and reading the CCR1/CCR2 when interested.

The inputs have a 1, 2, 4, and 8 Input Prescaler, ie can measure 8 periods

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

Thank you. Do you know of any example code counting external pulses? It seems that it would be simple to read the system clock tics and then the TIMx count from the external source to the frequency. Obviously you need 2 sequential counts to do the calculation. Anything wrong with this approach? Can you read the timer count register any time without worrying about it being changed in an interrupt?

S.Ma
Principal

It depends on the precision and consumption. Timer clock can be external, prescaled, and by using 32 bit timer, get wide range. Measure the number of edges over 100msec and directly get 0.1 Hz precision

0690X000008ApamQAC.png

> Can you read the timer count register any time without worrying about it being changed in an interrupt?

This is a 32-bit processor and 16- or 32-bit peripheral registers are read in in a single cycle.

JW

Hi.

To measure the frequency of a square wave 1-40 MHZ

1. Need an input. This is the clock source for counter 2 and can be only the ETR for timer 2.( note that max input freq is 36 MHZ )

2 Need a Time Base . This can be internal clock or an external more stable clock source like an OCXO,TCXO. If your intention is to use the internal clock to measure freq, then F_72Mhz, must be converted to a more suitable frequency to do this job. In practice, the period need to measure the frequency is from milliseconds to several seconds.

This Time Base will trigger one of the CCRegisters to capture the current value of Counter(there is no need to read the counter directly)

So, this time base, periodically, and automatically , catch the value of counter and can call an ISR. inside the ISR, subtract the previous catched value from current catched value and by dividing this result with time base period, you have the Frequency you want.

3. To produce the time base (without connect any external wire between pins, but internally ) is little bit tricky. Tim 2 will configure as Slave Trigger Mode and triggered from eg Timer3 (ITR2) or Timer4 (ITR3) as master. (we call it TimerM). TimerM must be configured as Master and Update_Event output to Timer2.

TimerM will have clock input the internal clock, prescaled appropriately to give an Update_Event at a desirable measurement period (eg 1 sec or 10msec).

Automatically, Freq calculation will be done inside ISR every period you prefer (usually bigger than milliseconds)

Don't use the Timer 2 as Slave_Reset_Mode. It is more easy, no need ISR, but introduce inaccuracies at every cycle because the extra delay need to reset and start again the timer.

>>Can you read the timer count register any time without worrying about it being changed in an interrupt?

The TIM can be read at a single edge, in that sense the read is atomic, but the TIM operates independently of the CPU. Two back-to-back reads may be several counts apart.

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

So the concept of simply using the timer to count external pulses, no interrupts involved, and reading both the system clock and current timer count periodically to determine the external pulse frequency is valid. Multiple frequency calculations could be averaged to get better resolution/accuracy of a stable external signal, I guess.

So I don't really care about the time base of the timer, I just want it to increment a counter (up to 40Mhz) that I can read any time the external rising edge pulse occurs. So the clock can be the system clock and the signal can come in on any valid timer channel. The timer can free run and the counter can roll over as needed. I'm not sure on the exact timer setup details to accomplish this however.

Do you know of any sample code that does this or approximately this? It would seem to be a relatively common thing to do.

> So I don't really care about the time base of the timer, I just want it to increment a counter (up to 40Mhz)

The input frequency can't be more than half of the internal timer clock, i.e. 36MHz if you clock it at 72MHz; see my post below.

JW

In response to @S.Ma​  and frequency resolution - not quite. Freq = (pulses per period) / (period in seconds), and measurement resolution is 1/period. WIth a measurement period of 100ms (0.1 seconds), your frequency resolution is 10 Hz. For example, a count of 20 pulses in 0.1 seconds means a frequency of 200 Hz. A count of 21 pulses means a frequency of 210 Hz. If the actual frequency is 205 Hz, the reading will (should) alternate between 20 and 21. In order to get a frequency resolution of 0.1 Hz you need to count pulses for 10 seconds.

To increase the frequency resolution for a given sampling period you need to add something like fractional period measurements where you measure the time from the last (say) rising edge to the end of the sampling period AND you need to measure the actual period of one of the pulses during the sample period. This gives you the fractional period beyond the integer pulse count.