cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Measurement Accuracy

Randi
Associate

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.

  • 50 Prescaler -> 51.5Hz; 1960.714uS
  • 75 Presclaer -> 50.667Hz,1973.214uS
  • 100 Prescaler -> 50.49Hz, 1979.76uS
  • 500 Prescaler -> 50.1Hz,19940.476uS
  • 1000 Prescaler -> 50.05Hz, 19880.09uS

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

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

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
Peter BENSCH
ST Employee

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

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Randi
Associate

Thank you very much Peter. Increasing the prescaler by one in my duty and frequency calculation increased the accuracy to nS range.