How to change PWM frequency or duty cycle.
Moderators: RoccoMarco, lbednarz, tfAteba
How to change PWM frequency or duty cycle.
Hi Giovanni,
After config and running PWM.
How to change PWM frequency or duty cycle on ChibiOS/RT ?.
Thanks in advance.
mculover
After config and running PWM.
How to change PWM frequency or duty cycle on ChibiOS/RT ?.
Thanks in advance.
mculover
- 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.
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
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.
Hi,
I try with STM32VL-Discovery demo.
I use PWM4 CH3 and CH4, But PWM can not generate output.
what is wrong ?.
Thanks in advance.
mculover
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
- 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.
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
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.
I change to
But no PWM output !!!
mculover
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
- 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.
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
pwmEnableChannel(&PWMD4, 2, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 5000)); /* 50% */
pwmEnableChannel(&PWMD4, 3, PWM_PERCENTAGE_TO_WIDTH (&PWMD4, 5000)); /* 50% */
Giovanni
- 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.
Hi,
I just tested it on the STM32VL-Discovery, this is the code I used:
The output on PB8 and PB9 is the following:
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
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:
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.
Thanks you for your help. 
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
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