Michael Ernst Peter / FastPWM

Dependents:   PM2_Libary PM2_Libary

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastPWM.h Source File

FastPWM.h

00001 /*
00002     .---.           _....._
00003    /  p  `\     .-""`:     :`"-.
00004    |__   - |  ,'     .     '    ',
00005     ._>    \ /:      :     ;      :,
00006      '-.    '\`.     .     :     '  \
00007         `.   | .'._.' '._.' '._.'.  |
00008           `;-\.   :     :     '   '/,__,
00009           .-'`'._ '     .     : _.'.__.'
00010          ((((-'/ `";--..:..--;"` \
00011              .'   /           \   \
00012        jgs  ((((-'           ((((-'
00013        
00014 Yeah ASCII art turtle more fun than copyright stuff
00015 */
00016 
00017 
00018 #include "mbed.h"
00019 
00020 #ifndef FASTPWM_H
00021 #define FASTPWM_H
00022 
00023 /** Library that allows faster and/or higher resolution PWM output
00024   *
00025   * Library can directly replace standard mbed PWM library.
00026   *
00027   * Contrary to the default mbed library, this library takes doubles instead of floats. The compiler will autocast if needed,
00028   * but do take into account it is done for a reason, your accuracy will otherwise be limitted by the floating point precision.
00029   */
00030 class FastPWM : public PwmOut {
00031 public:
00032     /**
00033     * Create a FastPWM object connected to the specified pin
00034     *
00035     * @param pin - PWM pin to connect to
00036     * @param prescaler - Clock prescaler, -1 is dynamic (default), 0 is bit random, everything else normal
00037     */
00038     FastPWM(PinName pin, int prescaler = -1);
00039     ~FastPWM(); 
00040     
00041     /**
00042     * Set the PWM period, specified in seconds (double), keeping the pulsewidth the same.
00043     */
00044     void period(double seconds);
00045     
00046     /**
00047     * Set the PWM period, specified in seconds (float), keeping the pulsewidth the same.
00048     */
00049     void period(float seconds);
00050     
00051     /**
00052     * Set the PWM period, specified in milli-seconds (int), keeping the pulsewidth the same.
00053     */
00054     void period_ms(int ms);
00055     
00056     /**
00057     * Set the PWM period, specified in micro-seconds (int), keeping the pulsewidth the same.
00058     */
00059     void period_us(int us);
00060     
00061     /**
00062     * Set the PWM period, specified in micro-seconds (double), keeping the pulsewidth the same.
00063     */
00064     void period_us(double us);
00065     
00066     /**
00067     * Set the PWM period, specified in clock ticks, keeping _pulse width_ the same.
00068     *
00069     * This function can be used if low overhead is required. Do take into account the result is
00070     * board (clock frequency) dependent, and this does not keep an equal duty cycle!
00071     */
00072     void period_ticks(uint32_t ticks);
00073     
00074     /**
00075     * Set the PWM pulsewidth, specified in seconds (double), keeping the period the same.
00076     */
00077     void pulsewidth(double seconds);
00078     
00079     /**
00080     * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
00081     */
00082     void pulsewidth_ms(int ms);
00083     
00084     /**
00085     * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
00086     */
00087     void pulsewidth_us(int us);
00088     
00089     /**
00090     * Set the PWM pulsewidth, specified in micro-seconds (double), keeping the period the same.
00091     */
00092     void pulsewidth_us(double us);
00093     
00094     /**
00095     * Set the PWM period, specified in clock ticks, keeping the period the same.
00096     *
00097     * This function can be used if low overhead is required. Do take into account the result is
00098     * board (clock frequency) dependent!
00099     */
00100     void pulsewidth_ticks(uint32_t ticks);
00101     
00102     /**
00103     * Set the ouput duty-cycle, specified as a percentage (double)
00104     *
00105     * @param 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%).
00106     */
00107     void write(double duty);
00108     void write(float duty);
00109     
00110     /**
00111     * Return the ouput duty-cycle, specified as a percentage (double)
00112     *
00113     * @param return - A double value representing the output duty-cycle, specified as a percentage.
00114     */
00115     double read( void );
00116     
00117     /**
00118     * An operator shorthand for write()
00119     */
00120     FastPWM& operator= (double value);
00121     
00122     /**
00123     * An operator shorthand for read()
00124     */
00125     operator double();
00126     
00127     /**
00128     * Set the PWM prescaler
00129     *
00130     * The period of all PWM pins on the same PWM unit have to be reset after using this!
00131     *
00132     * @param value - The required prescaler. Special values: 0 = lock current prescaler, -1 = use dynamic prescaler
00133     * @param return - The prescaler which was set (can differ from requested prescaler if not possible)
00134     */
00135     int prescaler(int value);
00136     
00137 private:
00138     void initFastPWM(void);
00139     
00140     uint32_t setPrescaler( uint32_t reqScale );
00141     int calcPrescaler(uint64_t clocks);
00142     uint32_t getPeriod( void );
00143     
00144     void updateTicks( uint32_t prescaler );
00145     uint32_t bits;
00146     
00147     double _duty;
00148     
00149     double dticks, dticks_us;
00150     int iticks_ms, iticks_us;
00151     
00152     bool dynamicPrescaler;
00153     
00154     void *fast_obj;
00155 };
00156 #endif