How to change PWM frequency or duty cycle.

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

How to change PWM frequency or duty cycle.

Post by mculover »

Hi Giovanni,

After config and running PWM.
How to change PWM frequency or duty cycle on ChibiOS/RT ?.

Thanks in advance.
mculover
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to change PWM frequency or duty cycle.

Post by Giovanni »

Hi,

The duty cycle can be changed on the fly using pwmEnableChannel(), you can see an example of this in the STM32VL-Discovery demo, the frequency change requires disabling channels using pwmDisableChannel(), invoking pwmStart() using a different configuration and re-enabling channels with the proper duty cycle (it has to be recalculated after a frequency change).

Giovanni
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: How to change PWM frequency or duty cycle.

Post by mculover »

Hi Giovanni,

Thanks you very much.
:D
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: How to change PWM frequency or duty cycle.

Post by mculover »

Hi,

I try with STM32VL-Discovery demo.
I use PWM4 CH3 and CH4, But PWM can not generate output.

Code: Select all

static PWMConfig pwm4cfg = {
NULL,
{
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* PWM4, CH3 */
{PWM_OUTPUT_ACTIVE_HIGH, NULL}  /* PWM4, CH4 */
},
PWM_COMPUTE_PSC(STM32_TIMCLK1, 10000),    /* 10KHz PWM clock frequency.   */
PWM_COMPUTE_ARR(10000, 1000000000),       /* PWM period 0.1S (in nS).       */
0};

  palSetPadMode(IOPORT2, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  palSetPadMode(IOPORT2, 9, PAL_MODE_STM32_ALTERNATE_PUSHPULL);

  pwmStart(&PWMD4, &pwm4cfg);
  pwmEnableChannel(&PWMD4, 2, 4999); /* 50% */
  pwmEnableChannel(&PWMD4, 3, 4999); /* 50% */


what is wrong ?.

Thanks in advance.
mculover
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to change PWM frequency or duty cycle.

Post by Giovanni »

Hi,

Probably it is because you specified 4999 as parameter for pwmEnableChannel, that parameter does not represent the percentage but the pulse width expressed as clock pulses number, if you want to specify a percentage then you should use the macro PWM_PERCENTAGE_TO_WIDTH() to converts from percentage to the proper width value.

Giovanni
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: How to change PWM frequency or duty cycle.

Post by mculover »

I change to

Code: Select all

  pwmEnableChannel(&PWMD4, 2, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 50)); /* 50% */
  pwmEnableChannel(&PWMD4, 3, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 50)); /* 50% */


But no PWM output !!!

mculover
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to change PWM frequency or duty cycle.

Post by Giovanni »

50 is equivalent to 0.05% that is probably rounded to 0 in the conversion. The range is 0...10000 so it should be:

pwmEnableChannel(&PWMD4, 2, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 5000)); /* 50% */
pwmEnableChannel(&PWMD4, 3, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 5000)); /* 50% */

Giovanni
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: How to change PWM frequency or duty cycle.

Post by mculover »

No anything change...
:(

mculover
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: How to change PWM frequency or duty cycle.

Post by Giovanni »

Hi,

I just tested it on the STM32VL-Discovery, this is the code I used:

Code: Select all

static PWMConfig pwm4cfg = {
  NULL,
  {
  {PWM_OUTPUT_DISABLED, NULL},
  {PWM_OUTPUT_DISABLED, NULL},
  {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* PWM4, CH3 */
  {PWM_OUTPUT_ACTIVE_HIGH, NULL}  /* PWM4, CH4 */
  },
  PWM_COMPUTE_PSC(STM32_TIMCLK1, 10000),    /* 10KHz PWM clock frequency.   */
  PWM_COMPUTE_ARR(10000, 1000000000),       /* PWM period 0.1S (in nS).       */
  0
};
...
  palSetPadMode(GPIOB, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  palSetPadMode(GPIOB, 9, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  pwmStart(&PWMD4, &pwm4cfg);
  pwmEnableChannel(&PWMD4, 2, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 3333));
  pwmEnableChannel(&PWMD4, 3, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 6666));


The output on PB8 and PB9 is the following:
pwm.jpg
pwm.jpg (9.25 KiB) Viewed 15018 times


May be you are measuring it in the wrong way. Note that the period is 1 second not 0.1 second as you wrote in the code.

Giovanni
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: How to change PWM frequency or duty cycle.

Post by mculover »

Thanks you for your help. :D

Sorry for unclear post before.

I use STM32 Discovery example code (PWM section only) with STM32F103RBT6 / Yagarto GCC 4.5.1 / Eclipse and Chibi RTOS 2.0.8.
I set mcuconf.h / halconf.h and board.h relation with my hardware.
I test with other board (same MCU), But same result.

mculover
Post Reply