27
27
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
28
28
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
29
29
#define TIMER_INTERRUPT_DEBUG 0
30
- #define _TIMERINTERRUPT_LOGLEVEL_ 3
30
+ #define _TIMERINTERRUPT_LOGLEVEL_ 4
31
31
32
32
#define USE_TIMER_1 true
33
33
38
38
// TIMER_4 Only valid for ATmega324PB, not ready in core yet
39
39
#define USE_TIMER_4 false
40
40
41
- #if (USE_TIMER_2)
42
- #warning Using Timer1 and Timer2
43
- #elif (USE_TIMER_3)
44
- #warning Using Timer1 and Timer3
45
- #elif (USE_TIMER_4)
46
- #warning Using Timer1 and Timer4
41
+ #if USE_TIMER_2
42
+ #define CurrentTimer ITimer2
43
+ #elif USE_TIMER_3
44
+ #define CurrentTimer ITimer3
45
+ #elif USE_TIMER_4
46
+ #define CurrentTimer ITimer4
47
+ #else
48
+ #error You must select one Timer
49
+ #endif
50
+
51
+ #if (_TIMERINTERRUPT_LOGLEVEL_ > 3)
52
+ #if (USE_TIMER_2)
53
+ #warning Using Timer1 and Timer2
54
+ #elif (USE_TIMER_3)
55
+ #warning Using Timer1 and Timer3
56
+ #elif (USE_TIMER_4)
57
+ #warning Using Timer1 and Timer4
58
+ #endif
47
59
#endif
48
60
49
61
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
@@ -109,7 +121,9 @@ void setup()
109
121
// Timer0 is already used for micros(), millis(), delay(), etc and can't be used
110
122
// Select Timer 1-2
111
123
// Timer 2 is 8-bit timer, only for higher frequency
112
-
124
+
125
+ #if USE_TIMER_1
126
+
113
127
ITimer1.init ();
114
128
115
129
// Using ATmega324 with 16MHz CPU clock ,
@@ -118,7 +132,7 @@ void setup()
118
132
119
133
if (ITimer1.attachInterruptInterval (TIMER1_INTERVAL_MS, TimerHandler1, outputPin1))
120
134
{
121
- Serial.print (F (" Starting ITimer1 OK, millis() = " )); Serial.println (millis ());
135
+ Serial.print (F (" Starting ITimer1 OK, millis() = " )); Serial.println (millis ());
122
136
123
137
#if (TIMER_INTERRUPT_DEBUG > 1)
124
138
Serial.print (F (" OutputPin1 = " )); Serial.print (outputPin1);
@@ -127,40 +141,25 @@ void setup()
127
141
}
128
142
else
129
143
Serial.println (F (" Can't set ITimer1. Select another freq. or timer" ));
144
+
145
+ #endif
130
146
131
- # if USE_TIMER_2
147
+ // /////////////////////////////////////////////
132
148
133
- ITimer2.init ();
134
-
135
- if (ITimer2.attachInterruptInterval (TIMER_INTERVAL_MS, TimerHandler, outputPin))
136
- {
137
- Serial.print (F (" Starting ITimer2 OK, millis() = " )); Serial.println (millis ());
149
+ // Init second timer
150
+ CurrentTimer.init ();
138
151
139
- #if (TIMER_INTERRUPT_DEBUG > 1)
140
- Serial.print (F (" OutputPin = " )); Serial.print (outputPin);
141
- Serial.print (F (" address: " )); Serial.println ((uint32_t ) &outputPin );
142
- #endif
143
- }
144
- else
145
- Serial.println (F (" Can't set ITimer2. Select another freq. or timer" ));
146
-
147
- #elif USE_TIMER_3
148
-
149
- ITimer3.init ();
150
-
151
- if (ITimer3.attachInterruptInterval (TIMER_INTERVAL_MS, TimerHandler, outputPin))
152
+ if (CurrentTimer.attachInterruptInterval (TIMER_INTERVAL_MS, TimerHandler, outputPin))
152
153
{
153
- Serial.print (F (" Starting ITimer3 OK, millis() = " )); Serial.println (millis ());
154
+ Serial.print (F (" Starting ITimer OK, millis() = " )); Serial.println (millis ());
154
155
155
156
#if (TIMER_INTERRUPT_DEBUG > 1)
156
157
Serial.print (F (" OutputPin = " )); Serial.print (outputPin);
157
158
Serial.print (F (" address: " )); Serial.println ((uint32_t ) &outputPin );
158
159
#endif
159
160
}
160
161
else
161
- Serial.println (F (" Can't set ITimer3. Select another freq. or timer" ));
162
-
163
- #endif
162
+ Serial.println (F (" Can't set ITimer. Select another freq. or timer" ));
164
163
}
165
164
166
165
void loop ()
0 commit comments