Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
PwmOut Class Reference
[PwmOut class]
A pulse-width modulation digital output. More...
#include <PwmOut.h>
Public Member Functions | |
PwmOut (PinName pin) | |
Create a PwmOut connected to the specified pin. | |
PwmOut (const PinMap &pinmap) | |
Create a PwmOut connected to the specified pin. | |
void | write (float value) |
Set the output 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), keeping the duty cycle the same. | |
void | period_ms (int ms) |
Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same. | |
void | period_us (int us) |
Set the PWM period, specified in microseconds (int), keeping the duty cycle the same. | |
void | pulsewidth (float seconds) |
Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. | |
void | pulsewidth_ms (int ms) |
Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same. | |
void | pulsewidth_us (int us) |
Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same. | |
void | suspend () |
Suspend PWM operation. | |
void | resume () |
Resume PWM operation. | |
PwmOut & | operator= (float value) |
A operator shorthand for write() | |
PwmOut & | operator= (PwmOut &rhs) |
A operator shorthand for write() | |
operator float () | |
An operator shorthand for read() | |
Protected Member Functions | |
void | lock_deep_sleep () |
Lock deep sleep only if it is not yet locked. | |
void | unlock_deep_sleep () |
Unlock deep sleep in case it is locked. | |
void | init () |
Initialize this instance. | |
void | deinit () |
Power down this instance. |
Detailed Description
A pulse-width modulation digital output.
- Note:
- Synchronization level: Interrupt safe
Example
// Gradually change the intensity of the LED. #include "mbed.h" PwmOut led(LED1); int main() { while(1) { led = led + 0.01; wait(0.2); if(led == 1.0) { led = 0; } } }
Definition at line 54 of file PwmOut.h.
Constructor & Destructor Documentation
PwmOut | ( | PinName | pin ) |
Create a PwmOut connected to the specified pin.
- Parameters:
-
pin PwmOut pin to connect to
Definition at line 29 of file PwmOut.cpp.
PwmOut | ( | const PinMap & | pinmap ) |
Create a PwmOut connected to the specified pin.
- Parameters:
-
pinmap reference to structure which holds static pinmap.
Definition at line 38 of file PwmOut.cpp.
Member Function Documentation
void deinit | ( | ) | [protected] |
Power down this instance.
Definition at line 156 of file PwmOut.cpp.
void init | ( | ) | [protected] |
Initialize this instance.
Definition at line 143 of file PwmOut.cpp.
void lock_deep_sleep | ( | ) | [protected] |
Lock deep sleep only if it is not yet locked.
Definition at line 127 of file PwmOut.cpp.
operator float | ( | ) |
PwmOut& operator= | ( | float | value ) |
void period | ( | float | seconds ) |
Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
- Parameters:
-
seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
- Note:
- The resolution is currently in microseconds; periods smaller than this will be set to zero.
Definition at line 65 of file PwmOut.cpp.
void period_ms | ( | int | ms ) |
Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
- Parameters:
-
ms Change the period of a PWM signal in milliseconds without modifying the duty cycle
Definition at line 72 of file PwmOut.cpp.
void period_us | ( | int | us ) |
Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
- Parameters:
-
us Change the period of a PWM signal in microseconds without modifying the duty cycle
Definition at line 79 of file PwmOut.cpp.
void pulsewidth | ( | float | seconds ) |
Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
- Parameters:
-
seconds Change the pulse width of a PWM signal specified in seconds (float)
Definition at line 86 of file PwmOut.cpp.
void pulsewidth_ms | ( | int | ms ) |
Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
- Parameters:
-
ms Change the pulse width of a PWM signal specified in milliseconds
Definition at line 93 of file PwmOut.cpp.
void pulsewidth_us | ( | int | us ) |
Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
- Parameters:
-
us Change the pulse width of a PWM signal specified in microseconds
Definition at line 100 of file PwmOut.cpp.
float read | ( | void | ) |
Return the current output duty-cycle setting, measured as a percentage (float)
- Returns:
- A floating-point value representing the current duty-cycle being output on the pin, measured as a percentage. The returned value will lie between 0.0f (representing on 0%) and 1.0f (representing on 100%).
- Note:
- This value may not match exactly the value set by a previous write().
Definition at line 57 of file PwmOut.cpp.
void resume | ( | ) |
Resume PWM operation.
Control the PWM state. This is primarily intended to resume PWM operations after a previous PwmOut::suspend call; This call restores the device state prior to suspension.
Definition at line 117 of file PwmOut.cpp.
void suspend | ( | ) |
Suspend PWM operation.
Control the PWM state. This is primarily intended for temporary power-saving; This call can allow pwm to be temporarily disabled to permit power saving without losing device state. The subsequent function call must be PwmOut::resume for PWM to resume; any other calls prior to resuming are undefined behavior.
Definition at line 107 of file PwmOut.cpp.
void unlock_deep_sleep | ( | ) | [protected] |
Unlock deep sleep in case it is locked.
Definition at line 135 of file PwmOut.cpp.
void write | ( | float | value ) |
Set the output duty-cycle, specified as a percentage (float)
- Parameters:
-
value A floating-point value representing the output duty-cycle, specified as a percentage. The value should lie between 0.0f (representing on 0%) and 1.0f (representing on 100%). Values outside this range will be saturated to 0.0f or 1.0f.
Definition at line 50 of file PwmOut.cpp.
Generated on Tue Jul 12 2022 13:55:42 by
