My forked repository. DISCO_F407VG, DISCO_F303VC, DISCO_F051R8 and DISCO_F100RB maybe added.
Dependents: FastPWM-DISCO-test
Fork of FastPWM by
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 milli-seconds (int), keeping the pulsewidth the same. 00048 */ 00049 void period_ms(int ms); 00050 00051 /** 00052 * Set the PWM period, specified in micro-seconds (int), keeping the pulsewidth the same. 00053 */ 00054 void period_us(int us); 00055 00056 /** 00057 * Set the PWM period, specified in micro-seconds (double), keeping the pulsewidth the same. 00058 */ 00059 void period_us(double us); 00060 00061 /** 00062 * Set the PWM period, specified in clock ticks, keeping _pulse width_ the same. 00063 * 00064 * This function can be used if low overhead is required. Do take into account the result is 00065 * board (clock frequency) dependent, and this does not keep an equal duty cycle! 00066 */ 00067 void period_ticks(uint32_t ticks); 00068 00069 /** 00070 * Set the PWM pulsewidth, specified in seconds (double), keeping the period the same. 00071 */ 00072 void pulsewidth(double seconds); 00073 00074 /** 00075 * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. 00076 */ 00077 void pulsewidth_ms(int ms); 00078 00079 /** 00080 * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. 00081 */ 00082 void pulsewidth_us(int us); 00083 00084 /** 00085 * Set the PWM pulsewidth, specified in micro-seconds (double), keeping the period the same. 00086 */ 00087 void pulsewidth_us(double us); 00088 00089 /** 00090 * Set the PWM period, specified in clock ticks, keeping the period the same. 00091 * 00092 * This function can be used if low overhead is required. Do take into account the result is 00093 * board (clock frequency) dependent! 00094 */ 00095 void pulsewidth_ticks(uint32_t ticks); 00096 00097 /** 00098 * Set the ouput duty-cycle, specified as a percentage (double) 00099 * 00100 * @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%). 00101 */ 00102 void write(double duty); 00103 00104 /** 00105 * Return the ouput duty-cycle, specified as a percentage (double) 00106 * 00107 * @param return - A double value representing the output duty-cycle, specified as a percentage. 00108 */ 00109 double read( void ); 00110 00111 /** 00112 * An operator shorthand for write() 00113 */ 00114 FastPWM& operator= (double value); 00115 00116 /** 00117 * An operator shorthand for read() 00118 */ 00119 operator double(); 00120 00121 /** 00122 * Set the PWM prescaler 00123 * 00124 * The period of all PWM pins on the same PWM unit have to be reset after using this! 00125 * 00126 * @param value - The required prescaler. Special values: 0 = lock current prescaler, -1 = use dynamic prescaler 00127 * @param return - The prescaler which was set (can differ from requested prescaler if not possible) 00128 */ 00129 int prescaler(int value); 00130 00131 private: 00132 void initFastPWM(void); 00133 00134 uint32_t setPrescaler( uint32_t reqScale ); 00135 int calcPrescaler(uint64_t clocks); 00136 uint32_t getPeriod( void ); 00137 00138 void updateTicks( uint32_t prescaler ); 00139 uint32_t bits; 00140 00141 double _duty; 00142 00143 double dticks, dticks_us; 00144 int iticks_ms, iticks_us; 00145 00146 bool dynamicPrescaler; 00147 00148 void *fast_obj; 00149 }; 00150 #endif
Generated on Tue Jul 19 2022 04:55:37 by 1.7.2