|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Argument_Complex.ino |
| 3 | + For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. ) |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic |
| 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 | + |
| 16 | +// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h" |
| 17 | +// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 |
| 18 | +// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. |
| 19 | +#define TIMER_INTERRUPT_DEBUG 0 |
| 20 | +#define _TIMERINTERRUPT_LOGLEVEL_ 0 |
| 21 | + |
| 22 | +// Select USING_16MHZ == true for 16MHz to Timer TCBx => shorter timer, but better accuracy |
| 23 | +// Select USING_8MHZ == true for 8MHz to Timer TCBx => shorter timer, but better accuracy |
| 24 | +// Select USING_250KHZ == true for 250KHz to Timer TCBx => shorter timer, but better accuracy |
| 25 | +// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy |
| 26 | +#define USING_16MHZ true |
| 27 | +#define USING_8MHZ false |
| 28 | +#define USING_250KHZ false |
| 29 | + |
| 30 | +#define USE_TIMER_0 false |
| 31 | +#define USE_TIMER_1 true |
| 32 | +#define USE_TIMER_2 false |
| 33 | +#define USE_TIMER_3 false |
| 34 | + |
| 35 | +#include "TimerInterrupt_Generic.h" |
| 36 | + |
| 37 | +#if !defined(LED_BUILTIN) |
| 38 | + #define LED_BUILTIN 13 |
| 39 | +#endif |
| 40 | + |
| 41 | +struct pinStruct |
| 42 | +{ |
| 43 | + unsigned int Pin1; |
| 44 | + unsigned int Pin2; |
| 45 | + unsigned int Pin3; |
| 46 | +}; |
| 47 | + |
| 48 | +volatile pinStruct myOutputPins = { LED_BUILTIN, A0, A1 }; |
| 49 | + |
| 50 | +void TimerHandler1(unsigned int outputPinsAddress) |
| 51 | +{ |
| 52 | + static bool toggle1 = false; |
| 53 | + static bool started = false; |
| 54 | + |
| 55 | + if (!started) |
| 56 | + { |
| 57 | + started = true; |
| 58 | + pinMode(((pinStruct *) outputPinsAddress)->Pin1, OUTPUT); |
| 59 | + pinMode(((pinStruct *) outputPinsAddress)->Pin2, INPUT_PULLUP); |
| 60 | + pinMode(((pinStruct *) outputPinsAddress)->Pin3, INPUT_PULLUP); |
| 61 | + } |
| 62 | + |
| 63 | + //timer interrupt toggles pins |
| 64 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 65 | + Serial.print("Toggle pin1 = "); Serial.println( ((pinStruct *) outputPinsAddress)->Pin1 ); |
| 66 | +#endif |
| 67 | + |
| 68 | + digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle1); |
| 69 | + |
| 70 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 71 | + Serial.print("Read pin2 A0 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin2 ); |
| 72 | + Serial.print(") = "); |
| 73 | + Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" ); |
| 74 | + |
| 75 | + Serial.print("Read pin3 A1 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin3 ); |
| 76 | + Serial.print(") = "); |
| 77 | + Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" ); |
| 78 | +#endif |
| 79 | + |
| 80 | + toggle1 = !toggle1; |
| 81 | +} |
| 82 | + |
| 83 | +#define TIMER1_INTERVAL_MS 5000 |
| 84 | + |
| 85 | +void setup() |
| 86 | +{ |
| 87 | + Serial.begin(115200); |
| 88 | + while (!Serial); |
| 89 | + |
| 90 | + Serial.print(F("\nStarting Argument_Complex on ")); |
| 91 | + Serial.println(BOARD_NAME); |
| 92 | + Serial.println(MEGA_AVR_TIMER_INTERRUPT_VERSION); |
| 93 | + |
| 94 | + Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz")); |
| 95 | + |
| 96 | + Serial.print(F("TCB Clock Frequency = ")); |
| 97 | + |
| 98 | +#if USING_16MHZ |
| 99 | + Serial.println(F("16MHz for highest accuracy")); |
| 100 | +#elif USING_8MHZ |
| 101 | + Serial.println(F("8MHz for very high accuracy")); |
| 102 | +#else |
| 103 | + Serial.println(F("250KHz for lower accuracy but longer time")); |
| 104 | +#endif |
| 105 | + |
| 106 | + // Timer0 is used for micros(), millis(), delay(), etc and can't be used |
| 107 | + // Select Timer 1-2 for UNO, 0-5 for MEGA |
| 108 | + // Timer 2 is 8-bit timer, only for higher frequency |
| 109 | + |
| 110 | + ITimer1.init(); |
| 111 | + |
| 112 | + // Using ATmega328 used in UNO => 16MHz CPU clock , |
| 113 | + // For 16-bit timer 1, 3, 4 and 5, set frequency from 0.2385 to some KHz |
| 114 | + // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz |
| 115 | + |
| 116 | + if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, (unsigned int) &myOutputPins)) |
| 117 | + { |
| 118 | + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); |
| 119 | + } |
| 120 | + else |
| 121 | + Serial.println(F("Can't set ITimer1. Select another freq. or timer")); |
| 122 | +} |
| 123 | + |
| 124 | +void loop() |
| 125 | +{ |
| 126 | +} |
0 commit comments