19
19
Based on BlynkTimer.h
20
20
Author: Volodymyr Shymanskyy
21
21
22
- Version: 1.0.1
22
+ Version: 1.0.2
23
23
24
24
Version Modified By Date Comments
25
25
------- ----------- ---------- -----------
26
26
1.0.0 K Hoang 02/11/2020 Initial coding
27
27
1.0.1 K Hoang 06/11/2020 Add complicated example ISR_16_Timers_Array using all 16 independent ISR Timers.
28
+ 1.0.2 K Hoang 24/11/2020 Add complicated example ISR_16_Timers_Array_Complex and optimize examples
28
29
*****************************************************************************************************************************/
29
30
30
31
/*
@@ -79,28 +80,20 @@ NRF52Timer ITimer0(NRF_TIMER_1);
79
80
// Init NRF52 timer NRF_TIMER2
80
81
NRF52Timer ITimer1 (NRF_TIMER_2);
81
82
83
+ volatile uint32_t Timer0Count = 0 ;
84
+ volatile uint32_t Timer1Count = 0 ;
85
+
86
+ void printResult (uint32_t currTime)
87
+ {
88
+ Serial.printf (" Time = %ld, Timer0Count = %lu, , Timer1Count = %lu\n " , currTime, Timer0Count, Timer1Count);
89
+ }
90
+
82
91
void TimerHandler0 (void )
83
92
{
84
93
static bool toggle0 = false ;
85
- static bool started = false ;
86
- static uint32_t curMillis = 0 ;
87
94
88
- if (!started)
89
- {
90
- started = true ;
91
- pinMode (LED_BUILTIN, OUTPUT);
92
- }
93
-
94
- #if (NRF52_TIMER_INTERRUPT_DEBUG > 0)
95
- curMillis = millis ();
96
-
97
- if (curMillis > TIMER0_INTERVAL_MS)
98
- {
99
- Serial.println (" ITimer0: millis() = " + String (curMillis) + " , delta = " + String (curMillis - preMillisTimer0));
100
- }
101
-
102
- preMillisTimer0 = curMillis;
103
- #endif
95
+ // Flag for checking to be sure ISR is working as SErial.print is not OK here in ISR
96
+ Timer0Count++;
104
97
105
98
// timer interrupt toggles pin LED_BUILTIN
106
99
digitalWrite (LED_BUILTIN, toggle0);
@@ -110,26 +103,10 @@ void TimerHandler0(void)
110
103
void TimerHandler1 (void )
111
104
{
112
105
static bool toggle1 = false ;
113
- static bool started = false ;
114
- static uint32_t curMillis = 0 ;
115
106
116
- if (!started)
117
- {
118
- started = true ;
119
- pinMode (LED_BLUE_PIN, OUTPUT);
120
- }
107
+ // Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
108
+ Timer1Count++;
121
109
122
- #if (NRF52_TIMER_INTERRUPT_DEBUG > 0)
123
- curMillis = millis ();
124
-
125
- if (curMillis > TIMER1_INTERVAL_MS)
126
- {
127
- Serial.println (" ITimer1: millis() = " + String (curMillis) + " , delta = " + String (curMillis - preMillisTimer1));
128
- }
129
-
130
- preMillisTimer1 = curMillis;
131
- #endif
132
-
133
110
// timer interrupt toggles outputPin
134
111
digitalWrite (LED_BLUE_PIN, toggle1);
135
112
toggle1 = !toggle1;
@@ -144,30 +121,40 @@ void setup()
144
121
while (!Serial);
145
122
146
123
delay (100 );
147
-
148
- Serial.println (" \n Starting Argument_None on " + String (BOARD_NAME));
149
- Serial.println (" Version : " + String (NRF52_TIMER_INTERRUPT_VERSION));
150
124
125
+ Serial.printf (" \n Starting Argument_None on %s\n " , BOARD_NAME);
126
+ Serial.printf (" Version : v%s\n " , NRF52_TIMER_INTERRUPT_VERSION);
127
+ Serial.printf (" CPU Frequency = %ld MHz\n " , F_CPU / 1000000 );
128
+
151
129
// Interval in microsecs
152
130
if (ITimer0.attachInterruptInterval (TIMER0_INTERVAL_MS * 1000 , TimerHandler0))
153
131
{
154
- preMillisTimer0 = millis ();
155
- Serial.println (" Starting ITimer0 OK, millis() = " + String (preMillisTimer0));
132
+ Serial.printf (" Starting ITimer0 OK, millis() = %ld\n " , millis ());
156
133
}
157
134
else
158
135
Serial.println (" Can't set ITimer0. Select another freq. or timer" );
159
136
160
137
// Interval in microsecs
161
138
if (ITimer1.attachInterruptInterval (TIMER1_INTERVAL_MS * 1000 , TimerHandler1))
162
139
{
163
- preMillisTimer1 = millis ();
164
- Serial.println (" Starting ITimer1 OK, millis() = " + String (preMillisTimer1));
140
+ Serial.printf (" Starting ITimer1 OK, millis() = %ld\n " , millis ());
165
141
}
166
142
else
167
143
Serial.println (" Can't set ITimer1. Select another freq. or timer" );
168
144
}
169
145
146
+ #define CHECK_INTERVAL_MS 10000L
147
+
170
148
void loop ()
171
149
{
150
+ static uint32_t lastTime = 0 ;
151
+ static uint32_t currTime;
152
+
153
+ currTime = millis ();
172
154
155
+ if (currTime - lastTime > CHECK_INTERVAL_MS)
156
+ {
157
+ printResult (currTime);
158
+ lastTime = currTime;
159
+ }
173
160
}
0 commit comments