Multiple PWM frequencies?

16 Oct 2009 . Edited: 16 Oct 2009

Hi,

I would like to use two PWM outputs with different frequencies. I tried the code below.

#include "mbed.h"

PwmOut led1(LED1);
PwmOut led2(LED2);

int main() {
float p1;

led1.period(4);

led1 = 0.5f;

led2.period(2);

led2 = 0.5f;

}

I expected to see two LED flashing in two different frequencies, but the result showed LED1 always on and LED2 flashing with the period of 2 seconds.

Any comment?

Best regards,

Paul

16 Oct 2009

Hi Paul,

Paul Kang wrote:
I would like to use two PWM outputs with different frequencies.

Me too! Unfortunately, a hardware limitation means the period setting is shared by all the PwmOut outputs (the pulsewidth can be changed independently however). I've fed this back to NXP, so they are aware of the constraint this places.

I've also added an implementation note to http://mbed.org/handbook/PwmOut

The pwm is still quite useful for brightness, speed and position control, but just be aware of this limitation for the frequency to be the same for all of them.

It may be worth building a "software pwm", but these will generally suffer from jitter. I assume the speeds you show in your example (seconds) were more for demonstration than a real application?

Simon

16 Oct 2009

Simon,

I assume the speeds you show in your example (seconds) were more for demonstration than a real application?

Yes, you are right. 4s and 2s just are used for me to monitor the LEDs. The jitter of "software PWM" is difficult to handle. Maybe need to consider the external PWM chips, any recommendation?

Regards,

Paul