Skip to main content
Arman Ilmak
Senior
June 5, 2018
Question

How to measure frequency using Timers

  • June 5, 2018
  • 5 replies
  • 9571 views
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
This topic has been closed for replies.

5 replies

S.Ma
Principal
June 5, 2018
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.

Tesla DeLorean
Guru
June 5, 2018
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 VenmoUp vote any posts that you find helpful, it shows what's working..
ST Employee
June 6, 2018
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
June 7, 2018
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)?

Tesla DeLorean
Guru
June 7, 2018
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 VenmoUp vote any posts that you find helpful, it shows what's working..
Arman Ilmak
Senior
June 7, 2018
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?

henry.dick
Associate II
June 8, 2018
Posted on June 08, 2018 at 02:22

'

Which one should I use?'

it depends on what you are trying to do.

The basic form of measuring frequency is to compare / gate two pulse trains, one of unknown frequency, and another of known frequency. 

1) the typical way is to use the known pulse train to generate a time base that turns on / off the counting of the unknown pulse train; this works for high frequency signal;

2) it can also be done the other way around: using the unknown pulse train to gate the known pulse train -> aka a reciprocal counter; it works for lower frequency measurement.

3) for cases where you don't need super high precision and the input pulse train isn't too fast / slow, you can use input capture to measure the frequency as well.

there are other ways too. all depends on what kind of signal you are trying to measure.