|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Argument_Complex.ino |
| 3 | + For Arduino AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/ATtiny_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 | + |
| 16 | +// Important Note: To use drag-and-drop into CURIOSITY virtual drive if you can program via Arduino IDE |
| 17 | +// For example, check https://ww1.microchip.com/downloads/en/DeviceDoc/40002193A.pdf |
| 18 | + |
| 19 | +#if !( defined(MEGATINYCORE) ) |
| 20 | + #error This is designed only for MEGATINYCORE megaAVR board! Please check your Tools->Board setting |
| 21 | +#endif |
| 22 | + |
| 23 | +// These define's must be placed at the beginning before #include "megaAVR_TimerInterrupt.h" |
| 24 | +// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 |
| 25 | +// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. |
| 26 | +#define TIMER_INTERRUPT_DEBUG 0 |
| 27 | +#define _TIMERINTERRUPT_LOGLEVEL_ 0 |
| 28 | + |
| 29 | +// Select USING_FULL_CLOCK == true for 20/16MHz to Timer TCBx => shorter timer, but better accuracy |
| 30 | +// Select USING_HALF_CLOCK == true for 10/ 8MHz to Timer TCBx => shorter timer, but better accuracy |
| 31 | +// Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy |
| 32 | +// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy |
| 33 | +#define USING_FULL_CLOCK true |
| 34 | +#define USING_HALF_CLOCK false |
| 35 | +#define USING_250KHZ false // Not supported now |
| 36 | + |
| 37 | +// Try to use RTC, TCA0 or TCD0 for millis() |
| 38 | +#define USE_TIMER_0 false // Check if used by millis(), Servo or tone() |
| 39 | +#define USE_TIMER_1 true // Check if used by millis(), Servo or tone() |
| 40 | + |
| 41 | +#if USE_TIMER_0 |
| 42 | + #define CurrentTimer ITimer0 |
| 43 | +#elif USE_TIMER_1 |
| 44 | + #define CurrentTimer ITimer1 |
| 45 | +#else |
| 46 | + #error You must select one Timer |
| 47 | +#endif |
| 48 | + |
| 49 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
| 50 | +#include "ATtiny_TimerInterrupt.h" |
| 51 | + |
| 52 | +#ifdef LED_BUILTIN |
| 53 | + #undef LED_BUILTIN |
| 54 | + |
| 55 | + // To modify according to your board |
| 56 | + // For Curiosity Nano ATtiny3217 => PIN_PA3 |
| 57 | + #if defined(ARDUINO_AVR_CuriosityNano3217) |
| 58 | + #define LED_BUILTIN PIN_PA3 |
| 59 | + #else |
| 60 | + // standard Arduino pin 13 |
| 61 | + #define LED_BUILTIN PIN_PA3 |
| 62 | + #endif |
| 63 | +#endif |
| 64 | + |
| 65 | +// standard Serial |
| 66 | +#define SerialDebug Serial |
| 67 | + |
| 68 | + |
| 69 | +struct pinStruct |
| 70 | +{ |
| 71 | + unsigned int Pin1; |
| 72 | + unsigned int Pin2; |
| 73 | + unsigned int Pin3; |
| 74 | +}; |
| 75 | + |
| 76 | +volatile pinStruct myOutputPins = { LED_BUILTIN, A0, A1 }; |
| 77 | + |
| 78 | +void TimerHandler1(unsigned int outputPinsAddress) |
| 79 | +{ |
| 80 | + static bool toggle = false; |
| 81 | + |
| 82 | + //timer interrupt toggles pins |
| 83 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 84 | + SerialDebug.print("Toggle pin1 = "); SerialDebug.println( ((pinStruct *) outputPinsAddress)->Pin1 ); |
| 85 | +#endif |
| 86 | + |
| 87 | + digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle); |
| 88 | + |
| 89 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 90 | + SerialDebug.print("Read pin2 A0 ("); SerialDebug.print(((pinStruct *) outputPinsAddress)->Pin2 ); |
| 91 | + SerialDebug.print(") = "); |
| 92 | + SerialDebug.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" ); |
| 93 | + |
| 94 | + SerialDebug.print("Read pin3 A1 ("); SerialDebug.print(((pinStruct *) outputPinsAddress)->Pin3 ); |
| 95 | + SerialDebug.print(") = "); |
| 96 | + SerialDebug.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" ); |
| 97 | +#endif |
| 98 | + |
| 99 | + toggle = !toggle; |
| 100 | +} |
| 101 | + |
| 102 | +#define TIMER1_INTERVAL_MS 1000 |
| 103 | + |
| 104 | +void setup() |
| 105 | +{ |
| 106 | + pinMode(myOutputPins.Pin1, OUTPUT); |
| 107 | + pinMode(myOutputPins.Pin2, INPUT_PULLUP); |
| 108 | + pinMode(myOutputPins.Pin3, INPUT_PULLUP); |
| 109 | + |
| 110 | + SerialDebug.begin(115200); |
| 111 | + while (!SerialDebug && millis() < 5000); |
| 112 | + |
| 113 | + SerialDebug.print(F("\nStarting Argument_Complex on ")); SerialDebug.println(BOARD_NAME); |
| 114 | + SerialDebug.println(AT_TINY_TIMER_INTERRUPT_VERSION); |
| 115 | + SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz")); |
| 116 | + |
| 117 | + SerialDebug.print(F("TCB Clock Frequency = ")); |
| 118 | + |
| 119 | +#if USING_FULL_CLOCK |
| 120 | + SerialDebug.println(F("Full clock (20/16MHz, etc) for highest accuracy")); |
| 121 | +#elif USING_HALF_CLOCK |
| 122 | + SerialDebug.println(F("Half clock (10/8MHz, etc.) for high accuracy")); |
| 123 | +#else |
| 124 | + SerialDebug.println(F("250KHz for lower accuracy but longer time")); |
| 125 | +#endif |
| 126 | + |
| 127 | + // Timer TCB2 is used for micros(), millis(), delay(), etc and can't be used |
| 128 | + CurrentTimer.init(); |
| 129 | + |
| 130 | + if (CurrentTimer.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, (unsigned int) &myOutputPins)) |
| 131 | + { |
| 132 | + SerialDebug.print(F("Starting ITimer OK, millis() = ")); SerialDebug.println(millis()); |
| 133 | + } |
| 134 | + else |
| 135 | + SerialDebug.println(F("Can't set ITimer. Select another freq. or timer")); |
| 136 | +} |
| 137 | + |
| 138 | +void loop() |
| 139 | +{ |
| 140 | +} |
0 commit comments