V D
/
di19sept2017
Potmeter FastPWM.h
FastPWM.h@0:16be67f4d9ac, 2017-09-19 (annotated)
- Committer:
- vd
- Date:
- Tue Sep 19 16:52:01 2017 +0000
- Revision:
- 0:16be67f4d9ac
Potmeter FastPWM.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vd | 0:16be67f4d9ac | 1 | /* |
vd | 0:16be67f4d9ac | 2 | .---. _....._ |
vd | 0:16be67f4d9ac | 3 | / p `\ .-""`: :`"-. |
vd | 0:16be67f4d9ac | 4 | |__ - | ,' . ' ', |
vd | 0:16be67f4d9ac | 5 | ._> \ /: : ; :, |
vd | 0:16be67f4d9ac | 6 | '-. '\`. . : ' \ |
vd | 0:16be67f4d9ac | 7 | `. | .'._.' '._.' '._.'. | |
vd | 0:16be67f4d9ac | 8 | `;-\. : : ' '/,__, |
vd | 0:16be67f4d9ac | 9 | .-'`'._ ' . : _.'.__.' |
vd | 0:16be67f4d9ac | 10 | ((((-'/ `";--..:..--;"` \ |
vd | 0:16be67f4d9ac | 11 | .' / \ \ |
vd | 0:16be67f4d9ac | 12 | jgs ((((-' ((((-' |
vd | 0:16be67f4d9ac | 13 | |
vd | 0:16be67f4d9ac | 14 | Yeah ASCII art turtle more fun than copyright stuff |
vd | 0:16be67f4d9ac | 15 | */ |
vd | 0:16be67f4d9ac | 16 | |
vd | 0:16be67f4d9ac | 17 | |
vd | 0:16be67f4d9ac | 18 | #include "mbed.h" |
vd | 0:16be67f4d9ac | 19 | |
vd | 0:16be67f4d9ac | 20 | #ifndef FASTPWM_H |
vd | 0:16be67f4d9ac | 21 | #define FASTPWM_H |
vd | 0:16be67f4d9ac | 22 | |
vd | 0:16be67f4d9ac | 23 | /** Library that allows faster and/or higher resolution PWM output |
vd | 0:16be67f4d9ac | 24 | * |
vd | 0:16be67f4d9ac | 25 | * Library can directly replace standard mbed PWM library. |
vd | 0:16be67f4d9ac | 26 | * |
vd | 0:16be67f4d9ac | 27 | * Contrary to the default mbed library, this library takes doubles instead of floats. The compiler will autocast if needed, |
vd | 0:16be67f4d9ac | 28 | * but do take into account it is done for a reason, your accuracy will otherwise be limitted by the floating point precision. |
vd | 0:16be67f4d9ac | 29 | */ |
vd | 0:16be67f4d9ac | 30 | class FastPWM : public PwmOut { |
vd | 0:16be67f4d9ac | 31 | public: |
vd | 0:16be67f4d9ac | 32 | /** |
vd | 0:16be67f4d9ac | 33 | * Create a FastPWM object connected to the specified pin |
vd | 0:16be67f4d9ac | 34 | * |
vd | 0:16be67f4d9ac | 35 | * @param pin - PWM pin to connect to |
vd | 0:16be67f4d9ac | 36 | * @param prescaler - Clock prescaler, -1 is dynamic (default), 0 is bit random, everything else normal |
vd | 0:16be67f4d9ac | 37 | */ |
vd | 0:16be67f4d9ac | 38 | FastPWM(PinName pin, int prescaler = -1); |
vd | 0:16be67f4d9ac | 39 | ~FastPWM(); |
vd | 0:16be67f4d9ac | 40 | |
vd | 0:16be67f4d9ac | 41 | /** |
vd | 0:16be67f4d9ac | 42 | * Set the PWM period, specified in seconds (double), keeping the pulsewidth the same. |
vd | 0:16be67f4d9ac | 43 | */ |
vd | 0:16be67f4d9ac | 44 | void period(double seconds); |
vd | 0:16be67f4d9ac | 45 | |
vd | 0:16be67f4d9ac | 46 | /** |
vd | 0:16be67f4d9ac | 47 | * Set the PWM period, specified in milli-seconds (int), keeping the pulsewidth the same. |
vd | 0:16be67f4d9ac | 48 | */ |
vd | 0:16be67f4d9ac | 49 | void period_ms(int ms); |
vd | 0:16be67f4d9ac | 50 | |
vd | 0:16be67f4d9ac | 51 | /** |
vd | 0:16be67f4d9ac | 52 | * Set the PWM period, specified in micro-seconds (int), keeping the pulsewidth the same. |
vd | 0:16be67f4d9ac | 53 | */ |
vd | 0:16be67f4d9ac | 54 | void period_us(int us); |
vd | 0:16be67f4d9ac | 55 | |
vd | 0:16be67f4d9ac | 56 | /** |
vd | 0:16be67f4d9ac | 57 | * Set the PWM period, specified in micro-seconds (double), keeping the pulsewidth the same. |
vd | 0:16be67f4d9ac | 58 | */ |
vd | 0:16be67f4d9ac | 59 | void period_us(double us); |
vd | 0:16be67f4d9ac | 60 | |
vd | 0:16be67f4d9ac | 61 | /** |
vd | 0:16be67f4d9ac | 62 | * Set the PWM period, specified in clock ticks, keeping _pulse width_ the same. |
vd | 0:16be67f4d9ac | 63 | * |
vd | 0:16be67f4d9ac | 64 | * This function can be used if low overhead is required. Do take into account the result is |
vd | 0:16be67f4d9ac | 65 | * board (clock frequency) dependent, and this does not keep an equal duty cycle! |
vd | 0:16be67f4d9ac | 66 | */ |
vd | 0:16be67f4d9ac | 67 | void period_ticks(uint32_t ticks); |
vd | 0:16be67f4d9ac | 68 | |
vd | 0:16be67f4d9ac | 69 | /** |
vd | 0:16be67f4d9ac | 70 | * Set the PWM pulsewidth, specified in seconds (double), keeping the period the same. |
vd | 0:16be67f4d9ac | 71 | */ |
vd | 0:16be67f4d9ac | 72 | void pulsewidth(double seconds); |
vd | 0:16be67f4d9ac | 73 | |
vd | 0:16be67f4d9ac | 74 | /** |
vd | 0:16be67f4d9ac | 75 | * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
vd | 0:16be67f4d9ac | 76 | */ |
vd | 0:16be67f4d9ac | 77 | void pulsewidth_ms(int ms); |
vd | 0:16be67f4d9ac | 78 | |
vd | 0:16be67f4d9ac | 79 | /** |
vd | 0:16be67f4d9ac | 80 | * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
vd | 0:16be67f4d9ac | 81 | */ |
vd | 0:16be67f4d9ac | 82 | void pulsewidth_us(int us); |
vd | 0:16be67f4d9ac | 83 | |
vd | 0:16be67f4d9ac | 84 | /** |
vd | 0:16be67f4d9ac | 85 | * Set the PWM pulsewidth, specified in micro-seconds (double), keeping the period the same. |
vd | 0:16be67f4d9ac | 86 | */ |
vd | 0:16be67f4d9ac | 87 | void pulsewidth_us(double us); |
vd | 0:16be67f4d9ac | 88 | |
vd | 0:16be67f4d9ac | 89 | /** |
vd | 0:16be67f4d9ac | 90 | * Set the PWM period, specified in clock ticks, keeping the period the same. |
vd | 0:16be67f4d9ac | 91 | * |
vd | 0:16be67f4d9ac | 92 | * This function can be used if low overhead is required. Do take into account the result is |
vd | 0:16be67f4d9ac | 93 | * board (clock frequency) dependent! |
vd | 0:16be67f4d9ac | 94 | */ |
vd | 0:16be67f4d9ac | 95 | void pulsewidth_ticks(uint32_t ticks); |
vd | 0:16be67f4d9ac | 96 | |
vd | 0:16be67f4d9ac | 97 | /** |
vd | 0:16be67f4d9ac | 98 | * Set the ouput duty-cycle, specified as a percentage (double) |
vd | 0:16be67f4d9ac | 99 | * |
vd | 0:16be67f4d9ac | 100 | * @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%). |
vd | 0:16be67f4d9ac | 101 | */ |
vd | 0:16be67f4d9ac | 102 | void write(double duty); |
vd | 0:16be67f4d9ac | 103 | |
vd | 0:16be67f4d9ac | 104 | /** |
vd | 0:16be67f4d9ac | 105 | * Return the ouput duty-cycle, specified as a percentage (double) |
vd | 0:16be67f4d9ac | 106 | * |
vd | 0:16be67f4d9ac | 107 | * @param return - A double value representing the output duty-cycle, specified as a percentage. |
vd | 0:16be67f4d9ac | 108 | */ |
vd | 0:16be67f4d9ac | 109 | double read( void ); |
vd | 0:16be67f4d9ac | 110 | |
vd | 0:16be67f4d9ac | 111 | /** |
vd | 0:16be67f4d9ac | 112 | * An operator shorthand for write() |
vd | 0:16be67f4d9ac | 113 | */ |
vd | 0:16be67f4d9ac | 114 | FastPWM& operator= (double value); |
vd | 0:16be67f4d9ac | 115 | |
vd | 0:16be67f4d9ac | 116 | /** |
vd | 0:16be67f4d9ac | 117 | * An operator shorthand for read() |
vd | 0:16be67f4d9ac | 118 | */ |
vd | 0:16be67f4d9ac | 119 | operator double(); |
vd | 0:16be67f4d9ac | 120 | |
vd | 0:16be67f4d9ac | 121 | /** |
vd | 0:16be67f4d9ac | 122 | * Set the PWM prescaler |
vd | 0:16be67f4d9ac | 123 | * |
vd | 0:16be67f4d9ac | 124 | * The period of all PWM pins on the same PWM unit have to be reset after using this! |
vd | 0:16be67f4d9ac | 125 | * |
vd | 0:16be67f4d9ac | 126 | * @param value - The required prescaler. Special values: 0 = lock current prescaler, -1 = use dynamic prescaler |
vd | 0:16be67f4d9ac | 127 | * @param return - The prescaler which was set (can differ from requested prescaler if not possible) |
vd | 0:16be67f4d9ac | 128 | */ |
vd | 0:16be67f4d9ac | 129 | int prescaler(int value); |
vd | 0:16be67f4d9ac | 130 | |
vd | 0:16be67f4d9ac | 131 | private: |
vd | 0:16be67f4d9ac | 132 | void initFastPWM(void); |
vd | 0:16be67f4d9ac | 133 | |
vd | 0:16be67f4d9ac | 134 | uint32_t setPrescaler( uint32_t reqScale ); |
vd | 0:16be67f4d9ac | 135 | int calcPrescaler(uint64_t clocks); |
vd | 0:16be67f4d9ac | 136 | uint32_t getPeriod( void ); |
vd | 0:16be67f4d9ac | 137 | |
vd | 0:16be67f4d9ac | 138 | void updateTicks( uint32_t prescaler ); |
vd | 0:16be67f4d9ac | 139 | uint32_t bits; |
vd | 0:16be67f4d9ac | 140 | |
vd | 0:16be67f4d9ac | 141 | double _duty; |
vd | 0:16be67f4d9ac | 142 | |
vd | 0:16be67f4d9ac | 143 | double dticks, dticks_us; |
vd | 0:16be67f4d9ac | 144 | int iticks_ms, iticks_us; |
vd | 0:16be67f4d9ac | 145 | |
vd | 0:16be67f4d9ac | 146 | bool dynamicPrescaler; |
vd | 0:16be67f4d9ac | 147 | |
vd | 0:16be67f4d9ac | 148 | void *fast_obj; |
vd | 0:16be67f4d9ac | 149 | }; |
vd | 0:16be67f4d9ac | 150 | #endif |