|
30 | 30 | // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
|
31 | 31 | #define _PWM_LOGLEVEL_ 4
|
32 | 32 |
|
33 |
| -#define USING_MICROS_RESOLUTION true //false |
| 33 | +#define USING_MICROS_RESOLUTION true //false |
34 | 34 |
|
| 35 | +// Select a Timer Clock |
| 36 | +#define USING_TIM_DIV1 true // for shortest and most accurate timer |
| 37 | +#define USING_TIM_DIV16 false // for medium time and medium accurate timer |
| 38 | +#define USING_TIM_DIV256 false // for longest timer but least accurate. Default |
| 39 | + |
| 40 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
35 | 41 | #include "ESP8266_PWM.h"
|
36 | 42 |
|
37 | 43 | #include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
|
@@ -97,12 +103,12 @@ typedef struct
|
97 | 103 | irqCallback irqCallbackStopFunc;
|
98 | 104 |
|
99 | 105 | #if USING_PWM_FREQUENCY
|
100 |
| - uint32_t PWM_Freq; |
| 106 | + double PWM_Freq; |
101 | 107 | #else
|
102 |
| - uint32_t PWM_Period; |
| 108 | + double PWM_Period; |
103 | 109 | #endif
|
104 | 110 |
|
105 |
| - uint32_t PWM_DutyCycle; |
| 111 | + double PWM_DutyCycle; |
106 | 112 |
|
107 | 113 | uint64_t deltaMicrosStart;
|
108 | 114 | uint64_t previousMicrosStart;
|
@@ -134,22 +140,22 @@ uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
|
134 | 140 | };
|
135 | 141 |
|
136 | 142 | // You can assign any interval for any timer here, in microseconds
|
137 |
| -uint32_t PWM_Period[NUMBER_ISR_PWMS] = |
| 143 | +double PWM_Period[NUMBER_ISR_PWMS] = |
138 | 144 | {
|
139 |
| - 1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L |
| 145 | + 1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0 |
140 | 146 | };
|
141 | 147 |
|
142 | 148 |
|
143 | 149 | // You can assign any interval for any timer here, in Hz
|
144 |
| -uint32_t PWM_Freq[NUMBER_ISR_PWMS] = |
| 150 | +double PWM_Freq[NUMBER_ISR_PWMS] = |
145 | 151 | {
|
146 |
| - 1, 2, 3, 4, 5, 6, 7, 8 |
| 152 | + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 |
147 | 153 | };
|
148 | 154 |
|
149 | 155 | // You can assign any interval for any timer here, in Microseconds
|
150 |
| -uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] = |
| 156 | +double PWM_DutyCycle[NUMBER_ISR_PWMS] = |
151 | 157 | {
|
152 |
| - 5, 10, 20, 30, 40, 45, 50, 55 |
| 158 | + 5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0 |
153 | 159 | };
|
154 | 160 |
|
155 | 161 | ///////////////////////////////////
|
@@ -269,29 +275,29 @@ void doingSomethingStop7()
|
269 | 275 | ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
|
270 | 276 | {
|
271 | 277 | //pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
|
272 |
| - { PIN_D0, doingSomethingStart0, doingSomethingStop0, 1, 5, 0, 0, 0, 0 }, |
273 |
| - { PIN_D1, doingSomethingStart1, doingSomethingStop1, 2, 10, 0, 0, 0, 0 }, |
274 |
| - { LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 3, 20, 0, 0, 0, 0 }, |
275 |
| - { PIN_D3, doingSomethingStart3, doingSomethingStop3, 4, 30, 0, 0, 0, 0 }, |
276 |
| - { PIN_D4, doingSomethingStart4, doingSomethingStop4, 5, 40, 0, 0, 0, 0 }, |
277 |
| - { PIN_D5, doingSomethingStart5, doingSomethingStop5, 6, 45, 0, 0, 0, 0 }, |
278 |
| - { PIN_D6, doingSomethingStart6, doingSomethingStop6, 7, 50, 0, 0, 0, 0 }, |
279 |
| - { PIN_D7, doingSomethingStart7, doingSomethingStop7, 8, 55, 0, 0, 0, 0 }, |
| 278 | + { PIN_D0, doingSomethingStart0, doingSomethingStop0, 1.0, 5.0, 0, 0, 0, 0 }, |
| 279 | + { PIN_D1, doingSomethingStart1, doingSomethingStop1, 2.0, 10.0, 0, 0, 0, 0 }, |
| 280 | + { LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 3.0, 20.0, 0, 0, 0, 0 }, |
| 281 | + { PIN_D3, doingSomethingStart3, doingSomethingStop3, 4.0, 30.0, 0, 0, 0, 0 }, |
| 282 | + { PIN_D4, doingSomethingStart4, doingSomethingStop4, 5.0, 40.0, 0, 0, 0, 0 }, |
| 283 | + { PIN_D5, doingSomethingStart5, doingSomethingStop5, 6.0, 45.0, 0, 0, 0, 0 }, |
| 284 | + { PIN_D6, doingSomethingStart6, doingSomethingStop6, 7.0, 50.0, 0, 0, 0, 0 }, |
| 285 | + { PIN_D7, doingSomethingStart7, doingSomethingStop7, 8.0, 55.0, 0, 0, 0, 0 }, |
280 | 286 | };
|
281 | 287 |
|
282 | 288 | #else // #if USING_PWM_FREQUENCY
|
283 | 289 |
|
284 | 290 | ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
|
285 | 291 | {
|
286 | 292 | //irqCallbackStartFunc, PWM_Period, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
|
287 |
| - { PIN_D0, doingSomethingStart0, doingSomethingStop0, 1000000L, 5, 0, 0, 0, 0 }, |
288 |
| - { PIN_D1, doingSomethingStart1, doingSomethingStop1, 500000L, 10, 0, 0, 0, 0 }, |
289 |
| - { LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 333333L, 20, 0, 0, 0, 0 }, |
290 |
| - { PIN_D3, doingSomethingStart3, doingSomethingStop3, 250000L, 30, 0, 0, 0, 0 }, |
291 |
| - { PIN_D4, doingSomethingStart4, doingSomethingStop4, 200000L, 40, 0, 0, 0, 0 }, |
292 |
| - { PIN_D5, doingSomethingStart5, doingSomethingStop5, 166667L, 45, 0, 0, 0, 0 }, |
293 |
| - { PIN_D6, doingSomethingStart6, doingSomethingStop6, 142857L, 50, 0, 0, 0, 0 }, |
294 |
| - { PIN_D7, doingSomethingStart7, doingSomethingStop7, 125000L, 55, 0, 0, 0, 0 }, |
| 293 | + { PIN_D0, doingSomethingStart0, doingSomethingStop0, 1000000.0, 5.0, 0, 0, 0, 0 }, |
| 294 | + { PIN_D1, doingSomethingStart1, doingSomethingStop1, 500000.0, 10.0, 0, 0, 0, 0 }, |
| 295 | + { LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 333333,333, 20.0, 0, 0, 0, 0 }, |
| 296 | + { PIN_D3, doingSomethingStart3, doingSomethingStop3, 250000.0, 30.0, 0, 0, 0, 0 }, |
| 297 | + { PIN_D4, doingSomethingStart4, doingSomethingStop4, 200000.0, 40.0, 0, 0, 0, 0 }, |
| 298 | + { PIN_D5, doingSomethingStart5, doingSomethingStop5, 166666.667, 45.0, 0, 0, 0, 0 }, |
| 299 | + { PIN_D6, doingSomethingStart6, doingSomethingStop6, 142857.143, 50.0, 0, 0, 0, 0 }, |
| 300 | + { PIN_D7, doingSomethingStart7, doingSomethingStop7, 125000.0, 55.0, 0, 0, 0, 0 }, |
295 | 301 | };
|
296 | 302 |
|
297 | 303 | #endif // #if USING_PWM_FREQUENCY
|
@@ -376,7 +382,7 @@ void simpleTimerdoingSomething2s()
|
376 | 382 | Serial.print(F("PWM Channel : ")); Serial.print(i);
|
377 | 383 |
|
378 | 384 | #if USING_PWM_FREQUENCY
|
379 |
| - Serial.print(1000000 / PWM_Freq[i]); |
| 385 | + Serial.print(1000000.0 / PWM_Freq[i]); |
380 | 386 | #else
|
381 | 387 | Serial.print(PWM_Period[i]);
|
382 | 388 | #endif
|
|
0 commit comments