PWM signal not accurate

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
oszfather
Posts: 11
Joined: Tue Jun 14, 2022 5:27 pm
Has thanked: 2 times

PWM signal not accurate

Postby oszfather » Tue Jun 28, 2022 12:22 pm

I've configured the PWM driver with the following parameters using the config struct:

Code: Select all

static PWMConfig oneshot125cfg = {
  10000,                                    /* 10kHz PWM clock frequency.   */
  20,                                         /* Prescalar ( /20 )     */
  pwmpcb,
  {
   {PWM_OUTPUT_ACTIVE_HIGH, pwmc1cb},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
  0,
  0
};


This give me a PWM frequency of 500Hz (2ms period). When i use a logic analyzer however I'm reading a frequency of 505Hz. The real issue though is when I'm trying to control the duty cycle. The output is only accurate for increments of 5 e.g. 10%, 15%, 20%, 25% and so on. Trying to set a duty cycle that isn't a multiple of 5 e.g. 43%, 87%, 52% etc. just rounds down to the nearest 5 multiple. E.g. setting 57% duty cycle, i measure 55% as output.

Code: Select all

 pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 9500));


Image

Why is this and how do I get a better resolution?

edit: When raising PWM clock frequency, the accuracy drops more. 57% is now measuring as 53%. Whats happening here?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: PWM signal not accurate

Postby Giovanni » Tue Jun 28, 2022 12:33 pm

Hi,

You need to consider that the PWM driver is mapped over a real TIM unit, the values you specify need to be compatible with the current TIM settings like:
- Clock frequency.
- ARR register value (the ARR value is your 100%).

If your clock/period causes ARR to be something like "17" then you should not expect it to be able to accurately allow all your %. Select your TIM input frequency in a way that ARR is kept to an high value for your desired period, 10000 for example, that will allow for accurate percentages.

For example, if you want a PWM frequency of 1000Hz then select an input frequency like 10MHz, that will allow for high accuracy.

The driver does no magic, the STM32 manual is your best friend when selecting operating conditions.

Giovanni


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 19 guests