Pwm term and definition

13 Nov 2010

Hi all,

Just help me confirm some definitions in PwmOut library

DutyCycle ?

pulsewidth ?

Period?

From my understand, dutycycle = pulsewidth/period ? is it correct ?

I write a test to generate a frequency with dutycycle = 50% with PwmOut connect to LED1 but seem it doesnot work.

#include "mbed.h"

PwmOut led(LED1);

int main() {
while(1)
{
led.pulsewidth(0.5f);
led.period(1.0f);

}
}

13 Nov 2010

Hi LFY,

Your understanding is correct. The program just has a couple of problems.

  • You should set the period first in this case (the pulsewidth can never be bigger than the period, and as you haven't set it yet, it'll just be the default of 0.02).
  • Because you are in a while loop, you keep reseting the pwm settings. Given it will go around this loop thousands and thousands of times a second, and your period is 1 second, you'll never see anything! It'll always be just starting out.

So for example, you could do the following:

#include "mbed.h"

PwmOut led(LED1);

int main() {
    led.period(1.0f);
    led.pulsewidth(0.5f);
}
and that would give you a 1Hz flash. Note that PwmOut is generally used for frequencies in this range and faster.

Simon

13 Nov 2010

Thank you Simon !

thanks for your quick response and clearly explain.

It works  now !

16 Nov 2010

Hi ,

What is the default frequency of PwmOut ?

I dont have a scope to check here .

16 Nov 2010

Hi LFY,

The default frequency (period) of the PwmOut is 0.020s (i.e. 20ms), as used by servos for position control and suitable for brightness and/or speed control. The default pulsewidth is 0.

Simon