20
20
Therefore, their executions are not blocked by bad-behaving functions / tasks.
21
21
This important feature is absolutely necessary for mission-critical tasks.
22
22
23
- Version: 1.0.0
23
+ Version: 1.0.1
24
24
25
25
Version Modified By Date Comments
26
26
------- ----------- ---------- -----------
27
27
1.0.0 K Hoang 20/09/2021 Initial coding for ESP32, ESP32_S2, ESP32_C3 boards with ESP32 core v2.0.0+
28
+ 1.0.1 K Hoang 21/09/2021 Fix bug. Ading PWM end-of-duty-cycle callback function. Improve examples
28
29
*****************************************************************************************************************************/
29
30
#if !defined( ESP32 )
30
31
#error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
41
42
42
43
#include " ESP32_PWM.h"
43
44
44
- #define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of ESP32
45
-
46
45
#ifndef LED_BUILTIN
47
46
#define LED_BUILTIN 2
48
47
#endif
57
56
58
57
#define HW_TIMER_INTERVAL_US 20L
59
58
60
- uint32_t startMillis = 0 ;
59
+ uint32_t startMicros = 0 ;
61
60
62
61
// Init ESP32 timer 1
63
62
ESP32Timer ITimer (1 );
@@ -72,6 +71,8 @@ bool IRAM_ATTR TimerHandler(void * timerNo)
72
71
return true ;
73
72
}
74
73
74
+ // ////////////////////////////////////////////////////
75
+
75
76
#define NUMBER_ISR_PWMS 16
76
77
77
78
#define PIN_D0 0 // Pin D0 mapped to pin GPIO0/BOOT/ADC11/TOUCH1 of ESP32
@@ -97,6 +98,12 @@ bool IRAM_ATTR TimerHandler(void * timerNo)
97
98
#define PIN_D26 26 // Pin D26 mapped to pin GPIO26/ADC19/DAC2 of ESP32
98
99
#define PIN_D27 27 // Pin D27 mapped to pin GPIO27/ADC17/TOUCH7 of ESP32
99
100
101
+ // ////////////////////////////////////////////////////
102
+
103
+ #define USING_PWM_FREQUENCY true
104
+
105
+ // ////////////////////////////////////////////////////
106
+
100
107
// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11
101
108
uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
102
109
{
@@ -196,7 +203,7 @@ void doingSomething15()
196
203
{
197
204
}
198
205
199
- irqCallback irqCallbackFunc [NUMBER_ISR_PWMS] =
206
+ irqCallback irqCallbackStartFunc [NUMBER_ISR_PWMS] =
200
207
{
201
208
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
202
209
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
@@ -222,8 +229,8 @@ void setup()
222
229
// Interval in microsecs
223
230
if (ITimer.attachInterruptInterval (HW_TIMER_INTERVAL_US, TimerHandler))
224
231
{
225
- startMillis = millis ();
226
- Serial.print (F (" Starting ITimer OK, millis () = " )); Serial.println (startMillis );
232
+ startMicros = micros ();
233
+ Serial.print (F (" Starting ITimer OK, micros () = " )); Serial.println (startMicros );
227
234
}
228
235
else
229
236
Serial.println (F (" Can't set ITimer. Select another freq. or timer" ));
@@ -235,17 +242,20 @@ void setup()
235
242
// void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
236
243
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
237
244
245
+ #if USING_PWM_FREQUENCY
246
+
238
247
// You can use this with PWM_Freq in Hz
239
- ISR_PWM.setPWM (PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackFunc [i]);
248
+ ISR_PWM.setPWM (PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackStartFunc [i]);
240
249
241
- #if USING_MICROS_RESOLUTION
242
- // Or using period in microsecs resolution
243
- // ISR_PWM.setPWM_Period(PWM_Pin[i], PWM_Period[i], PWM_DutyCycle[i], irqCallbackFunc[i]);
244
250
#else
251
+ #if USING_MICROS_RESOLUTION
252
+ // Or using period in microsecs resolution
253
+ ISR_PWM.setPWM_Period (PWM_Pin[i], PWM_Period[i], PWM_DutyCycle[i], irqCallbackStartFunc[i]);
254
+ #else
245
255
// Or using period in millisecs resolution
246
- // ISR_PWM.setPWM_Period(PWM_Pin[i], PWM_Period[i] / 1000, PWM_DutyCycle[i], irqCallbackFunc[i]);
256
+ ISR_PWM.setPWM_Period (PWM_Pin[i], PWM_Period[i] / 1000 , PWM_DutyCycle[i], irqCallbackStartFunc[i]);
257
+ #endif
247
258
#endif
248
-
249
259
}
250
260
}
251
261
0 commit comments