Mbed OS Reference
Loading...
Searching...
No Matches
PwmOut Class Reference

A pulse-width modulation digital output. More...

#include <PwmOut.h>

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...
 
int read_period_us ()
 Read the PWM period. 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...
 
int read_pulsewidth_us ()
 Read the PWM pulsewidth. More...
 
int read_pulsewitdth_us ()
 Read the PWM pulsewidth. More...
 
void suspend ()
 Suspend PWM operation. More...
 
void resume ()
 Resume PWM operation. More...
 
PwmOutoperator= (float value)
 A operator shorthand for write() More...
 
PwmOutoperator= (PwmOut &rhs)
 A operator shorthand for write() More...
 
 operator float ()
 An operator shorthand for read() More...
 

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;
ThisThread::sleep_for(200);
if(led == 1.0) {
led = 0;
}
}
}
A pulse-width modulation digital output.
Definition: PwmOut.h:54

Definition at line 54 of file PwmOut.h.

Constructor & Destructor Documentation

◆ PwmOut() [1/2]

PwmOut ( PinName  pin)

Create a PwmOut connected to the specified pin.

Parameters
pinPwmOut pin to connect to

◆ PwmOut() [2/2]

PwmOut ( const PinMap pinmap)

Create a PwmOut connected to the specified pin.

Parameters
pinmapreference to structure which holds static pinmap. This reference is stored in the PwmOut, so the pinmap needs to live as long as this object does.

Member Function Documentation

◆ write()

void write ( float  value)

Set the output duty-cycle, specified as a percentage (float)

Parameters
valueA 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.

◆ read()

float read ( )

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().

◆ period()

void period ( float  seconds)

Set the PWM period, specified in seconds (float), keeping the duty cycle the same.

Parameters
secondsChange 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.

◆ period_ms()

void period_ms ( int  ms)

Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.

Parameters
msChange the period of a PWM signal in milliseconds without modifying the duty cycle

◆ period_us()

void period_us ( int  us)

Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.

Parameters
usChange the period of a PWM signal in microseconds without modifying the duty cycle

◆ read_period_us()

int read_period_us ( )

Read the PWM period.

Returns
The PWM period, specified in microseconds (int)

◆ pulsewidth()

void pulsewidth ( float  seconds)

Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.

Parameters
secondsChange the pulse width of a PWM signal specified in seconds (float)

◆ pulsewidth_ms()

void pulsewidth_ms ( int  ms)

Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.

Parameters
msChange the pulse width of a PWM signal specified in milliseconds

◆ pulsewidth_us()

void pulsewidth_us ( int  us)

Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.

Parameters
usChange the pulse width of a PWM signal specified in microseconds

◆ read_pulsewidth_us()

int read_pulsewidth_us ( )

Read the PWM pulsewidth.

Returns
The PWM pulsewidth, specified in microseconds (int)

◆ read_pulsewitdth_us()

int read_pulsewitdth_us ( )

Read the PWM pulsewidth.

Returns
The PWM pulsewidth, specified in microseconds (int)

◆ suspend()

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.

◆ resume()

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.

◆ operator=() [1/2]

PwmOut & operator= ( float  value)

A operator shorthand for write()

See also
PwmOut::write()

Definition at line 169 of file PwmOut.h.

◆ operator=() [2/2]

PwmOut & operator= ( PwmOut rhs)

A operator shorthand for write()

See also
PwmOut::write()

Definition at line 179 of file PwmOut.h.

◆ operator float()

operator float ( )

An operator shorthand for read()

See also
PwmOut::read()

Definition at line 189 of file PwmOut.h.