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
| PwmOut | A pulse-width modulation digital output |
| Functions | |
| PwmOut | Create a PwmOut connected to the specified pin |
| write | Set the ouput duty-cycle, specified as a percentage (float) |
| read | Return the current output duty-cycle setting, measured as a percentage (float) |
| period | Set the PWM period, specified in seconds (float) |
| period_ms | Set the PWM period, specified in milli-seconds (int) |
| period_us | Set the PWM period, specified in micro-seconds (int) |
| pulsewidth | Set the PWM pulsewidth, specified in seconds (float) |
| pulsewidth_ms | Set the PWM pulsewidth, specified in milli-seconds (int) |
| pulsewidth_us | Set the PWM pulsewidth, specified in micro-seconds (int) |
| operator= | 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);
}
}
}