2019-08-13 10:45 PM
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.
2019-08-13 11:05 PM
Use the prescaler.
JW
2019-08-13 11:56 PM
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.
2019-08-14 12:50 AM
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.
2019-08-14 08:49 AM
You can emulate a 64 or 128 bit timer by software:
uint32_t TIM_MSB_64_32 = 0;
Run your timer so that:
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...
2019-08-14 09:14 AM
>> 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.
2019-08-14 09:17 AM
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.
2019-08-14 09:36 AM
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.
2019-08-15 03:37 AM
Well its Discovery 0 board with STM32F051..
2019-08-15 03:39 AM
Well @S.Ma i like your approach... i will give it a try.