|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Change_Interval.ino |
| 3 | + For Teensy boards |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/Teensy_TimerInterrupt |
| 7 | + Licensed under MIT license |
| 8 | +
|
| 9 | + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by |
| 10 | + unsigned long miliseconds), you just consume only one Teensy timer and avoid conflicting with other cores' tasks. |
| 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 | + Based on SimpleTimer - A timer library for Arduino. |
| 16 | + Author: mromani@ottotecnica.com |
| 17 | + Copyright (c) 2010 OTTOTECNICA Italy |
| 18 | +
|
| 19 | + Based on BlynkTimer.h |
| 20 | + Author: Volodymyr Shymanskyy |
| 21 | +
|
| 22 | + Version: 1.1.1 |
| 23 | +
|
| 24 | + Version Modified By Date Comments |
| 25 | + ------- ----------- ---------- ----------- |
| 26 | + 1.0.0 K Hoang 04/11/2020 Initial coding |
| 27 | + 1.0.1 K Hoang 06/11/2020 Add complicated example ISR_16_Timers_Array using all 16 independent ISR Timers. |
| 28 | + 1.1.1 K.Hoang 06/12/2020 Add complex examples. Bump up version to sync with other TimerInterrupt Libraries |
| 29 | +*****************************************************************************************************************************/ |
| 30 | + |
| 31 | +/* |
| 32 | + Notes: |
| 33 | + Special design is necessary to share data between interrupt code and the rest of your program. |
| 34 | + Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
| 35 | + variable can not spontaneously change. Because your function may change variables while your program is using them, |
| 36 | + the compiler needs this hint. But volatile alone is often not enough. |
| 37 | + When accessing shared variables, usually interrupts must be disabled. Even with volatile, |
| 38 | + if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. |
| 39 | + If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled |
| 40 | + or the entire sequence of your code which accesses the data. |
| 41 | +*/ |
| 42 | + |
| 43 | +#if !( defined(CORE_TEENSY) || defined(TEENSYDUINO) ) |
| 44 | + #error This code is designed to run on Teensy platform! Please check your Tools->Board setting. |
| 45 | +#endif |
| 46 | + |
| 47 | +// These define's must be placed at the beginning before #include "TeensyTimerInterrupt.h" |
| 48 | +// Don't define Teensy_TEENSY_TIMER_INTERRUPT_DEBUG > 0. Only for special ISR debugging only. Can hang the system. |
| 49 | +#define TEENSY_TIMER_INTERRUPT_DEBUG 0 |
| 50 | + |
| 51 | +#include "TeensyTimerInterrupt.h" |
| 52 | + |
| 53 | +#ifndef LED_BUILTIN |
| 54 | + #define LED_BUILTIN 13 |
| 55 | +#endif |
| 56 | + |
| 57 | +#ifndef LED_BLUE |
| 58 | + #define LED_BLUE 2 |
| 59 | +#endif |
| 60 | + |
| 61 | +#ifndef LED_RED |
| 62 | +# define LED_RED 3 |
| 63 | +#endif |
| 64 | + |
| 65 | +// For Teensy 4.0/4.1, F_BUS_ACTUAL = 150 MHz => max period is only 55922 us (~17.9 Hz) |
| 66 | + |
| 67 | +#define TIMER_INTERVAL_MS 10 |
| 68 | + |
| 69 | +volatile uint32_t TimerCount = 0; |
| 70 | + |
| 71 | +// You can select Teensy Hardware Timer from TEENSY_TIMER_1 or TEENSY_TIMER_3 |
| 72 | + |
| 73 | +// Init Teensy timer TEENSY_TIMER_1 |
| 74 | +TeensyTimer ITimer(TEENSY_TIMER_1); |
| 75 | + |
| 76 | +void printResult(uint32_t currTime) |
| 77 | +{ |
| 78 | + Serial.printf("Time = %ld, TimerCount = %lu\n", currTime, TimerCount); |
| 79 | +} |
| 80 | + |
| 81 | +void TimerHandler(void) |
| 82 | +{ |
| 83 | + static bool toggle = false; |
| 84 | + |
| 85 | + // Flag for checking to be sure ISR is working as SErial.print is not OK here in ISR |
| 86 | + TimerCount++; |
| 87 | + |
| 88 | + //timer interrupt toggles pin LED_BUILTIN |
| 89 | + digitalWrite(LED_BUILTIN, toggle); |
| 90 | + toggle = !toggle; |
| 91 | +} |
| 92 | + |
| 93 | +void setup() |
| 94 | +{ |
| 95 | + pinMode(LED_BUILTIN, OUTPUT); |
| 96 | + |
| 97 | + Serial.begin(115200); |
| 98 | + while (!Serial); |
| 99 | + |
| 100 | + delay(100); |
| 101 | + |
| 102 | + Serial.printf("\nStarting Change_Interval on %s\n", BOARD_NAME); |
| 103 | + Serial.println(TEENSY_TIMER_INTERRUPT_VERSION); |
| 104 | + Serial.println("CPU Frequency = " + String(F_CPU / 1000000) + " MHz"); |
| 105 | + |
| 106 | + // Interval in microsecs |
| 107 | + if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler)) |
| 108 | + { |
| 109 | + Serial.printf("Starting ITimer OK, millis() = %ld\n", millis()); |
| 110 | + } |
| 111 | + else |
| 112 | + Serial.println("Can't set ITimer. Select another freq. or timer"); |
| 113 | +} |
| 114 | + |
| 115 | +#define CHECK_INTERVAL_MS 10000L |
| 116 | +#define CHANGE_INTERVAL_MS 20000L |
| 117 | + |
| 118 | +void loop() |
| 119 | +{ |
| 120 | + static uint32_t lastTime = 0; |
| 121 | + static uint32_t lastChangeTime = 0; |
| 122 | + static uint32_t currTime; |
| 123 | + static uint32_t multFactor = 0; |
| 124 | + |
| 125 | + currTime = millis(); |
| 126 | + |
| 127 | + if (currTime - lastTime > CHECK_INTERVAL_MS) |
| 128 | + { |
| 129 | + printResult(currTime); |
| 130 | + lastTime = currTime; |
| 131 | + |
| 132 | + if (currTime - lastChangeTime > CHANGE_INTERVAL_MS) |
| 133 | + { |
| 134 | + //setInterval(unsigned long interval, timerCallback callback) |
| 135 | + multFactor = (multFactor + 1) % 2; |
| 136 | + |
| 137 | + ITimer.setInterval(TIMER_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler); |
| 138 | + |
| 139 | + Serial.printf("Changing Interval, Timer = %lu\n", TIMER_INTERVAL_MS * (multFactor + 1)); |
| 140 | + |
| 141 | + lastChangeTime = currTime; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments