|
17 | 17 | defined(ARDUINO_AVR_ATmega4809) || defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) || \
|
18 | 18 | defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) || defined(ARDUINO_AVR_ATmega1608) || \
|
19 | 19 | defined(ARDUINO_AVR_ATmega809) || defined(ARDUINO_AVR_ATmega808) )
|
20 |
| - #error This is designed only for Arduino or MegaCoreX megaAVR board! Please check your Tools->Board setting |
| 20 | +#error This is designed only for Arduino or MegaCoreX megaAVR board! Please check your Tools->Board setting |
21 | 21 | #endif
|
22 | 22 |
|
23 | 23 | // These define's must be placed at the beginning before #include "megaAVR_TimerInterrupt.h"
|
|
43 | 43 | #include "megaAVR_TimerInterrupt.h"
|
44 | 44 |
|
45 | 45 | #if !defined(LED_BUILTIN)
|
46 |
| - #define LED_BUILTIN 13 |
| 46 | + #define LED_BUILTIN 13 |
47 | 47 | #endif
|
48 | 48 |
|
49 | 49 | struct pinStruct
|
50 | 50 | {
|
51 |
| - unsigned int Pin1; |
52 |
| - unsigned int Pin2; |
53 |
| - unsigned int Pin3; |
| 51 | + unsigned int Pin1; |
| 52 | + unsigned int Pin2; |
| 53 | + unsigned int Pin3; |
54 | 54 | };
|
55 | 55 |
|
56 | 56 | volatile pinStruct myOutputPins = { LED_BUILTIN, A0, A1 };
|
57 | 57 |
|
58 | 58 | void TimerHandler1(unsigned int outputPinsAddress)
|
59 | 59 | {
|
60 |
| - static bool toggle1 = false; |
61 |
| - static bool started = false; |
62 |
| - |
63 |
| - if (!started) |
64 |
| - { |
65 |
| - started = true; |
66 |
| - pinMode(((pinStruct *) outputPinsAddress)->Pin1, OUTPUT); |
67 |
| - pinMode(((pinStruct *) outputPinsAddress)->Pin2, INPUT_PULLUP); |
68 |
| - pinMode(((pinStruct *) outputPinsAddress)->Pin3, INPUT_PULLUP); |
69 |
| - } |
70 |
| - |
71 |
| - //timer interrupt toggles pins |
| 60 | + static bool toggle1 = false; |
| 61 | + static bool started = false; |
| 62 | + |
| 63 | + if (!started) |
| 64 | + { |
| 65 | + started = true; |
| 66 | + pinMode(((pinStruct *) outputPinsAddress)->Pin1, OUTPUT); |
| 67 | + pinMode(((pinStruct *) outputPinsAddress)->Pin2, INPUT_PULLUP); |
| 68 | + pinMode(((pinStruct *) outputPinsAddress)->Pin3, INPUT_PULLUP); |
| 69 | + } |
| 70 | + |
| 71 | + //timer interrupt toggles pins |
72 | 72 | #if (TIMER_INTERRUPT_DEBUG > 1)
|
73 |
| - Serial.print("Toggle pin1 = "); Serial.println( ((pinStruct *) outputPinsAddress)->Pin1 ); |
| 73 | + Serial.print("Toggle pin1 = "); |
| 74 | + Serial.println( ((pinStruct *) outputPinsAddress)->Pin1 ); |
74 | 75 | #endif
|
75 |
| - |
76 |
| - digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle1); |
77 | 76 |
|
78 |
| -#if (TIMER_INTERRUPT_DEBUG > 1) |
79 |
| - Serial.print("Read pin2 A0 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin2 ); |
80 |
| - Serial.print(") = "); |
81 |
| - Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" ); |
| 77 | + digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle1); |
82 | 78 |
|
83 |
| - Serial.print("Read pin3 A1 ("); Serial.print(((pinStruct *) outputPinsAddress)->Pin3 ); |
84 |
| - Serial.print(") = "); |
85 |
| - Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" ); |
| 79 | +#if (TIMER_INTERRUPT_DEBUG > 1) |
| 80 | + Serial.print("Read pin2 A0 ("); |
| 81 | + Serial.print(((pinStruct *) outputPinsAddress)->Pin2 ); |
| 82 | + Serial.print(") = "); |
| 83 | + Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" ); |
| 84 | + |
| 85 | + Serial.print("Read pin3 A1 ("); |
| 86 | + Serial.print(((pinStruct *) outputPinsAddress)->Pin3 ); |
| 87 | + Serial.print(") = "); |
| 88 | + Serial.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" ); |
86 | 89 | #endif
|
87 |
| - |
88 |
| - toggle1 = !toggle1; |
| 90 | + |
| 91 | + toggle1 = !toggle1; |
89 | 92 | }
|
90 | 93 |
|
91 | 94 | #define TIMER1_INTERVAL_MS 5000
|
92 | 95 |
|
93 | 96 | void setup()
|
94 | 97 | {
|
95 |
| - Serial.begin(115200); |
96 |
| - while (!Serial); |
| 98 | + Serial.begin(115200); |
| 99 | + |
| 100 | + while (!Serial); |
97 | 101 |
|
98 |
| - Serial.print(F("\nStarting Argument_Complex on ")); |
99 |
| - Serial.println(BOARD_NAME); |
100 |
| - Serial.println(MEGA_AVR_TIMER_INTERRUPT_VERSION); |
101 |
| - Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz")); |
| 102 | + Serial.print(F("\nStarting Argument_Complex on ")); |
| 103 | + Serial.println(BOARD_NAME); |
| 104 | + Serial.println(MEGA_AVR_TIMER_INTERRUPT_VERSION); |
| 105 | + Serial.print(F("CPU Frequency = ")); |
| 106 | + Serial.print(F_CPU / 1000000); |
| 107 | + Serial.println(F(" MHz")); |
102 | 108 |
|
103 |
| - Serial.print(F("TCB Clock Frequency = ")); |
| 109 | + Serial.print(F("TCB Clock Frequency = ")); |
104 | 110 |
|
105 |
| -#if USING_16MHZ |
106 |
| - Serial.println(F("16MHz for highest accuracy")); |
107 |
| -#elif USING_8MHZ |
108 |
| - Serial.println(F("8MHz for very high accuracy")); |
| 111 | +#if USING_16MHZ |
| 112 | + Serial.println(F("16MHz for highest accuracy")); |
| 113 | +#elif USING_8MHZ |
| 114 | + Serial.println(F("8MHz for very high accuracy")); |
109 | 115 | #else
|
110 |
| - Serial.println(F("250KHz for lower accuracy but longer time")); |
| 116 | + Serial.println(F("250KHz for lower accuracy but longer time")); |
111 | 117 | #endif
|
112 | 118 |
|
113 |
| - // Timer0 is used for micros(), millis(), delay(), etc and can't be used |
114 |
| - // Select Timer 1-2 for UNO, 0-5 for MEGA |
115 |
| - // Timer 2 is 8-bit timer, only for higher frequency |
| 119 | + // Timer0 is used for micros(), millis(), delay(), etc and can't be used |
| 120 | + // Select Timer 1-2 for UNO, 0-5 for MEGA |
| 121 | + // Timer 2 is 8-bit timer, only for higher frequency |
116 | 122 |
|
117 |
| - ITimer1.init(); |
| 123 | + ITimer1.init(); |
118 | 124 |
|
119 |
| - // Using ATmega328 used in UNO => 16MHz CPU clock , |
120 |
| - // For 16-bit timer 1, 3, 4 and 5, set frequency from 0.2385 to some KHz |
121 |
| - // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz |
| 125 | + // Using ATmega328 used in UNO => 16MHz CPU clock , |
| 126 | + // For 16-bit timer 1, 3, 4 and 5, set frequency from 0.2385 to some KHz |
| 127 | + // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz |
122 | 128 |
|
123 |
| - if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, (unsigned int) &myOutputPins)) |
124 |
| - { |
125 |
| - Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); |
126 |
| - } |
127 |
| - else |
128 |
| - Serial.println(F("Can't set ITimer1. Select another freq. or timer")); |
| 129 | + if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, (unsigned int) &myOutputPins)) |
| 130 | + { |
| 131 | + Serial.print(F("Starting ITimer1 OK, millis() = ")); |
| 132 | + Serial.println(millis()); |
| 133 | + } |
| 134 | + else |
| 135 | + Serial.println(F("Can't set ITimer1. Select another freq. or timer")); |
129 | 136 | }
|
130 | 137 |
|
131 | 138 | void loop()
|
|
0 commit comments