3
3
For SAMD boards
4
4
Written by Khoi Hoang
5
5
6
- Built by Khoi Hoang https://github.com/khoih-prog/SAMD_TimerInterrupt
6
+ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
7
7
Licensed under MIT license
8
8
9
9
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
36
36
#error This code is designed to run on SAMD21/SAMD51 platform! Please check your Tools->Board setting.
37
37
#endif
38
38
39
+ // ///////////////////////////////////////////////////////////////
40
+
39
41
// These define's must be placed at the beginning before #include "SAMDTimerInterrupt.h"
40
42
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
41
43
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
42
44
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
43
45
#define TIMER_INTERRUPT_DEBUG 0
44
- #define _TIMERINTERRUPT_LOGLEVEL_ 0
46
+ #define _TIMERINTERRUPT_LOGLEVEL_ 1
47
+
48
+ // Select only one to be true for SAMD21. Must must be placed at the beginning before #include "SAMDTimerInterrupt.h"
49
+ #define USING_TIMER_TC3 true // Only TC3 can be used for SAMD51
50
+ #define USING_TIMER_TC4 false // Not to use with Servo library
51
+ #define USING_TIMER_TC5 false
52
+ #define USING_TIMER_TCC false
53
+ #define USING_TIMER_TCC1 false
54
+ #define USING_TIMER_TCC2 false // Don't use this, can crash on some boards
55
+
56
+ // Uncomment To test if conflict with Servo library
57
+ // #include "Servo.h"
58
+
59
+ // ///////////////////////////////////////////////////////////////
45
60
46
61
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
47
62
#include " SAMDTimerInterrupt.h"
50
65
#define LED_BUILTIN 13
51
66
#endif
52
67
53
- #ifndef LED_BLUE
54
- #define LED_BLUE 2
55
- #endif
56
-
57
- #ifndef LED_RED
58
- #define LED_RED 8
59
- #endif
60
-
61
- #define TIMER0_INTERVAL_MS 500 // 1000
62
-
63
- volatile uint32_t preMillisTimer0 = 0 ;
64
-
65
- // Depending on the board, you can select SAMD21 Hardware Timer from TC3, TC4, TC5, TCC, TCC1 or TCC2
66
- // SAMD51 Hardware Timer only TC3
67
-
68
- // Init SAMD timer TIMER_TC3
69
- SAMDTimer ITimer0 (TIMER_TC3);
68
+ volatile uint32_t preMillisTimer = 0 ;
70
69
71
70
// ////////////////////////////////////////////
72
71
73
- void TimerHandler0 ()
74
- {
75
- static bool toggle0 = false ;
76
- static bool started = false ;
77
-
78
- if (!started)
79
- {
80
- started = true ;
81
- pinMode (LED_BUILTIN, OUTPUT);
82
- }
83
-
84
- #if (TIMER_INTERRUPT_DEBUG > 0)
85
- static uint32_t curMillis = 0 ;
86
-
87
- curMillis = millis ();
88
-
89
- if (curMillis > TIMER0_INTERVAL_MS)
90
- {
91
- Serial.print (F (" ITimer0: millis() = " )); Serial.print (curMillis);
92
- Serial.print (F (" , delta = " )); Serial.println (curMillis - preMillisTimer0);
93
- }
94
-
95
- preMillisTimer0 = curMillis;
96
- #endif
97
-
98
- // timer interrupt toggles pin LED_BUILTIN
99
- digitalWrite (LED_BUILTIN, toggle0);
100
- toggle0 = !toggle0;
101
- }
102
-
103
- // ////////////////////////////////////////////
104
-
105
- #if (TIMER_INTERRUPT_USING_SAMD21)
106
-
107
- #define TIMER1_INTERVAL_MS 2000
72
+ // TC3, TC4, TC5 max permissible TIMER_INTERVAL_MS is 1398.101 ms, larger will overflow, therefore not permitted
73
+ // Use TCC, TCC1, TCC2 for longer TIMER_INTERVAL_MS
74
+ #define TIMER_INTERVAL_MS 1000
108
75
109
76
volatile uint32_t preMillisTimer1 = 0 ;
110
77
111
- // Init SAMD timer TIMER_TCC
112
- // SAMDTimer ITimer1(TIMER_TC4);
113
- // SAMDTimer ITimer1(TIMER_TC5);
114
- SAMDTimer ITimer1 (TIMER_TCC);
115
- // SAMDTimer ITimer1(TIMER_TCC1);
116
- // SAMDTimer ITimer1(TIMER_TCC2);
78
+ #if USING_TIMER_TC3
79
+ #define SELECTED_TIMER TIMER_TC3
80
+ #elif USING_TIMER_TC4
81
+ #define SELECTED_TIMER TIMER_TC4
82
+ #elif USING_TIMER_TC5
83
+ #define SELECTED_TIMER TIMER_TC5
84
+ #elif USING_TIMER_TCC
85
+ #define SELECTED_TIMER TIMER_TCC
86
+ #elif USING_TIMER_TCC1
87
+ #define SELECTED_TIMER TIMER_TCC1
88
+ #elif USING_TIMER_TCC2
89
+ #define SELECTED_TIMER TIMER_TCC
90
+ #else
91
+ #error You have to select 1 Timer
92
+ #endif
93
+
94
+ // Init selected SAMD timer
95
+ SAMDTimer ITimer (SELECTED_TIMER);
117
96
118
97
// ////////////////////////////////////////////
119
98
120
- void TimerHandler1 ()
99
+ void TimerHandler ()
121
100
{
122
- static bool toggle1 = false ;
123
- static bool started = false ;
124
-
125
- if (!started)
126
- {
127
- started = true ;
128
- pinMode (LED_BLUE, OUTPUT);
129
- }
101
+ static bool toggle = false ;
130
102
131
- #if (TIMER_INTERRUPT_DEBUG > 0)
132
- static uint32_t curMillis = 0 ;
133
-
134
- curMillis = millis ();
135
-
136
- if (curMillis > TIMER1_INTERVAL_MS)
137
- {
138
- Serial.print (F (" ITimer1: millis() = " )); Serial.print (curMillis);
139
- Serial.print (F (" , delta = " )); Serial.println (curMillis - preMillisTimer1);
140
- }
141
-
142
- preMillisTimer0 = curMillis;
143
- #endif
103
+ // Serial.println(toggle? "ON" : "OFF");
144
104
145
- // timer interrupt toggles outputPin
146
- digitalWrite (LED_BLUE, toggle1 );
147
- toggle1 = !toggle1 ;
105
+ // timer interrupt toggles pin LED_BUILTIN
106
+ digitalWrite (LED_BUILTIN, toggle );
107
+ toggle = !toggle ;
148
108
}
149
- #endif
150
109
151
110
// ////////////////////////////////////////////
152
111
@@ -164,24 +123,13 @@ void setup()
164
123
Serial.print (F (" CPU Frequency = " )); Serial.print (F_CPU / 1000000 ); Serial.println (F (" MHz" ));
165
124
166
125
// Interval in millisecs
167
- if (ITimer0 .attachInterruptInterval_MS (TIMER0_INTERVAL_MS, TimerHandler0 ))
126
+ if (ITimer .attachInterruptInterval_MS (TIMER_INTERVAL_MS, TimerHandler ))
168
127
{
169
- preMillisTimer0 = millis ();
170
- Serial.print (F (" Starting ITimer0 OK, millis() = " )); Serial.println (preMillisTimer0 );
128
+ preMillisTimer = millis ();
129
+ Serial.print (F (" Starting ITimer OK, millis() = " )); Serial.println (preMillisTimer );
171
130
}
172
131
else
173
- Serial.println (F (" Can't set ITimer0. Select another freq. or timer" ));
174
-
175
- #if (TIMER_INTERRUPT_USING_SAMD21)
176
- // Interval in millisecs
177
- if (ITimer1.attachInterruptInterval_MS (TIMER1_INTERVAL_MS, TimerHandler1))
178
- {
179
- preMillisTimer1 = millis ();
180
- Serial.print (F (" Starting ITimer1 OK, millis() = " )); Serial.println (preMillisTimer1);
181
- }
182
- else
183
- Serial.println (F (" Can't set ITimer1. Select another freq. or timer" ));
184
- #endif
132
+ Serial.println (F (" Can't set ITimer. Select another freq. or timer" ));
185
133
}
186
134
187
135
// ////////////////////////////////////////////
0 commit comments