cancel
Showing results for 
Search instead for 
Did you mean: 

How to measure frequency using Timers

Arman Ilmak
Senior
Posted on June 05, 2018 at 22:30

Hello guys.

When I used LPC17xx series in my last experience of ARM MCUs I used 2 timers one for generating interrupt every 1 second and one for counting the rising edges of the input CAP.

So I started reading the Reference menu of the stm32F2xx ,but there were a lot of timer functions described in timer section.I was confused which one to use for my purpose.

Now I know that how to set the timer to work as a TIME BASE,but for input I didn't know which one to use.One section described external clock mode 1 and the other external clock mode 2 and the other was input capture mode .

Which one should I use?

#stm32f2 #cube-hal
11 REPLIES 11
S.Ma
Principal
Posted on June 05, 2018 at 22:50

There are several ways to measure frequency, and it's on case by case...

One simple way is free running timer and measure time between two successive input capture. Optionally an edge prescaler can be used prior to the capture for higher frequency signals.

Another way is to count input signal edges by using it as timer clock during one second.

Posted on June 05, 2018 at 22:55

For higher frequencies use the External Count mode, basically take TIM2_CH1 as the TIM_CLK

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Artur IWANICKI
ST Employee
Posted on June 06, 2018 at 07:04

Hello,

Please have a look at PWM Input mode of timers. It allows you to measure length of 'H' and 'L' time of the input signal and storing them in the separate registers.

You can refer to existing application note

http://www.st.com/content/ccc/resource/technical/document/application_note/group0/91/01/84/3f/7c/67/41/3f/DM00236305/files/DM00236pdf/jcr:content/translations/en.DM00236pdf

or some examples which you can find in

#stm32f2

#cube%20hal

library folder, i.e.

.\STM32Cube_FW_F2_V1.7.0\Projects\STM32F207ZG-Nucleo\Examples\TIM\TIM_PWMInput\

if you prefer to operate on lower abstraction layer libraries, you can have a look at this:

.\STM32Cube_FW_F2_V1.7.0\Projects\STM32F207ZG-Nucleo\Examples_MIX\TIM\TIM_PWMInput\

Mentioned library you can find

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-mcu-packages/stm32cubef2.html

.

Best Regards,

Artur

Arman Ilmak
Senior
Posted on June 07, 2018 at 19:06

I found a way to measure the frequency. But there are some parts that I can't figure out the difference between.

First is that I found that when using capture input mode after a capture occurs the CCR register is loaded with the content f the CNT.So what are external clock mode 1 (with TI input) and external clock mode 2 (with ETR input)?

Posted on June 07, 2018 at 19:13

The external clock makes CNT increment/decrement rather than the APBx clock source.

As I recall the clock routing allows for CH1 (TI1FP1),CH2 (TI2FP2) or ETR as a source

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 07, 2018 at 23:06

Clive One wrote:

The external clock makes CNT increment/decrement rather than the APBx clock source.

You mean it counts the input signals' edge defined by the software?

If it is a clock that used rather than APBx clock source, why it must be synchronized first?

0690X00000604dbQAA.jpg 

As I recall the clock routing allows for CH1 (TI1FP1),CH2 (TI2FP2) or ETR as a source

And when we use ETR and when TI?

Posted on June 07, 2018 at 23:07

Forgot to ask that when using external clock modes what is the usage of TIMx->CCRx?

Posted on June 07, 2018 at 23:30

You still have to access the TIM peripheral registers in the APB clock domain,  so the clock edge that increments the counter is coherent with the APB clock, and you don't get phase issues, and setup/hold violations.

The CCRx function as they did before, but now you're time stamping using a counter from a different clock

ie CCRx := CNT at your Input Capture Events

Or acts as a phase comparator in the Output modes

If you pushed a GPS 1PPS signal in as the external clock the TIMx->CNT would increment every second. Equally you could clock it with a 30.72 MHz or 19.2 MHz source if that's what you want to do.

If you drove a 1PPS into CH2, and 19.2 MHz into CH1, clocked the counter with CH1, and Input Captured on CH2, you'd observe CCR2 increase by 19,200,000 at each event.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 07, 2018 at 23:50

Thank you.

Another question that I have is that when I use input capture to measure the frequency on every capture event the CCR register is loaded with CNT, but the CNT goes on without any reset. If I want to reset it after each capture what should I do? I tried some cube mx to do this but it didn't work.

0690X00000604dWQAQ.jpg