|
| 1 | +# ESP8266 TimerInterrupt Library |
| 2 | + |
| 3 | +This library enables you to use Interrupt from Hardware Timers on an ESP8266-based board. |
| 4 | + |
| 5 | +Why do we need this Hardware Timer Interrupt? |
| 6 | + |
| 7 | +Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup(). |
| 8 | + |
| 9 | +So your function might not be executed, and the result would be disastrous. |
| 10 | + |
| 11 | +You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.). |
| 12 | + |
| 13 | +The correct choice is to use a Hardware Timer with Interrupt to call your function. |
| 14 | + |
| 15 | +These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy. |
| 16 | + |
| 17 | +Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services. |
| 18 | + |
| 19 | +The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on: |
| 20 | + |
| 21 | +https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ |
| 22 | + |
| 23 | +**Important Notes:** |
| 24 | +1. Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function. |
| 25 | + |
| 26 | +2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile. |
| 27 | + |
| 28 | +## Installation |
| 29 | +1. Navigate to (https://github.com/khoih-prog/ESP8266TimerInterrupt) page. |
| 30 | +2. Download the latest release `ESP8266TimerInterrupt-master.zip`. |
| 31 | +3. Extract the zip file to `ESP8266TimerInterrupt-master` directory |
| 32 | +4. Copy whole folder to Arduino libraries' directory such as `.Arduino/libraries/ESP8266TimerInterrupt-master`. |
| 33 | + |
| 34 | +## More useful Information |
| 35 | + |
| 36 | +The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega. |
| 37 | +The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available. |
| 38 | +The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short. |
| 39 | +Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!! |
| 40 | + |
| 41 | +The timer1 counters can be configured to support automatic reload. |
| 42 | + |
| 43 | +## Supported Boards |
| 44 | + |
| 45 | +- ESP8266 |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +How to use: |
| 50 | + |
| 51 | +``` |
| 52 | +//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h" |
| 53 | +#define TIMER_INTERRUPT_DEBUG 1 |
| 54 | +
|
| 55 | +#include "ESP8266TimerInterrupt.h" |
| 56 | +
|
| 57 | +#ifndef LED_BUILTIN |
| 58 | +#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED |
| 59 | +#endif |
| 60 | +
|
| 61 | +volatile uint32_t lastMillis = 0; |
| 62 | +
|
| 63 | +void IRAM_ATTR TimerHandler(void) |
| 64 | +{ |
| 65 | + static bool toggle = false; |
| 66 | + static bool started = false; |
| 67 | +
|
| 68 | + if (!started) |
| 69 | + { |
| 70 | + started = true; |
| 71 | + pinMode(LED_BUILTIN, OUTPUT); |
| 72 | + } |
| 73 | +
|
| 74 | + #if (TIMER_INTERRUPT_DEBUG > 0) |
| 75 | + if (lastMillis != 0) |
| 76 | + Serial.println("Delta ms = " + String(millis() - lastMillis)); |
| 77 | + lastMillis = millis(); |
| 78 | + #endif |
| 79 | + |
| 80 | + //timer interrupt toggles pin LED_BUILTIN |
| 81 | + digitalWrite(LED_BUILTIN, toggle); |
| 82 | + toggle = !toggle; |
| 83 | +} |
| 84 | +
|
| 85 | +#define TIMER_INTERVAL_MS 1000 |
| 86 | +
|
| 87 | +// Init ESP8266 timer 0 |
| 88 | +ESP8266Timer ITimer; |
| 89 | +
|
| 90 | +
|
| 91 | +void setup() |
| 92 | +{ |
| 93 | + Serial.begin(115200); |
| 94 | + Serial.println("\nStarting"); |
| 95 | + |
| 96 | + // Interval in microsecs |
| 97 | + if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler)) |
| 98 | + Serial.println("Starting ITimer OK, millis() = " + String(millis())); |
| 99 | + else |
| 100 | + Serial.println("Can't set ITimer correctly. Select another freq. or interval"); |
| 101 | +
|
| 102 | +} |
| 103 | +
|
| 104 | +void loop() |
| 105 | +{ |
| 106 | + |
| 107 | +} |
| 108 | +
|
| 109 | +``` |
| 110 | +## TO DO |
| 111 | + |
| 112 | +1. More hardware-initiated software-enabled timers |
| 113 | +2. Longer time interval |
| 114 | + |
| 115 | + |
| 116 | +## DONE |
| 117 | + |
| 118 | +For current version v1.0.0 |
| 119 | + |
| 120 | +1. Basic hardware timers for ESP8266. |
| 121 | + |
| 122 | + |
| 123 | +## Contributing |
| 124 | +If you want to contribute to this project: |
| 125 | +- Report bugs and errors |
| 126 | +- Ask for enhancements |
| 127 | +- Create issues and pull requests |
| 128 | +- Tell other people about this library |
| 129 | + |
| 130 | +## Copyright |
| 131 | +Copyright 2019- Khoi Hoang |
0 commit comments