From f3b27af3eba6de4dd56ae49f04e4a8f8c3b18f9d Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Sat, 27 Aug 2022 19:49:13 -0400 Subject: [PATCH] v1.1.1 to fix bug, optimize examples ### Releases v1.1.1 1. Fix bug possibly causing system crash when using `_TIMERINTERRUPT_LOGLEVEL_ > 0` 2. Optimize code in examples --- changelog.md | 6 ++ .../Argument_Complex/Argument_Complex.ino | 72 +++++--------- .../Argument_Complex_Multi.h | 30 ++++-- .../Argument_Complex_Multi.ino | 44 +-------- examples/Argument_None/Argument_None.ino | 52 +++++----- examples/Argument_Simple/Argument_Simple.ino | 63 ++++++------- examples/Change_Interval/Change_Interval.ino | 94 ++++++------------- .../Change_Interval_HF/Change_Interval_HF.ino | 94 ++++++------------- examples/FakeAnalogWrite/FakeAnalogWrite.ino | 69 +++++--------- .../ISR_16_Timers_Array_Complex.ino | 69 ++++++-------- examples/ISR_RPM_Measure/ISR_RPM_Measure.ino | 67 +++++-------- .../ISR_Timers_Array_Simple.ino | 66 +++++-------- examples/RPM_Measure/RPM_Measure.ino | 67 +++++-------- examples/SwitchDebounce/SwitchDebounce.ino | 68 +++++--------- examples/TimerDuration/TimerDuration.ino | 71 ++++++-------- .../TimerInterruptTest/TimerInterruptTest.ino | 90 +++++++----------- library.json | 2 +- library.properties | 2 +- src/ATmega_TimerInterrupt.h | 3 +- src/ATmega_TimerInterrupt.hpp | 9 +- src/ATmega_TimerInterrupt_Impl.h | 85 ++--------------- src/ISR_Timer.h | 5 +- src/ISR_Timer.hpp | 11 ++- src/ISR_Timer_Impl.h | 3 +- src/TimerInterrupt_Generic_Debug.h | 3 +- 25 files changed, 420 insertions(+), 725 deletions(-) diff --git a/changelog.md b/changelog.md index 89b4cf1..545b9a3 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.1.1](#releases-v111) * [Releases v1.1.0](#releases-v110) * [Releases v1.0.0](#releases-v100) @@ -21,6 +22,11 @@ ## Changelog +### Releases v1.1.1 + +1. Fix bug possibly causing system crash when using `_TIMERINTERRUPT_LOGLEVEL_ > 0` +2. Optimize code in examples + ### Releases v1.1.0 1. Fix missing code for Timer3 and Timer4 diff --git a/examples/Argument_Complex/Argument_Complex.ino b/examples/Argument_Complex/Argument_Complex.ino index cad9520..48d7fb2 100644 --- a/examples/Argument_Complex/Argument_Complex.ino +++ b/examples/Argument_Complex/Argument_Complex.ino @@ -37,14 +37,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -106,52 +120,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, (unsigned int) &myOutputPins)) - { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, (unsigned int) &myOutputPins)) { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - -#if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print(F("OutputPin = ")); Serial.print(outputPin); - Serial.print(F(" address: ")); Serial.println((uint32_t) &outputPin ); -#endif - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/Argument_Complex_Multi/Argument_Complex_Multi.h b/examples/Argument_Complex_Multi/Argument_Complex_Multi.h index 2266f24..f7dd02e 100644 --- a/examples/Argument_Complex_Multi/Argument_Complex_Multi.h +++ b/examples/Argument_Complex_Multi/Argument_Complex_Multi.h @@ -43,14 +43,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // Can be included in many files without `Multiple Definitions` Linker Error diff --git a/examples/Argument_Complex_Multi/Argument_Complex_Multi.ino b/examples/Argument_Complex_Multi/Argument_Complex_Multi.ino index 50828ed..12139d7 100644 --- a/examples/Argument_Complex_Multi/Argument_Complex_Multi.ino +++ b/examples/Argument_Complex_Multi/Argument_Complex_Multi.ino @@ -52,53 +52,19 @@ void setup() // Timer0 is already used for micros(), millis(), delay(), etc and can't be used // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency - -#if USE_TIMER_1 - - ITimer1.init(); + + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, (unsigned int) &myOutputPins)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, (unsigned int) &myOutputPins)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - -#if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print(F("OutputPin = ")); Serial.print(outputPin); - Serial.print(F(" address: ")); Serial.println((uint32_t) &outputPin ); -#endif - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/Argument_None/Argument_None.ino b/examples/Argument_None/Argument_None.ino index 7be39cf..f94738e 100644 --- a/examples/Argument_None/Argument_None.ino +++ b/examples/Argument_None/Argument_None.ino @@ -38,12 +38,24 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_2) - #warning Using Timer1 and Timer2 -#elif (USE_TIMER_3) - #warning Using Timer1 and Timer3 -#elif (USE_TIMER_4) - #warning Using Timer1 and Timer4 +#if USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_2) + #warning Using Timer1 and Timer2 + #elif (USE_TIMER_3) + #warning Using Timer1 and Timer3 + #elif (USE_TIMER_4) + #warning Using Timer1 and Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -98,38 +110,26 @@ void setup() if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); -#if USE_TIMER_2 - // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); -#elif USE_TIMER_3 - - // For ATmega1284(P) - ITimer3.init(); + /////////////////////////////////////////////// + + // Init second timer + CurrentTimer.init(); - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/Argument_Simple/Argument_Simple.ino b/examples/Argument_Simple/Argument_Simple.ino index 387b247..bd992c1 100644 --- a/examples/Argument_Simple/Argument_Simple.ino +++ b/examples/Argument_Simple/Argument_Simple.ino @@ -27,7 +27,7 @@ // _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 // Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define TIMER_INTERRUPT_DEBUG 0 -#define _TIMERINTERRUPT_LOGLEVEL_ 3 +#define _TIMERINTERRUPT_LOGLEVEL_ 4 #define USE_TIMER_1 true @@ -38,12 +38,24 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_2) - #warning Using Timer1 and Timer2 -#elif (USE_TIMER_3) - #warning Using Timer1 and Timer3 -#elif (USE_TIMER_4) - #warning Using Timer1 and Timer4 +#if USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_2) + #warning Using Timer1 and Timer2 + #elif (USE_TIMER_3) + #warning Using Timer1 and Timer3 + #elif (USE_TIMER_4) + #warning Using Timer1 and Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -109,7 +121,9 @@ void setup() // Timer0 is already used for micros(), millis(), delay(), etc and can't be used // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency - + +#if USE_TIMER_1 + ITimer1.init(); // Using ATmega324 with 16MHz CPU clock , @@ -118,7 +132,7 @@ void setup() if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, outputPin1)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); #if (TIMER_INTERRUPT_DEBUG > 1) Serial.print(F("OutputPin1 = ")); Serial.print(outputPin1); @@ -127,30 +141,17 @@ void setup() } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); + +#endif -#if USE_TIMER_2 + /////////////////////////////////////////////// - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); + // Init second timer + CurrentTimer.init(); -#if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print(F("OutputPin = ")); Serial.print(outputPin); - Serial.print(F(" address: ")); Serial.println((uint32_t) &outputPin ); -#endif - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); #if (TIMER_INTERRUPT_DEBUG > 1) Serial.print(F("OutputPin = ")); Serial.print(outputPin); @@ -158,9 +159,7 @@ void setup() #endif } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/Change_Interval/Change_Interval.ino b/examples/Change_Interval/Change_Interval.ino index 7fc561d..173ebc9 100644 --- a/examples/Change_Interval/Change_Interval.ino +++ b/examples/Change_Interval/Change_Interval.ino @@ -48,14 +48,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -71,17 +85,13 @@ #define TIMER1_INTERVAL_MS 100UL -#define TIMER_INTERVAL_MS 200UL volatile uint32_t Timer1Count = 0; -volatile uint32_t TimerCount = 0; void printResult(uint32_t currTime) { Serial.print(F("Time = ")); Serial.print(currTime); Serial.print(F(", Timer1Count = ")); Serial.print(Timer1Count); - - Serial.print(F(", TimerCount = ")); Serial.println(TimerCount); } void TimerHandler1(void) @@ -96,18 +106,6 @@ void TimerHandler1(void) toggle1 = !toggle1; } -void TimerHandler(void) -{ - static bool toggle = false; - - // Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR - TimerCount++; - - //timer interrupt toggles outputPin - digitalWrite(LED_BLUE, toggle); - toggle = !toggle; -} - void setup() { pinMode(LED_BUILTIN, OUTPUT); @@ -124,42 +122,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency - ITimer1.init(); + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1)) - { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#if USE_TIMER_2 - - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1)) { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } #define CHECK_INTERVAL_MS 10000L @@ -187,20 +161,10 @@ void loop() // interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // bool setInterval(unsigned long interval, timer_callback callback, unsigned long duration) - ITimer1.setInterval(TIMER1_INTERVAL_MS * (multFactor + 1), TimerHandler1); - - Serial.print(F("Changing Interval, Timer1 = ")); Serial.println(TIMER1_INTERVAL_MS * (multFactor + 1)); + CurrentTimer.setInterval(TIMER1_INTERVAL_MS * (multFactor + 1), TimerHandler1); -#if USE_TIMER_2 - ITimer2.setInterval(TIMER_INTERVAL_MS * (multFactor + 1), TimerHandler); - - Serial.print(F("Changing Interval, Timer2 = ")); Serial.println(TIMER_INTERVAL_MS * (multFactor + 1)); -#elif USE_TIMER_3 - ITimer3.setInterval(TIMER_INTERVAL_MS * (multFactor + 1), TimerHandler); - - Serial.print(F("Changing Interval, Timer3 = ")); Serial.println(TIMER_INTERVAL_MS * (multFactor + 1)); -#endif - + Serial.print(F("Changing Interval, Timer = ")); Serial.println(TIMER1_INTERVAL_MS * (multFactor + 1)); + lastChangeTime = currTime; } } diff --git a/examples/Change_Interval_HF/Change_Interval_HF.ino b/examples/Change_Interval_HF/Change_Interval_HF.ino index ceeb24b..6ab9f80 100644 --- a/examples/Change_Interval_HF/Change_Interval_HF.ino +++ b/examples/Change_Interval_HF/Change_Interval_HF.ino @@ -48,14 +48,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -71,20 +85,15 @@ #define TIMER1_FREQUENCY 5000UL -#define TIMER_FREQUENCY 1000UL - volatile uint32_t Timer1Count = 0; -volatile uint32_t TimerCount = 0; void printResult(uint32_t currTime) { Serial.print(F("Time = ")); Serial.print(currTime); Serial.print(F(", Timer1Count = ")); Serial.print(Timer1Count); - - Serial.print(F(", TimerCount = ")); Serial.println(TimerCount); } -void TimerHandler1(void) +void TimerHandler1() { static bool toggle1 = false; @@ -96,18 +105,6 @@ void TimerHandler1(void) toggle1 = !toggle1; } -void TimerHandler(void) -{ - static bool toggle = false; - - // Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR - TimerCount++; - - //timer interrupt toggles outputPin - digitalWrite(LED_BLUE, toggle); - toggle = !toggle; -} - void setup() { pinMode(LED_BUILTIN, OUTPUT); @@ -124,42 +121,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency - ITimer1.init(); + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - if (ITimer1.attachInterrupt(TIMER1_FREQUENCY, TimerHandler1)) - { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#if USE_TIMER_2 - - ITimer2.init(); - - if (ITimer2.attachInterrupt(TIMER_FREQUENCY, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterrupt(TIMER_FREQUENCY, TimerHandler)) + if (CurrentTimer.attachInterrupt(TIMER1_FREQUENCY, TimerHandler1)) { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } #define CHECK_INTERVAL_MS 10000L @@ -187,21 +160,10 @@ void loop() // interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // bool setInterval(unsigned long interval, timer_callback callback, unsigned long duration) - ITimer1.setFrequency(TIMER1_FREQUENCY / (multFactor + 1), TimerHandler1); + CurrentTimer.setFrequency(TIMER1_FREQUENCY / (multFactor + 1), TimerHandler1); Serial.print(F("Changing Frequency, Timer1 = ")); Serial.println(TIMER1_FREQUENCY / (multFactor + 1)); -#if USE_TIMER_2 - ITimer2.setFrequency(TIMER_FREQUENCY / (multFactor + 1), TimerHandler); - - Serial.print(F("Changing Frequency, Timer2 = ")); Serial.println(TIMER_FREQUENCY / (multFactor + 1)); - -#elif USE_TIMER_3 - ITimer3.setFrequency(TIMER_FREQUENCY / (multFactor + 1), TimerHandler); - - Serial.print(F("Changing Frequency, Timer3 = ")); Serial.println(TIMER_FREQUENCY / (multFactor + 1)); -#endif - lastChangeTime = currTime; } } diff --git a/examples/FakeAnalogWrite/FakeAnalogWrite.ino b/examples/FakeAnalogWrite/FakeAnalogWrite.ino index d4e17cc..b6e2a9a 100644 --- a/examples/FakeAnalogWrite/FakeAnalogWrite.ino +++ b/examples/FakeAnalogWrite/FakeAnalogWrite.ino @@ -56,14 +56,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -121,8 +135,6 @@ void TimerHandler(void) ///////////////////////////////////////////////// - - typedef void (*irqCallback) (void); ///////////////////////////////////////////////// @@ -206,47 +218,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - if (ITimer1.attachInterruptInterval(TIMER_FREQUENCY_HZ, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_FREQUENCY_HZ, TimerHandler)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_FREQUENCY_HZ, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_FREQUENCY_HZ, TimerHandler)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITime1. Select another freq. or timer")); // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary // You can use up to 16 timer for each ISR_Timer diff --git a/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino b/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino index d1e213e..be76126 100644 --- a/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino +++ b/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino @@ -36,14 +36,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -106,7 +120,7 @@ typedef void (*irqCallback) (void); unsigned long previousMillis; } ISRTimerData; - // In NRF52, avoid doing something fancy in ISR, for example Serial.print() + // Avoid doing something fancy in ISR, for example Serial.print() // The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment // Or you can get this run-time error / crash @@ -314,48 +328,19 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); + Serial.println(F("Can't set ITimer. Select another freq. or timer")); -#elif USE_TIMER_2 - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif - //ISR_timer.setInterval(2000L, doingSomething2s); //ISR_timer.setInterval(5000L, doingSomething5s); diff --git a/examples/ISR_RPM_Measure/ISR_RPM_Measure.ino b/examples/ISR_RPM_Measure/ISR_RPM_Measure.ino index b25b80a..26b8787 100644 --- a/examples/ISR_RPM_Measure/ISR_RPM_Measure.ino +++ b/examples/ISR_RPM_Measure/ISR_RPM_Measure.ino @@ -47,14 +47,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -151,47 +165,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); // Assumming the interruptPin will go LOW attachInterrupt(digitalPinToInterrupt(interruptPin), detectRotation, FALLING); diff --git a/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino b/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino index 4583248..eb30335 100644 --- a/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino +++ b/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino @@ -37,14 +37,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -154,47 +168,19 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); + Serial.println(F("Can't set ITimer. Select another freq. or timer")); -#endif ISR_timer.setInterval(2000L, doingSomething2s); ISR_timer.setInterval(5000L, doingSomething5s); diff --git a/examples/RPM_Measure/RPM_Measure.ino b/examples/RPM_Measure/RPM_Measure.ino index cd164a5..58b798e 100644 --- a/examples/RPM_Measure/RPM_Measure.ino +++ b/examples/RPM_Measure/RPM_Measure.ino @@ -47,14 +47,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -130,47 +144,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/SwitchDebounce/SwitchDebounce.ino b/examples/SwitchDebounce/SwitchDebounce.ino index c067d29..509dcf7 100644 --- a/examples/SwitchDebounce/SwitchDebounce.ino +++ b/examples/SwitchDebounce/SwitchDebounce.ino @@ -35,7 +35,6 @@ // Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. #define TIMER_INTERRUPT_DEBUG 0 #define _TIMERINTERRUPT_LOGLEVEL_ 0 - // Select just 1 TIMER to be true #define USE_TIMER_1 true #define USE_TIMER_2 false @@ -44,14 +43,28 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_1) - #warning Using Timer1 -#elif (USE_TIMER_2) - #warning Using Timer2 -#elif (USE_TIMER_3) - #warning Using Timer3 -#elif (USE_TIMER_4) - #warning Using Timer4 +#if USE_TIMER_1 + #define CurrentTimer ITimer1 +#elif USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_1) + #warning Using Timer1 + #elif (USE_TIMER_2) + #warning Using Timer2 + #elif (USE_TIMER_3) + #warning Using Timer3 + #elif (USE_TIMER_4) + #warning Using Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -170,47 +183,18 @@ void setup() // Select Timer 1-2 // Timer 2 is 8-bit timer, only for higher frequency -#if USE_TIMER_1 - - ITimer1.init(); - - // Using ATmega324 with 16MHz CPU clock , - // For 16-bit timer 1, set frequency from 0.2385 to some KHz - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - - if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); - -#elif USE_TIMER_2 + CurrentTimer.init(); // Using ATmega324 with 16MHz CPU clock , // For 16-bit timer 1, set frequency from 0.2385 to some KHz // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz - ITimer2.init(); - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); } else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); - - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/TimerDuration/TimerDuration.ino b/examples/TimerDuration/TimerDuration.ino index ba12562..1c3a05d 100644 --- a/examples/TimerDuration/TimerDuration.ino +++ b/examples/TimerDuration/TimerDuration.ino @@ -38,12 +38,24 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_2) - #warning Using Timer1 and Timer2 -#elif (USE_TIMER_3) - #warning Using Timer1 and Timer3 -#elif (USE_TIMER_4) - #warning Using Timer1 and Timer4 +#if USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_2) + #warning Using Timer1 and Timer2 + #elif (USE_TIMER_3) + #warning Using Timer1 and Timer3 + #elif (USE_TIMER_4) + #warning Using Timer1 and Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -59,10 +71,6 @@ void TimerHandler1(unsigned int outputPin = LED_BUILTIN) { static bool toggle1 = false; -#if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print("ITimer1 called, millis() = "); Serial.println(millis()); -#endif - //timer interrupt toggles pin LED_BUILTIN digitalWrite(outputPin, toggle1); toggle1 = !toggle1; @@ -70,27 +78,15 @@ void TimerHandler1(unsigned int outputPin = LED_BUILTIN) #endif -#if (USE_TIMER_2 || USE_TIMER_3) - void TimerHandler(unsigned int outputPin = LED_BUILTIN) { static bool toggle = false; -#if (TIMER_INTERRUPT_DEBUG > 1) - #if USE_TIMER_2 - Serial.print("ITimer2 called, millis() = "); - #endif - - Serial.println(millis()); -#endif - //timer interrupt toggles outputPin digitalWrite(outputPin, toggle); toggle = !toggle; } -#endif - unsigned int outputPin1 = LED_BUILTIN; unsigned int outputPin = A0; @@ -128,36 +124,29 @@ void setup() if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, outputPin1, TIMER1_DURATION_MS)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); #endif -#if USE_TIMER_2 - - ITimer2.init(); + /////////////////////////////////////////////// + + // Init second timer + CurrentTimer.init(); - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin, TIMER_DURATION_MS)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin, TIMER_DURATION_MS)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); +#if (TIMER_INTERRUPT_DEBUG > 1) + Serial.print(F("OutputPin = ")); Serial.print(outputPin); + Serial.print(F(" address: ")); Serial.println((uint32_t) &outputPin ); +#endif } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() diff --git a/examples/TimerInterruptTest/TimerInterruptTest.ino b/examples/TimerInterruptTest/TimerInterruptTest.ino index 6f9457a..0d1a9dd 100644 --- a/examples/TimerInterruptTest/TimerInterruptTest.ino +++ b/examples/TimerInterruptTest/TimerInterruptTest.ino @@ -38,12 +38,24 @@ // TIMER_4 Only valid for ATmega324PB, not ready in core yet #define USE_TIMER_4 false -#if (USE_TIMER_2) - #warning Using Timer1 and Timer2 -#elif (USE_TIMER_3) - #warning Using Timer1 and Timer3 -#elif (USE_TIMER_4) - #warning Using Timer1 and Timer4 +#if USE_TIMER_2 + #define CurrentTimer ITimer2 +#elif USE_TIMER_3 + #define CurrentTimer ITimer3 +#elif USE_TIMER_4 + #define CurrentTimer ITimer4 +#else + #error You must select one Timer +#endif + +#if (_TIMERINTERRUPT_LOGLEVEL_ > 3) + #if (USE_TIMER_2) + #warning Using Timer1 and Timer2 + #elif (USE_TIMER_3) + #warning Using Timer1 and Timer3 + #elif (USE_TIMER_4) + #warning Using Timer1 and Timer4 + #endif #endif // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -59,10 +71,6 @@ void TimerHandler1(unsigned int outputPin = LED_BUILTIN) { static bool toggle1 = false; -#if (TIMER_INTERRUPT_DEBUG > 1) - Serial.print("ITimer1 called, millis() = "); Serial.println(millis()); -#endif - //timer interrupt toggles pin LED_BUILTIN digitalWrite(outputPin, toggle1); toggle1 = !toggle1; @@ -70,27 +78,15 @@ void TimerHandler1(unsigned int outputPin = LED_BUILTIN) #endif -#if (USE_TIMER_2 || USE_TIMER_3) - void TimerHandler(unsigned int outputPin = LED_BUILTIN) { static bool toggle = false; -#if (TIMER_INTERRUPT_DEBUG > 1) - #if USE_TIMER_2 - Serial.print("ITimer2 called, millis() = "); - #endif - - Serial.println(millis()); -#endif - //timer interrupt toggles outputPin digitalWrite(outputPin, toggle); toggle = !toggle; } -#endif - unsigned int outputPin1 = LED_BUILTIN; unsigned int outputPin = A0; @@ -137,36 +133,29 @@ void setup() if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, outputPin1, TIMER1_DURATION_MS)) { - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); } else Serial.println(F("Can't set ITimer1. Select another freq. or timer")); #endif -#if USE_TIMER_2 - - ITimer2.init(); + /////////////////////////////////////////////// + + // Init second timer + CurrentTimer.init(); - if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin, TIMER_DURATION_MS)) + if (CurrentTimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) { - Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); - } - else - Serial.println(F("Can't set ITimer2. Select another freq. or timer")); - -#elif USE_TIMER_3 - - ITimer3.init(); + Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis()); - if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin, TIMER_DURATION_MS)) - { - Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); +#if (TIMER_INTERRUPT_DEBUG > 1) + Serial.print(F("OutputPin = ")); Serial.print(outputPin); + Serial.print(F(" address: ")); Serial.println((uint32_t) &outputPin ); +#endif } else - Serial.println(F("Can't set ITimer3. Select another freq. or timer")); - -#endif + Serial.println(F("Can't set ITimer. Select another freq. or timer")); } void loop() @@ -180,30 +169,15 @@ void loop() if (millis() - lastTimer1 > TIMER1_DURATION_MS * 3) { -#if USE_TIMER_2 - if (millis() - lastTimer > TIMER_DURATION_MS * 3) { lastTimer = millis(); - Serial.print(F("Re-enable ITimer2, millis() = ")); Serial.println(lastTimer); + Serial.print(F("Re-enable ITimer, millis() = ")); Serial.println(lastTimer); - ITimer2.reattachInterrupt(TIMER_DURATION_MS); + CurrentTimer.reattachInterrupt(TIMER_DURATION_MS); } -#elif USE_TIMER_3 - - if (millis() - lastTimer > TIMER_DURATION_MS * 3) - { - lastTimer = millis(); - - Serial.print(F("Re-enable ITimer3, millis() = ")); Serial.println(lastTimer); - - ITimer3.reattachInterrupt(TIMER_DURATION_MS); - } - -#endif - lastTimer1 = millis(); // try reinit timer Serial.print(F("Re-enable ITimer1, millis() = ")); Serial.print(lastTimer1); diff --git a/library.json b/library.json index 33f6a03..c051bb7 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ATmega_TimerInterrupt", - "version": "1.1.0", + "version": "1.1.1", "keywords": "timer, interrupt, timer-interrupt, hardware, isr, isr-based, hardware-timer, mission-critical, accuracy, precise, avr, atmega, atmega164, atmega324, atmega644, atmega1284, atmega16, atmega32, atmega8535, mightycore", "description": "This library enables you to use Interrupt from Hardware Timers on an AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MCUdude MightyCore. It now supports 16 ISR-based timers, while consuming only 1 hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.", "authors": diff --git a/library.properties b/library.properties index 2d8ead9..3a40831 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ATmega_TimerInterrupt -version=1.1.0 +version=1.1.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Interrupt from Hardware Timers on an AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MCUdude MightyCore diff --git a/src/ATmega_TimerInterrupt.h b/src/ATmega_TimerInterrupt.h index def32c0..b69773c 100644 --- a/src/ATmega_TimerInterrupt.h +++ b/src/ATmega_TimerInterrupt.h @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples ****************************************************************************************************************************/ #pragma once diff --git a/src/ATmega_TimerInterrupt.hpp b/src/ATmega_TimerInterrupt.hpp index f18d45c..b7fcae6 100644 --- a/src/ATmega_TimerInterrupt.hpp +++ b/src/ATmega_TimerInterrupt.hpp @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples ****************************************************************************************************************************/ #pragma once @@ -116,13 +117,13 @@ #include "TimerInterrupt_Generic_Debug.h" #ifndef ATMEGA_TIMER_INTERRUPT_VERSION - #define ATMEGA_TIMER_INTERRUPT_VERSION "ATmega_TimerInterrupt v1.1.0" + #define ATMEGA_TIMER_INTERRUPT_VERSION "ATmega_TimerInterrupt v1.1.1" #define ATMEGA_TIMER_INTERRUPT_VERSION_MAJOR 1 #define ATMEGA_TIMER_INTERRUPT_VERSION_MINOR 1 - #define ATMEGA_TIMER_INTERRUPT_VERSION_PATCH 0 + #define ATMEGA_TIMER_INTERRUPT_VERSION_PATCH 1 - #define ATMEGA_TIMER_INTERRUPT_VERSION_INT 1001000 + #define ATMEGA_TIMER_INTERRUPT_VERSION_INT 1001001 #endif #include diff --git a/src/ATmega_TimerInterrupt_Impl.h b/src/ATmega_TimerInterrupt_Impl.h index 17b7313..269bb3e 100644 --- a/src/ATmega_TimerInterrupt_Impl.h +++ b/src/ATmega_TimerInterrupt_Impl.h @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples ****************************************************************************************************************************/ #pragma once @@ -51,8 +52,6 @@ void ATmega_TimerInterrupt::init(int8_t timer) bitWrite(TCCR1B, WGM12, 1); // No scaling now bitWrite(TCCR1B, CS10, 1); - - TISR_LOGWARN(F("T1")); break; #endif @@ -67,8 +66,6 @@ void ATmega_TimerInterrupt::init(int8_t timer) bitWrite(TCCR2A, WGM21, 1); // No scaling now bitWrite(TCCR2B, CS20, 1); - - TISR_LOGWARN(F("T2")); break; #endif @@ -80,8 +77,6 @@ void ATmega_TimerInterrupt::init(int8_t timer) TCCR3B = 0; bitWrite(TCCR3B, WGM32, 1); bitWrite(TCCR3B, CS30, 1); - - TISR_LOGWARN(F("T3")); break; #endif @@ -98,8 +93,6 @@ void ATmega_TimerInterrupt::init(int8_t timer) bitWrite(TCCR4B, CS43, 1); #endif bitWrite(TCCR4B, CS40, 1); - - TISR_LOGWARN(F("T4")); break; #endif @@ -199,9 +192,6 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb if (duration > 0) { _toggle_count = frequency * duration / 1000; - - TISR_LOGWARN1(F("setFrequency => _toggle_count ="), _toggle_count); - TISR_LOGWARN3(F("Frequency ="), frequency, F(", duration ="), duration); if (_toggle_count < 1) { @@ -233,10 +223,6 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb { OCRValue = F_CPU / (frequency * prescalerDiv[prescalerIndex]) - 1; - TISR_LOGWARN1(F("Freq * 1000 ="), frequency * 1000); - TISR_LOGWARN3(F("F_CPU ="), F_CPU, F(", preScalerDiv ="), prescalerDiv[prescalerIndex]); - TISR_LOGWARN3(F("OCR ="), OCRValue, F(", preScalerIndex ="), prescalerIndex); - // We use very large _OCRValue now, and every time timer ISR activates, we deduct min(MAX_COUNT_16BIT, _OCRValueRemaining) from _OCRValueRemaining // So that we can create very long timer, even if the counter is only 16-bit. // Use very high frequency (OCRValue / MAX_COUNT_16BIT) around 16 * 1024 to achieve higher accuracy @@ -246,10 +232,7 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb _OCRValue = OCRValue; _OCRValueRemaining = OCRValue; _prescalerIndex = prescalerIndex; - - TISR_LOGWARN1(F("OK in loop => _OCR ="), _OCRValue); - TISR_LOGWARN3(F("_preScalerIndex ="), _prescalerIndex, F(", preScalerDiv ="), prescalerDiv[_prescalerIndex]); - + isSuccess = true; break; @@ -262,9 +245,6 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb _OCRValue = OCRValue; _OCRValueRemaining = OCRValue; _prescalerIndex = PRESCALER_1024; - - TISR_LOGWARN1(F("OK out loop => _OCR ="), _OCRValue); - TISR_LOGWARN3(F("_preScalerIndex ="), _prescalerIndex, F(", preScalerDiv ="), prescalerDiv[_prescalerIndex]); } } else @@ -284,9 +264,6 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb { OCRValue = F_CPU / (frequency * prescalerDivT2[prescalerIndex]) - 1; - TISR_LOGWARN3(F("F_CPU ="), F_CPU, F(", preScalerDiv ="), prescalerDivT2[prescalerIndex]); - TISR_LOGWARN3(F("OCR2 ="), OCRValue, F(", preScalerIndex ="), prescalerIndex); - // We use very large _OCRValue now, and every time timer ISR activates, we deduct min(MAX_COUNT_8BIT, _OCRValue) from _OCRValue // to create very long timer, even if the counter is only 16-bit. // Use very high frequency (OCRValue / MAX_COUNT_8BIT) around 16 * 1024 to achieve higher accuracy @@ -296,10 +273,7 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb _OCRValueRemaining = OCRValue; // same as prescalarbits _prescalerIndex = prescalerIndex; - - TISR_LOGWARN1(F("OK in loop => _OCR ="), _OCRValue); - TISR_LOGWARN3(F("_preScalerIndex ="), _prescalerIndex, F(", preScalerDiv ="), prescalerDivT2[_prescalerIndex]); - + isSuccess = true; break; @@ -313,9 +287,6 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb _OCRValueRemaining = OCRValue; // same as prescalarbits _prescalerIndex = T2_PRESCALER_1024; - - TISR_LOGWARN1(F("OK out loop => _OCR ="), _OCRValue); - TISR_LOGWARN3(F("_preScalerIndex ="), _prescalerIndex, F(", preScalerDiv ="), prescalerDivT2[_prescalerIndex]); } } @@ -332,9 +303,7 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb #if defined(TCCR2B) if (_timer == 2) { - TCCR2B = (TCCR2B & andMask) | _prescalerIndex; //prescalarbits; - - TISR_LOGWARN1(F("TCCR2B ="), TCCR2B); + TCCR2B = (TCCR2B & andMask) | _prescalerIndex; //prescalarbits; } #endif @@ -343,8 +312,6 @@ bool ATmega_TimerInterrupt::setFrequency(float frequency, timer_callback_p callb else if (_timer == 1) { TCCR1B = (TCCR1B & andMask) | _prescalerIndex; //prescalarbits; - - TISR_LOGWARN1(F("TCCR1B ="), TCCR1B); } #endif @@ -380,8 +347,6 @@ void ATmega_TimerInterrupt::detachInterrupt(void) #if defined(TIMSK1) && defined(OCIE1A) case 1: bitWrite(TIMSK1, OCIE1A, 0); - - TISR_LOGWARN(F("Disable T1")); break; #endif @@ -390,16 +355,12 @@ void ATmega_TimerInterrupt::detachInterrupt(void) #if defined(TIMSK2) && defined(OCIE2A) bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt #endif - - TISR_LOGWARN(F("Disable T2")); break; #if defined(TIMSK3) && defined(OCIE3A) case 3: bitWrite(TIMSK3, OCIE3A, 0); - - TISR_LOGWARN(F("Disable T3")); break; #endif @@ -407,8 +368,6 @@ void ATmega_TimerInterrupt::detachInterrupt(void) #if defined(TIMSK4) && defined(OCIE4A) case 4: bitWrite(TIMSK4, OCIE4A, 0); - - TISR_LOGWARN(F("Disable T4")); break; #endif @@ -439,8 +398,6 @@ void ATmega_TimerInterrupt::reattachInterrupt(unsigned long duration) #if defined(TIMSK1) && defined(OCIE1A) case 1: bitWrite(TIMSK1, OCIE1A, 1); - - TISR_LOGWARN(F("Enable T1")); break; #endif @@ -449,16 +406,12 @@ void ATmega_TimerInterrupt::reattachInterrupt(unsigned long duration) #if defined(TIMSK2) && defined(OCIE2A) bitWrite(TIMSK2, OCIE2A, 1); // enable interrupt #endif - - TISR_LOGWARN(F("Enable T2")); break; #if defined(TIMSK3) && defined(OCIE3A) case 3: bitWrite(TIMSK3, OCIE3A, 1); - - TISR_LOGWARN(F("Enable T3")); break; #endif @@ -466,8 +419,6 @@ void ATmega_TimerInterrupt::reattachInterrupt(unsigned long duration) #if defined(TIMSK4) && defined(OCIE4A) case 4: bitWrite(TIMSK4, OCIE4A, 1); - - TISR_LOGWARN(F("Enable T4")); break; #endif @@ -489,8 +440,6 @@ void ATmega_TimerInterrupt::pauseTimer(void) if (_timer == 2) { TCCR2B = (TCCR2B & andMask); - - TISR_LOGWARN1(F("TCCR2B ="), TCCR2B); } #endif @@ -499,8 +448,6 @@ void ATmega_TimerInterrupt::pauseTimer(void) else if (_timer == 1) { TCCR1B = (TCCR1B & andMask); - - TISR_LOGWARN1(F("TCCR1B ="), TCCR1B); } #endif @@ -526,8 +473,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) if (_timer == 2) { TCCR2B = (TCCR2B & andMask) | _prescalerIndex; //prescalarbits; - - TISR_LOGWARN1(F("TCCR2B ="), TCCR2B); } #endif @@ -536,8 +481,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) else if (_timer == 1) { TCCR1B = (TCCR1B & andMask) | _prescalerIndex; //prescalarbits; - - TISR_LOGWARN1(F("TCCR1B ="), TCCR1B); } #endif @@ -572,9 +515,7 @@ void ATmega_TimerInterrupt::resumeTimer(void) if (countLocal != 0) { if (ITimer1.checkTimerDone()) - { - TISR_LOGDEBUG3(("T1 callback, _OCRValueRemaining ="), ITimer1.get_OCRValueRemaining(), (", millis ="), millis()); - + { ITimer1.callback(); if (ITimer1.get_OCRValue() > MAX_COUNT_16BIT) @@ -596,8 +537,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) } else { - TISR_LOGWARN(("T1 done")); - ITimer1.detachInterrupt(); } } @@ -621,8 +560,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) { if (ITimer2.checkTimerDone()) { - TISR_LOGDEBUG3(("T2 callback, _OCRValueRemaining ="), ITimer2.get_OCRValueRemaining(), (", millis ="), millis()); - ITimer2.callback(); if (ITimer2.get_OCRValue() > MAX_COUNT_8BIT) @@ -644,8 +581,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) } else { - TISR_LOGWARN(("T2 done")); - ITimer2.detachInterrupt(); } } @@ -672,8 +607,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) { if (ITimer3.checkTimerDone()) { - TISR_LOGDEBUG3(("T3 callback, _OCRValueRemaining ="), ITimer3.get_OCRValueRemaining(), (", millis ="), millis()); - ITimer3.callback(); if (ITimer3.get_OCRValue() > MAX_COUNT_16BIT) @@ -695,8 +628,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) } else { - TISR_LOGWARN(("T3 done")); - ITimer3.detachInterrupt(); } } @@ -725,8 +656,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) { if (ITimer4.checkTimerDone()) { - TISR_LOGDEBUG3(("T4 callback, _OCRValueRemaining ="), ITimer4.get_OCRValueRemaining(), (", millis ="), millis()); - ITimer4.callback(); if (ITimer4.get_OCRValue() > MAX_COUNT_16BIT) @@ -748,8 +677,6 @@ void ATmega_TimerInterrupt::resumeTimer(void) } else { - TISR_LOGWARN(("T4 done")); - ITimer4.detachInterrupt(); } } diff --git a/src/ISR_Timer.h b/src/ISR_Timer.h index 3b5e3fe..671e385 100644 --- a/src/ISR_Timer.h +++ b/src/ISR_Timer.h @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples *****************************************************************************************************************************/ #pragma once @@ -26,6 +27,6 @@ #define ISR_TIMER_H #include "ISR_Timer.hpp" -#include "ISR_Timer-Impl.h" +#include "ISR_Timer_Impl.h" #endif diff --git a/src/ISR_Timer.hpp b/src/ISR_Timer.hpp index 3ca7797..55684cd 100644 --- a/src/ISR_Timer.hpp +++ b/src/ISR_Timer.hpp @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples *****************************************************************************************************************************/ #pragma once @@ -26,13 +27,13 @@ #define ISR_TIMER_HPP #ifndef ATMEGA_TIMER_INTERRUPT_VERSION - #define ATMEGA_TIMER_INTERRUPT_VERSION "ATmega_TimerInterrupt v1.0.0" + #define ATMEGA_TIMER_INTERRUPT_VERSION "ATmega_TimerInterrupt v1.1.1" #define ATMEGA_TIMER_INTERRUPT_VERSION_MAJOR 1 - #define ATMEGA_TIMER_INTERRUPT_VERSION_MINOR 0 - #define ATMEGA_TIMER_INTERRUPT_VERSION_PATCH 0 + #define ATMEGA_TIMER_INTERRUPT_VERSION_MINOR 1 + #define ATMEGA_TIMER_INTERRUPT_VERSION_PATCH 1 - #define ATMEGA_TIMER_INTERRUPT_VERSION_INT 1000000 + #define ATMEGA_TIMER_INTERRUPT_VERSION_INT 1001001 #endif #include "TimerInterrupt_Generic_Debug.h" diff --git a/src/ISR_Timer_Impl.h b/src/ISR_Timer_Impl.h index b99644b..15c7f40 100644 --- a/src/ISR_Timer_Impl.h +++ b/src/ISR_Timer_Impl.h @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples *****************************************************************************************************************************/ #pragma once diff --git a/src/TimerInterrupt_Generic_Debug.h b/src/TimerInterrupt_Generic_Debug.h index 91b8058..f46d261 100644 --- a/src/TimerInterrupt_Generic_Debug.h +++ b/src/TimerInterrupt_Generic_Debug.h @@ -12,12 +12,13 @@ Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks. - Version: 1.1.0 + Version: 1.1.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2022 Initial coding for AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore 1.1.0 K Hoang 22/08/2022 Fix missing code for Timer3 and Timer4 + 1.1.1 K Hoang 27/08/2022 Fix bug and optimize code in examples *****************************************************************************************************************************/ #pragma once