PwmOut
Use the PwmOut interface to control the frequency and duty cycle of a PWM signal.
Tips:
- Set the cycle time first, and then set the duty cycle using either a relative time period via the write() function or an absolute time period using the pulsewidth() function.
- The default period is 0.020s, and the default pulse width is 0.
PwmOut class reference
Public Member Functions | |
PwmOut (PinName pin) | |
Create a PwmOut connected to the specified pin. More... | |
PwmOut (const PinMap &pinmap) | |
Create a PwmOut connected to the specified pin. More... | |
void | write (float value) |
Set the output duty-cycle, specified as a percentage (float) More... | |
float | read () |
Return the current output duty-cycle setting, measured as a percentage (float) More... | |
void | period (float seconds) |
Set the PWM period, specified in seconds (float), keeping the duty cycle the same. More... | |
void | period_ms (int ms) |
Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same. More... | |
void | period_us (int us) |
Set the PWM period, specified in microseconds (int), keeping the duty cycle the same. More... | |
void | pulsewidth (float seconds) |
Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. More... | |
void | pulsewidth_ms (int ms) |
Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same. More... | |
void | pulsewidth_us (int us) |
Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same. More... | |
void | suspend () |
Suspend PWM operation. More... | |
void | resume () |
Resume PWM operation. More... | |
PwmOut & | operator= (float value) |
A operator shorthand for write() More... | |
PwmOut & | operator= (PwmOut &rhs) |
A operator shorthand for write() More... | |
operator float () | |
An operator shorthand for read() More... |
PwmOut hello, world
This code example uses the default period of 0.020s and ramps the duty cycle from 0% to 100% in increments of 10%.
#include "mbed.h"
PwmOut led(LED1);
int main() {
// specify period first
led.period(4.0f); // 4 second period
led.write(0.50f); // 50% duty cycle, relative to period
//led = 0.5f; // shorthand for led.write()
//led.pulsewidth(2); // alternative to led.write, set duty cycle time in seconds
while(1);
}
PwmOut code examples
Example one
This code example sets the period in seconds and the duty cycle as a percentage of the period in floating point (range: 0 to 1). The effect of this code snippet will be to blink LED2 over a four-second cycle, 50% on, for a pattern of two seconds on, two seconds off.
#include "mbed.h"
PwmOut led(LED2);
int main() {
// specify period first, then everything else
led.period(4.0f); // 4 second period
led.write(0.50f); // 50% duty cycle
while(1); // led flashing
}
Example two
The following example does the same thing, but instead of specifying the duty cycle as a relative percentage of the period, it specifies it as an absolute value in seconds. In this case we have a four-second period and a two-second duty cycle, meaning the LED will be on for two seconds and off for two seconds.
#include "mbed.h"
PwmOut led(LED2);
int main() {
// specify period first, then everything else
led.period(4.0f); // 4 second period
led.pulsewidth(2); // 2 second pulse (on)
while(1); // led flashing
}