|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Argument_Complex.ino |
| 3 | + For AVR ATmega164, ATmega324, ATmega644, ATmega1284 with MightyCore |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/ATmega_TimerInterrupt |
| 7 | + Licensed under MIT license |
| 8 | +
|
| 9 | + Now with we can use these new 16 ISR-based timers, while consuming only 1 hwarware Timer. |
| 10 | + Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds) |
| 11 | + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
| 12 | + Therefore, their executions are not blocked by bad-behaving functions / tasks. |
| 13 | + This important feature is absolutely necessary for mission-critical tasks. |
| 14 | +
|
| 15 | + Notes: |
| 16 | + Special design is necessary to share data between interrupt code and the rest of your program. |
| 17 | + Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
| 18 | + variable can not spontaneously change. Because your function may change variables while your program is using them, |
| 19 | + the compiler needs this hint. But volatile alone is often not enough. |
| 20 | + When accessing shared variables, usually interrupts must be disabled. Even with volatile, |
| 21 | + if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. |
| 22 | + If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled |
| 23 | + or the entire sequence of your code which accesses the data. |
| 24 | +*****************************************************************************************************************************/ |
| 25 | + |
| 26 | +// These define's must be placed at the beginning before #include "TimerInterrupt.h" |
| 27 | +// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 |
| 28 | +// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. |
| 29 | +#define TIMER_INTERRUPT_DEBUG 0 |
| 30 | +#define _TIMERINTERRUPT_LOGLEVEL_ 0 |
| 31 | + |
| 32 | +// Select just 1 TIMER to be true |
| 33 | +#define USE_TIMER_1 true |
| 34 | +#define USE_TIMER_2 false |
| 35 | +// TIMER_3 Only valid for ATmega1284 and ATmega324PB (not ready in core yet) |
| 36 | +#define USE_TIMER_3 false |
| 37 | +// TIMER_4 Only valid for ATmega324PB, not ready in core yet |
| 38 | +#define USE_TIMER_4 false |
| 39 | + |
| 40 | +#if (USE_TIMER_1) |
| 41 | + #warning Using Timer1 |
| 42 | +#elif (USE_TIMER_2) |
| 43 | + #warning Using Timer2 |
| 44 | +#elif (USE_TIMER_3) |
| 45 | + #warning Using Timer3 |
| 46 | +#elif (USE_TIMER_4) |
| 47 | + #warning Using Timer4 |
| 48 | +#endif |
| 49 | + |
| 50 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
| 51 | +#include "ATmega_TimerInterrupt.h" |
| 52 | + |
| 53 | +#if !defined(LED_BUILTIN) |
| 54 | + #define LED_BUILTIN 13 |
| 55 | +#endif |
| 56 | + |
| 57 | +struct pinStruct |
| 58 | +{ |
| 59 | + unsigned int Pin1; |
| 60 | + unsigned int Pin2; |
| 61 | + unsigned int Pin3; |
| 62 | +}; |
| 63 | + |
| 64 | +volatile pinStruct myOutputPins = { LED_BUILTIN, A0, A1 }; |
| 65 | + |
| 66 | +void TimerHandler(unsigned int outputPinsAddress) |
| 67 | +{ |
| 68 | + static bool toggle = false; |
| 69 | + |
| 70 | + //timer interrupt toggles pins |
| 71 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 72 | + Serial.print("Toggle pin1 = "); Serial.println( ((pinStruct *) outputPinsAddress)->Pin1 ); |
| 73 | +#endif |
| 74 | + |
| 75 | + digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle); |
| 76 | + |
| 77 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 78 | + Serial.print("Read pin2 A0 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin2 ); |
| 79 | + Serial.print(") = "); |
| 80 | + Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" ); |
| 81 | + |
| 82 | + Serial.print("Read pin3 A1 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin3 ); |
| 83 | + Serial.print(") = "); |
| 84 | + Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" ); |
| 85 | +#endif |
| 86 | + |
| 87 | + toggle = !toggle; |
| 88 | +} |
| 89 | + |
| 90 | +#define TIMER_INTERVAL_MS 1000 |
| 91 | + |
| 92 | +void setup() |
| 93 | +{ |
| 94 | + pinMode(myOutputPins.Pin1, OUTPUT); |
| 95 | + pinMode(myOutputPins.Pin2, OUTPUT); |
| 96 | + pinMode(myOutputPins.Pin3, OUTPUT); |
| 97 | + |
| 98 | + Serial.begin(115200); |
| 99 | + while (!Serial && millis() < 5000); |
| 100 | + |
| 101 | + Serial.print(F("\nStarting Argument_Complex on ")); Serial.println(BOARD_TYPE); |
| 102 | + Serial.println(ATMEGA_TIMER_INTERRUPT_VERSION); |
| 103 | + Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz")); |
| 104 | + |
| 105 | + // Timer0 is already used for micros(), millis(), delay(), etc and can't be used |
| 106 | + // Select Timer 1-2 |
| 107 | + // Timer 2 is 8-bit timer, only for higher frequency |
| 108 | + |
| 109 | +#if USE_TIMER_1 |
| 110 | + |
| 111 | + ITimer1.init(); |
| 112 | + |
| 113 | + // Using ATmega324 with 16MHz CPU clock , |
| 114 | + // For 16-bit timer 1, set frequency from 0.2385 to some KHz |
| 115 | + // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz |
| 116 | + |
| 117 | + if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, (unsigned int) &myOutputPins)) |
| 118 | + { |
| 119 | + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); |
| 120 | + } |
| 121 | + else |
| 122 | + Serial.println(F("Can't set ITimer1. Select another freq. or timer")); |
| 123 | + |
| 124 | +#elif USE_TIMER_2 |
| 125 | + |
| 126 | + // Using ATmega324 with 16MHz CPU clock , |
| 127 | + // For 16-bit timer 1, set frequency from 0.2385 to some KHz |
| 128 | + // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz |
| 129 | + ITimer2.init(); |
| 130 | + |
| 131 | + if (ITimer2.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler)) |
| 132 | + { |
| 133 | + Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); |
| 134 | + } |
| 135 | + else |
| 136 | + Serial.println(F("Can't set ITimer2. Select another freq. or timer")); |
| 137 | + |
| 138 | +#elif USE_TIMER_3 |
| 139 | + |
| 140 | + ITimer3.init(); |
| 141 | + |
| 142 | + if (ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler, outputPin)) |
| 143 | + { |
| 144 | + Serial.print(F("Starting ITimer3 OK, millis() = ")); Serial.println(millis()); |
| 145 | + |
| 146 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 147 | + Serial.print(F("OutputPin = ")); Serial.print(outputPin); |
| 148 | + Serial.print(F(" address: ")); Serial.println((uint32_t) &outputPin ); |
| 149 | +#endif |
| 150 | + } |
| 151 | + else |
| 152 | + Serial.println(F("Can't set ITimer3. Select another freq. or timer")); |
| 153 | + |
| 154 | +#endif |
| 155 | +} |
| 156 | + |
| 157 | +void loop() |
| 158 | +{ |
| 159 | +} |
0 commit comments