2022-10-09 01:23 PM
Hi,
I am trying to measure the duty cycle and the frequency of a standard servo PWM signal (50Hz and 5% to 10% duty cycle) using STM32F446. I have configured the timers to be in PWM input mode, and I can read both the frequency and duty cycle. The measurement is very accurate with the 32 bit timers (TIM2 and TIM5) down to sub uS for pulse length. However, with the 16 bit timers the frequency and the duty cycle measurement is very inaccurate. I believe this is due to the prescaler I used. I had to use a prescaler greater than 50 with the 16 bit timers to avoid overflow. However with the 32 bit timer I used a prescaler of 0. Interestingly, it is also very inaccurate when I used the same prescaler on the 32bit that I had to use for the 16 bit timer. Below are some tests I did to measure 50Hz and 2000uS pulse length, the clock frequency to the timer is 84MHz.
Interestingly, a higher prescaler increases accuracy to a point. I thought a lower prescaler would increase the accuracy as it is a smaller step size. Is the accuracy a limitation of the STM32? Is there any way I can make the measurements more accurate?
Thank you in advance for any help.
Solved! Go to Solution.
2022-10-10 12:01 AM
Welcome, @Randi, to the community!
You should note that the value for the prescaler is mathematically greater by one, i.e. PRE=0 corresponds to a divisor of 1, PRE=50 corresponds to 51.
Your measurement results fit quite well with the error caused by this: PRE=50 results in an error of 50/51= 2%, which corresponds to a measurement value of 1960.78 at 2000µs setpoint (assuming the 2000µs are exact).
Does it answer your question?
Regards
/Peter
2022-10-10 12:01 AM
Welcome, @Randi, to the community!
You should note that the value for the prescaler is mathematically greater by one, i.e. PRE=0 corresponds to a divisor of 1, PRE=50 corresponds to 51.
Your measurement results fit quite well with the error caused by this: PRE=50 results in an error of 50/51= 2%, which corresponds to a measurement value of 1960.78 at 2000µs setpoint (assuming the 2000µs are exact).
Does it answer your question?
Regards
/Peter
2022-10-10 01:08 PM
Thank you very much Peter. Increasing the prescaler by one in my duty and frequency calculation increased the accuracy to nS range.