2020 Therefore, their executions are not blocked by bad-behaving functions / tasks.
2121 This important feature is absolutely necessary for mission-critical tasks.
2222
23- Version: 1.0.0
23+ Version: 1.0.1
2424
2525 Version Modified By Date Comments
2626 ------- ----------- ---------- -----------
2727 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
2829*****************************************************************************************************************************/
2930#if !defined( ESP32 )
3031 #error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
4142
4243#include " ESP32_PWM.h"
4344
44- #define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of ESP32
45-
4645#ifndef LED_BUILTIN
4746 #define LED_BUILTIN 2
4847#endif
5756
5857#define HW_TIMER_INTERVAL_US 20L
5958
60- uint32_t startMillis = 0 ;
59+ uint32_t startMicros = 0 ;
6160
6261// Init ESP32 timer 1
6362ESP32Timer ITimer (1 );
@@ -72,6 +71,8 @@ bool IRAM_ATTR TimerHandler(void * timerNo)
7271 return true ;
7372}
7473
74+ // ////////////////////////////////////////////////////
75+
7576#define NUMBER_ISR_PWMS 16
7677
7778#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)
9798#define PIN_D26 26 // Pin D26 mapped to pin GPIO26/ADC19/DAC2 of ESP32
9899#define PIN_D27 27 // Pin D27 mapped to pin GPIO27/ADC17/TOUCH7 of ESP32
99100
101+ // ////////////////////////////////////////////////////
102+
103+ #define USING_PWM_FREQUENCY true
104+
105+ // ////////////////////////////////////////////////////
106+
100107// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11
101108uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
102109{
@@ -196,7 +203,7 @@ void doingSomething15()
196203{
197204}
198205
199- irqCallback irqCallbackFunc [NUMBER_ISR_PWMS] =
206+ irqCallback irqCallbackStartFunc [NUMBER_ISR_PWMS] =
200207{
201208 doingSomething0, doingSomething1, doingSomething2, doingSomething3,
202209 doingSomething4, doingSomething5, doingSomething6, doingSomething7,
@@ -222,8 +229,8 @@ void setup()
222229 // Interval in microsecs
223230 if (ITimer.attachInterruptInterval (HW_TIMER_INTERVAL_US, TimerHandler))
224231 {
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 );
227234 }
228235 else
229236 Serial.println (F (" Can't set ITimer. Select another freq. or timer" ));
@@ -235,17 +242,20 @@ void setup()
235242 // void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
236243 // , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
237244
245+ #if USING_PWM_FREQUENCY
246+
238247 // 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]);
240249
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]);
244250#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
245255 // 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
247258#endif
248-
249259 }
250260}
251261
0 commit comments