PwmOut duty cycle changes after setting period?

17 Feb 2010 . Edited: 17 Feb 2010

Hi guys,

I've got a question. This program kept me wondering why I see two different duty cycles after setting the PWM period.

//Fiddling with the mbed PWM


#include "mbed.h"
PwmOut led(LED1);
Serial pc(USBTX, USBRX); // tx, rx
int main() {
    pc.printf("testing...\n"); //testing serial port
    led.write(0.5f); //output duty cycle percentage (float)
    pc.printf("LED duty cycle is %f\n", led.read());
    led.period(1.0f); //PWM period in seconds (float)
    pc.printf("LED duty cycle is %f\n\n", led.read());
}

Output:

testing...
LED duty cycle is 0.500000
LED duty cycle is 0.010000

Am I missing something? Why doesn't the duty cycle stay the same?

 

Off topic: another handy terminal application is Terminal by Bray. Worth including in the Handbook?
Link

Screenshot:

Screenshot Bray Terminal

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Thanks!

18 Feb 2010

Might be related to this issue.

18 Feb 2010

Hi Martijn,

This behaviour is simply how it is implemented; the pulsewidth remains constant, rather than the dutycycle.

However, I think the behaviour you are expecting is actually more desirable/natural, so I think we should perhaps revise the specification and implementation to:

  • Change pulsewidth -> period remains constant (and hence duty cycle changes)
  • Change period -> dutycyle remains constant (and hence pulsewidth changes)
  • Change dutycycle -> pulsewidth changes (and hence period remains constant)

Does that sound more appropriate? If so, i'll put that on our list for the next release.

Thanks for the Terminal hints. I've added your suggestion and also Putty to:

Thanks,
Simon

18 Feb 2010

Hi Simon,

Thanks for the explanation! It made me scratch my head in bewilderment I must admit.
Yes the proposed behaviour sounds more appropriate. I can't offer a rational explanation right now, frequency/period just has a more 'solid' feel to it.

And Igor, thanks for thinking with me!

Regards, Martijn