Library that allows for higher resolution and speed than standard mbed PWM library. Based on FastPWM without floating point calculation in most functions

Fork of FastPWM by Erik -

Embed: (wiki syntax)

« Back to documentation index

HighPWM Class Reference

HighPWM Class Reference

Library that allows faster and/or higher resolution PWM output. More...

#include <HighPWM.h>

Public Member Functions

 HighPWM (PinName pin)
 Create a HighPWM object connected to the specified pin.
void frequency (double frq)
 Set the PWM frequency, specified in Hz (double), keeping the duty cycle the same.
void period (double seconds)
 Set the PWM period, specified in seconds (double), keeping the duty cycle the same.
void period_us (double us)
 Set the PWM period, specified in micro-seconds (double), keeping the duty cycle the same.
void period_ms (unsigned int ms)
 Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
void period_us (unsigned int us)
 Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
void period_ticks (unsigned int ticks)
 Set the PWM period, specified in cpu ticks based on 96MHz (int), keeping the duty cycle the same.
void pulsewidth (double seconds)
 Set the PWM pulsewidth, specified in seconds (double), keeping the period the same.
void pulsewidth_us (double us)
 Set the PWM pulsewidth, specified in micro seconds (double), keeping the period the same.
void pulsewidth_ms (unsigned int ms)
 Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
void pulsewidth_us (unsigned int us)
 Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
void pulsewidth_ticks (unsigned int ticks)
 Set the PWM pulsewidth, specified in cpu ticks based on 96MHz (int), keeping the period the same.
void write (double duty)
 Set the ouput duty-cycle, specified as a percentage (double)
double read (void)
 Return the ouput duty-cycle, specified as a percentage (double)
HighPWMoperator= (double value)
 An operator shorthand for write()
 operator double ()
 An operator shorthand for read()

Detailed Description

Library that allows faster and/or higher resolution PWM output.

This library is based original on FastPWM by Erik Olieman but without floating point (double) calculation in each call of pulsewidth_xx. This change was nessecary to get a performant motion controller.

Library can directly replace standard mbed PWM library. Only limitation is that the maximum PWM period is four times shorter The maximum achievable period is roughly 40 seconds, I dont think that should be a problem. Do take into account all PWM objects (based on defailt PWM lib) will run four times faster than default.

Contrary to the default mbed library, this library takes doubles instead of floats. The compiler will autocast if needed, but do take into account it is done for a reason, your accuracy will otherwise be limitted by the floating point precision.

Using this library for a RC servo the resolution of steps (min to max) is increased from 1000 to 96000.

This library can be also used as a analoge output (with external low-pass filter) with a frequency of e.g. 20kHz and a resolution 4800 steps (similar to a 12 bit DAC) instead of 50 steps (similar to a 5 bit DAC) on original PWM lib.

In your program you can define F_CLK if you use a different clock frequency than the default one.

Only works on LPC1768 for now. If you want support for the other one, send a PM and I will have a look, but I cannot even compile for it.

Definition at line 34 of file HighPWM.h.


Constructor & Destructor Documentation

HighPWM ( PinName  pin )

Create a HighPWM object connected to the specified pin.

Parameters:
pin- PWM pin to connect to

Definition at line 3 of file HighPWM.cpp.


Member Function Documentation

void frequency ( double  frq )

Set the PWM frequency, specified in Hz (double), keeping the duty cycle the same.

Definition at line 40 of file HighPWM.cpp.

operator double (  )

An operator shorthand for read()

Definition at line 119 of file HighPWM.cpp.

HighPWM & operator= ( double  value )

An operator shorthand for write()

Definition at line 113 of file HighPWM.cpp.

void period ( double  seconds )

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

Definition at line 46 of file HighPWM.cpp.

void period_ms ( unsigned int  ms )

Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.

Definition at line 56 of file HighPWM.cpp.

void period_ticks ( unsigned int  ticks )

Set the PWM period, specified in cpu ticks based on 96MHz (int), keeping the duty cycle the same.

Definition at line 66 of file HighPWM.cpp.

void period_us ( unsigned int  us )

Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.

Definition at line 61 of file HighPWM.cpp.

void period_us ( double  us )

Set the PWM period, specified in micro-seconds (double), keeping the duty cycle the same.

Definition at line 51 of file HighPWM.cpp.

void pulsewidth ( double  seconds )

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

Definition at line 76 of file HighPWM.cpp.

void pulsewidth_ms ( unsigned int  ms )

Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.

Definition at line 86 of file HighPWM.cpp.

void pulsewidth_ticks ( unsigned int  ticks )

Set the PWM pulsewidth, specified in cpu ticks based on 96MHz (int), keeping the period the same.

Definition at line 96 of file HighPWM.cpp.

void pulsewidth_us ( unsigned int  us )

Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.

Definition at line 91 of file HighPWM.cpp.

void pulsewidth_us ( double  us )

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

Definition at line 81 of file HighPWM.cpp.

double read ( void   )

Return the ouput duty-cycle, specified as a percentage (double)

Parameters:
return- A double value representing the output duty-cycle, specified as a percentage.

Definition at line 108 of file HighPWM.cpp.

void write ( double  duty )

Set the ouput duty-cycle, specified as a percentage (double)

Parameters:
duty- A double value representing the output duty-cycle, specified as a percentage. The value should lie between 0.0 (representing on 0%) and 1.0 (representing on 100%).

Definition at line 103 of file HighPWM.cpp.