source:Interfaces/PwmOut/PwmOut_Interfaces.png

PwmOut

The PwmOut interface is used to control the frequency and mark-space ratio of a digital pulse train.

Hello World!

// Example code for dimming an on-board LED.

#include "mbed.h"

PwmOut led(p21);

int main() {
    while(1) {
        for(float p = 0.0f; p < 1.0f; p += 0.1f) {
            led = p;
            wait(0.1);
        }
    }
}

API

PwmOutA pulse-width modulation digital output
Functions
PwmOutCreate a PwmOut connected to the specified pin
writeSet the ouput duty-cycle, specified as a percentage (float)
readReturn the current output duty-cycle setting, measured as a percentage (float)
periodSet the PWM period, specified in seconds (float)
period_msSet the PWM period, specified in milli-seconds (int)
period_usSet the PWM period, specified in micro-seconds (int)
pulsewidthSet the PWM pulsewidth, specified in seconds (float)
pulsewidth_msSet the PWM pulsewidth, specified in milli-seconds (int)
pulsewidth_usSet the PWM pulsewidth, specified in micro-seconds (int)
operator=A operator shorthand for write()
operator float()An operator shorthand for read()
class PwmOut : public Base
A pulse-width modulation digital output
PwmOut(PinName pin,  
const char *name =  NULL)
Create a PwmOut connected to the specified pin
void write(float value)
Set the ouput duty-cycle, specified as a percentage (float)
float read()
Return the current output duty-cycle setting, measured as a percentage (float)
void period(float seconds)
Set the PWM period, specified in seconds (float)
void period_ms(int ms)
Set the PWM period, specified in milli-seconds (int)
void period_us(int us)
Set the PWM period, specified in micro-seconds (int)
void pulsewidth(float seconds)
Set the PWM pulsewidth, specified in seconds (float)
void pulsewidth_ms(int ms)
Set the PWM pulsewidth, specified in milli-seconds (int)
void pulsewidth_us(int us)
Set the PWM pulsewidth, specified in micro-seconds (int)
PwmOut& operator= (float value)
A operator shorthand for write()
operator float()
An operator shorthand for read()

Details

The PwmOut Interface can be used on mbed pins p21-p26, and also on-board LED1-LED4

The PwmOut interface can express the pulse train in many ways depening on how it is to be used. The period and pulse width can be expressed directly in units of seconds, millisecond or microseconds. The pulsewidth can also be expressed as a percentage of the the period.

Implementation Details

On the mbed LPC2368 and LPC1768, the PwmOut hardware is limited to share the period value between all outputs. Therefore, if you change the period of one output, you change them all. The pulsewidth can be set independently for each output.

Examples

// Control a R/C model servo 

#include "mbed.h"

PwmOut servo(p21);

int main() {
    servo.period(0.020);          // servo requires a 20ms period
    while (1) {
        for(float offset=0.0; offset<0.001; offset+=0.0001) {
            servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
            wait(0.25);
        }
    }
}