11/* ***************************************************************************************************************************
2- * examples/Argument_Simple.ino
3- * For Arduino AVR boards
4- * Written by Khoi Hoang
5- *
6- * Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt
7- * Licensed under MIT license
8- * Version: v1.0.2
9- *
10- * Now we can use these new 16 ISR-based timers, while consuming only 1 hardware Timer.
11- * Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
12- * The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
13- * Therefore, their executions are not blocked by bad-behaving functions / tasks.
14- * This important feature is absolutely necessary for mission-critical tasks.
15- *
16- * Notes:
17- * Special design is necessary to share data between interrupt code and the rest of your program.
18- * Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
19- * variable can not spontaneously change. Because your function may change variables while your program is using them,
20- * the compiler needs this hint. But volatile alone is often not enough.
21- * When accessing shared variables, usually interrupts must be disabled. Even with volatile,
22- * if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
23- * If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
24- * or the entire sequence of your code which accesses the data.
25- *
26- * Version Modified By Date Comments
27- * ------- ----------- ---------- -----------
28- * 1.0.0 K Hoang 23/11/2019 Initial coding
29- * 1.0.1 K Hoang 25/11/2019 New release fixing compiler error
30- * 1.0.2 K.Hoang 28/11/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
2+ Argument_Simple.ino
3+ For Arduino boards (UNO, Nano, Mega, etc. )
4+ Written by Khoi Hoang
5+
6+ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt
7+ Licensed under MIT license
8+
9+ Now we can use these new 16 ISR-based timers, while consuming only 1 hardware 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+ Notes:
16+ Special design is necessary to share data between interrupt code and the rest of your program.
17+ Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
18+ variable can not spontaneously change. Because your function may change variables while your program is using them,
19+ the compiler needs this hint. But volatile alone is often not enough.
20+ When accessing shared variables, usually interrupts must be disabled. Even with volatile,
21+ if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
22+ If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
23+ or the entire sequence of your code which accesses the data.
24+
25+ Version: 1.0.3
26+
27+ Version Modified By Date Comments
28+ ------- ----------- ---------- -----------
29+ 1.0.0 K Hoang 23/11/2019 Initial coding
30+ 1.0.1 K Hoang 25/11/2019 New release fixing compiler error
31+ 1.0.2 K.Hoang 28/11/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
32+ 1.0.3 K.Hoang 01/12/2020 Add complex examples ISR_16_Timers_Array_Complex and ISR_16_Timers_Array_Complex
3133 *****************************************************************************************************************************/
3234// These define's must be placed at the beginning before #include "TimerInterrupt.h"
3335#define TIMER_INTERRUPT_DEBUG 0
@@ -53,7 +55,7 @@ void TimerHandler1(unsigned int outputPin = LED_BUILTIN)
5355 started = true ;
5456 pinMode (outputPin, OUTPUT);
5557 }
56-
58+
5759 // timer interrupt toggles pin outputPin, default LED_BUILTIN
5860 Serial.println (" pin1 = " + String (outputPin) + " address: " + String ((uint32_t ) &outputPin) );
5961 digitalWrite (outputPin, toggle1);
@@ -70,47 +72,51 @@ void TimerHandler2(unsigned int outputPin = LED_BUILTIN)
7072 started = true ;
7173 pinMode (outputPin, OUTPUT);
7274 }
73-
75+
7476 // timer interrupt toggles pin outputPin, default LED_BUILTIN
7577 digitalWrite (outputPin, toggle2);
7678 toggle2 = !toggle2;
7779}
7880
79- #define TIMER1_INTERVAL_MS 1000
81+ #define TIMER1_INTERVAL_MS 1000
8082
81- #define TIMER2_INTERVAL_MS 2000
83+ #define TIMER2_INTERVAL_MS 2000
8284
8385void setup ()
8486{
8587 Serial.begin (115200 );
86- Serial.println (" \n Starting" );
88+ while (!Serial);
89+
90+ Serial.println (" \n Starting Argument_Simple" );
91+ Serial.println (" Version : " + String (TIMER_INTERRUPT_VERSION));
92+ Serial.println (" CPU Frequency = " + String (F_CPU / 1000000 ) + " MHz" );
8793
8894 // Timer0 is used for micros(), millis(), delay(), etc and can't be used
8995 // Select Timer 1-2 for UNO, 0-5 for MEGA
90- // Timer 2 is 8-bit timer, only for higher frequency
91-
96+ // Timer 2 is 8-bit timer, only for higher frequency
97+
9298 ITimer1.init ();
93-
94- // Using ATmega328 used in UNO => 16MHz CPU clock ,
99+
100+ // Using ATmega328 used in UNO => 16MHz CPU clock ,
95101 // For 16-bit timer 1, 3, 4 and 5, set frequency from 0.2385 to some KHz
96102 // For 8-bit timer 2 (prescaler up to 1024, set frequency from 61.5Hz to some KHz
97-
103+
98104 if (ITimer1.attachInterruptInterval (TIMER1_INTERVAL_MS, TimerHandler1, outputPin1))
99105 {
100106 Serial.println (" Starting ITimer1 OK, millis() = " + String (millis ()));
101107 Serial.println (" OutputPin1 = " + String (outputPin1) + " , address = " + String ((uint32_t ) &outputPin1) );
102108 }
103109 else
104110 Serial.println (" Can't set ITimer1. Select another freq. or timer" );
105-
111+
106112 ITimer2.init ();
107-
113+
108114 if (ITimer2.attachInterruptInterval (TIMER2_INTERVAL_MS, TimerHandler2, outputPin2))
109115 Serial.println (" Starting ITimer2 OK, millis() = " + String (millis ()));
110116 else
111- Serial.println (" Can't set ITimer2. Select another freq. or timer" );
117+ Serial.println (" Can't set ITimer2. Select another freq. or timer" );
112118}
113119
114120void loop ()
115- {
121+ {
116122}
0 commit comments