Page 1 of 2

How to change PWM frequency or duty cycle.

Posted: Wed Jan 05, 2011 4:12 am
by mculover
Hi Giovanni,

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

Thanks in advance.
mculover

Re: How to change PWM frequency or duty cycle.

Posted: Wed Jan 05, 2011 8:55 am
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

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 5:39 am
by mculover
Hi Giovanni,

Thanks you very much.
:D

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 12:31 pm
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

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 2:00 pm
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

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 2:57 pm
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

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 3:11 pm
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

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 3:27 pm
by mculover
No anything change...
:(

mculover

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 4:05 pm
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 15022 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

Re: How to change PWM frequency or duty cycle.

Posted: Thu Jan 06, 2011 4:34 pm
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