Skip to content

Class hal::pwm

ClassList > hal > pwm

Pulse Width Modulation (PWM) channel hardware abstraction. More...

  • #include <pwm.hpp>

Inherited by the following classes: hal::lpc40::pwm, hal::mock::pwm, hal::soft::inert_pwm

Classes

Type Name
struct duty_cycle_t
Feedback setting the pwm duty cycle.
struct frequency_t
Feedback setting the pwm frequency.

Public Functions

Type Name
result< duty_cycle_t > duty_cycle (float p_duty_cycle)
Set the pwm waveform duty cycle.
result< frequency_t > frequency (hertz p_frequency)
Set the pwm waveform frequency.
virtual ~pwm () = default

Detailed Description

This driver controls the waveform generation of a square wave and its properties such as frequency and duty cycle.

Frequency, meaning how often the waveform cycles from from low to high.

Duty cycle, what proportion of the wavelength of the pulse is the voltage HIGH.

___ _ | | | | |___| ^ ^ ^ |<------ high ------>||

HIGH Duration = 18 segments LOW Duration = 5 segments Duty Cycle = 20 / (20 + 5) = 80%

If each segment is 1us then the wavelength is 25us Thus frequency is (1 / 25us) = 40kHz

PWM is used for power control like motor control, lighting, transmitting signals to servos, sending telemetry and much more.

Public Functions Documentation

function duty_cycle

Set the pwm waveform duty cycle.

inline result< duty_cycle_t > hal::pwm::duty_cycle (
    float p_duty_cycle
) 

The input value p_duty_cycle is a 32-bit floating point value from 0.0f to 1.0f.

The floating point value is directly proportional to the duty cycle percentage, such that 0.0f is 0%, 0.25f is 25%, 0.445f is 44.5% and 1.0f is 100%.

This function clamps the input value between 0.0f and 1.0f and thus values passed to driver implementations are guaranteed to be within this range. Callers of this function do not need to clamp their values before passing them into this function as it would be redundant. The rationale for doing this at the interface layer is that it allows callers and driver implementors to omit redundant clamping code, reducing code bloat.

Parameters:

  • p_duty_cycle - a value from 0.0f to +1.0f representing the duty cycle percentage.

Returns:

result<duty_cycle_t> - success or failure

function frequency

Set the pwm waveform frequency.

inline result< frequency_t > hal::pwm::frequency (
    hertz p_frequency
) 

This function clamps the input value between 1.0_Hz and 1.0_GHz and thus values passed to driver implementations are guaranteed to be within this range. Callers of this function do not need to clamp their values before passing them into this function as it would be redundant. The rationale for doing this at the interface layer is that it allows callers and driver implementors to omit redundant clamping code, reducing code bloat.

Parameters:

  • p_frequency - settings to apply to pwm driver

Returns:

result<frequency_t> - success or failure

Exception:

  • std::errc::argument_out_of_domain - if the frequency is beyond what the pwm generator is capable of achieving.

function ~pwm

virtual hal::pwm::~pwm () = default

The documentation for this class was generated from the following file libraries/include/libhal/pwm.hpp