cancel
Showing results for
Did you mean:

# Measure PWM Input Frequency from 0.016 Hz to 250 Hz

Associate III

Greetings Everybody,

I want to ready PWM Input Frequency from 0.016 Hz to 250 Hz. I tried a bit with PWM Input Capture and i can read up to 11 Hz . Is it possible to ready frequencies lower than this ?

May be i might be doing some thing wrong in setting the timer clock. I have used Timer 2 for this purpose..

Any further guidance will be of great help..

Thanks.

11 REPLIES 11
Guru

Use the prescaler.

JW

Associate III

Ok...

I tried but after recuding the clock frequency to the max i am not able to get prescaler value near to 16-bits value... I am using ST Disco0. I have set APB1 to 0.011719 MHz and min frequency i can go is 0.179 Hz.. So i think this approach is not viable.

Senior

I would just use an exti interrupt. When the interrupt comes, you note the time. If you run a 32-bit timer at 1MHz it will overflow after about an hour. Plenty.

So then you have timestamps with microsecond resolution. At 250Hz = 4ms you get a 1:4000 resolution, with the 1 minute frequency much better. Your crystal clocksource is easily the biggest contributor to errors in that range.

Tips:

* In the interrupt, FIRST get the timestamp. Then do the math, calculate where to store it etc etc.

* To work with these timestamps, calculate the difference between two timestamps and store that in an integer variable. Now the wraparound does not hurt.

* After doing the preliminary math like above, you can consider running the timer at 48 or 24MHz (those are options on the STMs that I use, I don't know about yours). I would recommend having the timer not overflow before 2 minutes because then you start to have to be careful about the overflows.

Principal

You can emulate a 64 or 128 bit timer by software:

``uint32_t TIM_MSB_64_32 = 0;``

When it overflows, it causes the interrupt to increment TIM_MSB_64_32

When you input capture from the timer the 32 bit (bit 0..32), capture this value and store it as concatenated 64_32 and 32_0

If you substract with previous 64 bit capture, you will get the period as you wish, without resolution compromize.

The HW relaxes the SW constrains and timings, then the SW can emulate extra timer bits...

>> I am using ST Disco0

Yeah, I've no idea what that is.

The Input Prescaler can allow multiple cycles to be measured.

TIM2 is 32-bit on most recent M4/M7 parts.

As others have mentioned there is EXTI, even on the old F103 parts you could use the 32-bit DWT CYCNT to measure close to a minute duration at 72 MHz. Time stamp the edges, delta the time measurements.

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

One of the failings in the STM32 design is the awful TIM. Should have just build some simple 32-bit counters from the outset, for encoders or whatever, rather than kitchen sink the design.

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

Even though he didn't say it explicitly, I'm guessing he has an STM32F0xx, because of the 0 in "Disco0". Might be L0 or G0 or something like that, but most probably not an M4 or M7.

That said, on the STM32F072 that I normally use and the STM32F051 that's on hte STM32F0 Discoveryboard both have a thirtytwo bit TIM2 as well.

Associate III

Well its Discovery 0 board with STM32F051..

Associate III

Well @S.Ma​  i like your approach... i will give it a try.