项目作者: khoih-prog

项目描述 :
This library enables you to use Interrupt from Hardware Timers on an ESP8266-based board. It now supports 16 ISR-based timers, while consuming only 1 hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.
高级语言: C++
项目地址: git://github.com/khoih-prog/ESP8266TimerInterrupt.git
创建时间: 2019-11-23T18:44:50Z
项目社区:https://github.com/khoih-prog/ESP8266TimerInterrupt

开源协议:MIT License

下载


ESP8266TimerInterrupt Library

arduino-library-badge
GitHub release
GitHub
contributions welcome
GitHub issues

Donate to my libraries using BuyMeACoffee

—-

Table of Contents

—-

Important Change from v1.6.0

Please have a look at HOWTO Fix Multiple Definitions Linker Error

—-

Why do we need this ESP8266TimerInterrupt library

Features

This library enables you to use Interrupt from Hardware Timers on an ESP8266-based board.

As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers’ interval is very long (ulong millisecs).

Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long milliseconds) while the accuracy is nearly perfect compared to software timers.

The most important feature is they’re ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.

The ISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.

Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many (up to 16) timers to use.

This non-being-blocked important feature is absolutely necessary for mission-critical tasks.

You’ll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
in loop(), using delay() function as an example. The elapsed time then is very unaccurate

Why using ISR-based Hardware Timer Interrupt is better

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().

So your function might not be executed, and the result would be disastrous.

You’d prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use a Hardware Timer with Interrupt to call your function.

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.

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.

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:

HOWTO Attach Interrupt


Currently supported Boards

  1. ESP8266-based boards

Important Notes about ISR

  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.

  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.

—-

Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. ESP8266 Core 3.0.2+ for ESP8266-based boards. Latest release. To use ESP8266 core 2.7.1+ for LittleFS.
  3. SimpleTimer library to use with some examples.

—-

Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for ESP8266TimerInterrupt, then select / install the latest version.
You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to ESP8266TimerInterrupt page.
  2. Download the latest release ESP8266TimerInterrupt-master.zip.
  3. Extract the zip file to ESP8266TimerInterrupt-master directory
  4. Copy whole ESP8266TimerInterrupt-master folder to Arduino libraries’ directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install ESP8266TimerInterrupt library by using Library Manager. Search for ESP8266TimerInterrupt in Platform.io Author’s Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File

—-

HOWTO Fix Multiple Definitions Linker Error

The current library implementation, using xyz-Impl.h instead of standard xyz.cpp, possibly creates certain Multiple Definitions Linker error in certain use cases.

You can use

  1. #include "ESP8266TimerInterrupt.h" //https://github.com/khoih-prog/ESP8266TimerInterrupt
  2. #include "ESP8266_ISR_Timer.hpp" //https://github.com/khoih-prog/ESP8266TimerInterrupt

in many files. But be sure to use the following #include <ESP8266_ISR_Timer.h> in just 1 .h, .cpp or .ino file, which must not be included in any other file, to avoid Multiple Definitions Linker Error

  1. // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
  2. #include "ESP8266_ISR_Timer.h" //https://github.com/khoih-prog/ESP8266TimerInterrupt

Check the new multiFileProject example for a HOWTO demo.

Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80

—-

HOWTO Use PWM analogWrite() with ESP8266 running Timer1 Interrupt

Please have a look at ESP8266TimerInterrupt Issue 8: ESP8266Timer and PWM —> wdt reset to have more detailed description and solution of the issue.

1. ESP8266 has only 2 hardware timers, named Timer0 and Timer1

2. ESP8266 hardware timers’ functions

  • Timer0 has been used for WiFi and it’s not advisable to use while using WiFi (if not using WiFi, why select ESP8266 ??)
  • Timer1 is used by this ESP8266TimerInterrupt Library

3. How to use PWM analogWrite() functions while using this library

  1. If possible, use software timer instead of ESP8266TimerInterrupt Hardware Timer1
  2. If using ESP8266TimerInterrupt Hardware Timer1 is a must, you can either

—-

More useful Information

The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They’re only better than UNO / Mega.
The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it’s not advisable to use. Only timer1 is available.
The timer1’s 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!

The timer1 counters can be configured to support automatic reload.


Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long milliseconds).

The accuracy is nearly perfect compared to software timers. The most important feature is they’re ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions / tasks.

This important feature is absolutely necessary for mission-critical tasks.

The ISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.

Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many (up to 16) timers to use. This non-being-blocked important feature is absolutely necessary for mission-critical tasks.

You’ll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task in loop(), using delay() function as an example. The elapsed time then is very unaccurate

—-

Usage

The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They’re only better than UNO / Mega.

The ESP8266 has two hardware timers, but Timer0 has been used for WiFi and it’s not advisable to use. Only Timer1 is available.

The Timer1’s 23-bit counter can terribly count only up to 8,388,607. So the timer1 maximum interval is very short.
Using 256 prescaler, maximum Timer1 interval is only 26.843542 seconds !!!

1. Using only Hardware Timer directly

1.1 Init Hardware Timer

  1. // Select a Timer Clock
  2. #define USING_TIM_DIV1 false // for shortest and most accurate timer
  3. #define USING_TIM_DIV16 false // for medium time and medium accurate timer
  4. #define USING_TIM_DIV256 true // for longest timer but least accurate. Default
  5. // Init ESP8266 only and only Timer 1
  6. ESP8266Timer ITimer;

1.2 Set Hardware Timer Interval and attach Timer Interrupt Handler function

Use one of these functions with interval in unsigned long milliseconds

  1. // interval (in microseconds)
  2. bool setInterval(unsigned long interval, timer_callback callback)
  3. // interval (in microseconds)
  4. bool attachInterruptInterval(unsigned long interval, timer_callback callback)

as follows

  1. void IRAM_ATTR TimerHandler()
  2. {
  3. // Doing something here inside ISR
  4. }
  5. #define TIMER_INTERVAL_MS 1000
  6. void setup()
  7. {
  8. ....
  9. // Interval in microsecs
  10. if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
  11. {
  12. lastMillis = millis();
  13. Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(lastMillis);
  14. }
  15. else
  16. Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));
  17. }

1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function

Use one of these functions with frequency in float Hz

  1. // frequency (in hertz)
  2. bool setFrequency(float frequency, timer_callback callback)
  3. // frequency (in hertz)
  4. bool attachInterrupt(float frequency, timer_callback callback)

as follows

  1. void TimerHandler()
  2. {
  3. // Doing something here inside ISR
  4. }
  5. #define TIMER_FREQ_HZ 5555.555
  6. void setup()
  7. {
  8. ....
  9. // Frequency in float Hz
  10. if (ITimer.attachInterrupt(TIMER_FREQ_HZ, TimerHandler))
  11. Serial.println("Starting ITimer OK, millis() = " + String(millis()));
  12. else
  13. Serial.println("Can't set ITimer. Select another freq. or timer");
  14. }

2. Using 16 ISR_based Timers from 1 Hardware Timer

2.1 Important Note

The 16 ISR_based Timers, designed for long timer intervals, only support using unsigned long millisec intervals. If you have to use much higher frequency or sub-millisecond interval, you have to use the Hardware Timers directly as in 1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function

2.2 Init Hardware Timer and ISR-based Timer

  1. // Select a Timer Clock
  2. #define USING_TIM_DIV1 false // for shortest and most accurate timer
  3. #define USING_TIM_DIV16 false // for medium time and medium accurate timer
  4. #define USING_TIM_DIV256 true // for longest timer but least accurate. Default
  5. #include "ESP8266TimerInterrupt.h"
  6. #include "ESP8266_ISR_Timer.h"
  7. // Init ESP8266 timer 1
  8. ESP8266Timer ITimer;
  9. // Init ESP8266_ISR_Timer
  10. ESP8266_ISR_Timer ISR_Timer;

2.3 Set Hardware Timer Interval and attach Timer Interrupt Handler functions

  1. void IRAM_ATTR TimerHandler()
  2. {
  3. ISR_timer.run();
  4. }
  5. #define HW_TIMER_INTERVAL_MS 50L
  6. #define TIMER_INTERVAL_2S 2000L
  7. #define TIMER_INTERVAL_5S 5000L
  8. #define TIMER_INTERVAL_11S 11000L
  9. #define TIMER_INTERVAL_101S 101000L
  10. // In AVR, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
  11. // The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
  12. // Or you can get this run-time error / crash
  13. void doingSomething2s()
  14. {
  15. // Doing something here inside ISR every 2 seconds
  16. }
  17. void doingSomething5s()
  18. {
  19. // Doing something here inside ISR every 5 seconds
  20. }
  21. void doingSomething11s()
  22. {
  23. // Doing something here inside ISR every 11 seconds
  24. }
  25. void doingSomething101s()
  26. {
  27. // Doing something here inside ISR every 101 seconds
  28. }
  29. void setup()
  30. {
  31. ....
  32. // Interval in microsecs
  33. if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
  34. {
  35. startMillis = millis();
  36. Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis);
  37. }
  38. else
  39. Serial.println(F("Can't set ITimer. Select another freq. or timer"));
  40. // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
  41. // You can use up to 16 timer for each ISR_Timer
  42. ISR_timer.setInterval(TIMER_INTERVAL_2S, doingSomething2s);
  43. ISR_timer.setInterval(TIMER_INTERVAL_5S, doingSomething5s);
  44. ISR_timer.setInterval(TIMER_INTERVAL_11S, doingSomething11s);
  45. ISR_timer.setInterval(TIMER_INTERVAL_101S, doingSomething101s);
  46. }

2.4 Set One-Shot Hardware Timer Interval

https://github.com/khoih-prog/ESP8266TimerInterrupt/blob/f497e733eda4a749067e037747c73d8c188a59d9/examples/ISR_16_Timers_Array_OneShot/ISR_16_Timers_Array_OneShot.ino#L414-L421

—-

Examples:

  1. Argument_None
  2. ISR_RPM_Measure
  3. RPM_Measure
  4. SwitchDebounce
  5. TimerInterruptTest
  6. Change_Interval.
  7. ISR_16_Timers_Array
  8. ISR_16_Timers_Array_Complex
  9. ISR_16_Timers_Array_OneShot New
    1. multiFileProject New

—-

Example Change_Interval

https://github.com/khoih-prog/ESP8266TimerInterrupt/blob/f497e733eda4a749067e037747c73d8c188a59d9/examples/Change_Interval/Change_Interval.ino#L32-L130

—-

Debug Terminal Output Samples

1. TimerInterruptTest on ESP8266_NODEMCU_ESP12E

The following is the sample terminal output when running example TimerInterruptTest on ESP8266_NODEMCU_ESP12E to demonstrate the accuracy of Hardware Timers.

  1. Starting TimerInterruptTest on ESP8266_NODEMCU_ESP12E
  2. ESP8266TimerInterrupt v1.6.0
  3. CPU Frequency = 160 MHz
  4. ESP8266TimerInterrupt: _fre = 312500.00, _count = 312500
  5. Starting ITimer OK, millis() = 262
  6. Delta ms = 1000
  7. Delta ms = 1000
  8. Delta ms = 1000
  9. Delta ms = 1000
  10. Delta ms = 1000
  11. Delta ms = 1000
  12. Delta ms = 1000
  13. Delta ms = 1000
  14. Delta ms = 1000
  15. Delta ms = 1000
  16. Delta ms = 1000
  17. Delta ms = 1000
  18. Delta ms = 1000
  19. Delta ms = 1000
  20. Delta ms = 1000
  21. Delta ms = 1000
  22. Delta ms = 1000

2. Change_Interval on ESP8266_NODEMCU_ESP12E

The following is the sample terminal output when running example Change_Interval on ESP8266_NODEMCU_ESP12E to demonstrate how to change Timer Interval on-the-fly

  1. Starting Change_Interval on ESP8266_NODEMCU_ESP12E
  2. ESP8266TimerInterrupt v1.6.0
  3. CPU Frequency = 160 MHz
  4. Starting ITimer OK, millis() = 162
  5. Time = 10001, TimerCount = 19
  6. Time = 20002, TimerCount = 39
  7. Changing Interval, Timer = 1000
  8. Time = 30003, TimerCount = 49
  9. Time = 40004, TimerCount = 59
  10. Changing Interval, Timer = 500
  11. Time = 50005, TimerCount = 79
  12. Time = 60006, TimerCount = 99
  13. Changing Interval, Timer = 1000
  14. Time = 70007, TimerCount = 109
  15. Time = 80008, TimerCount = 119
  16. Changing Interval, Timer = 500
  17. Time = 90009, TimerCount = 139
  18. Time = 100010, TimerCount = 159
  19. Changing Interval, Timer = 1000
  20. Time = 110011, TimerCount = 169
  21. Time = 120012, TimerCount = 179
  22. Changing Interval, Timer = 500
  23. Time = 130013, TimerCount = 199
  24. Time = 140014, TimerCount = 219
  25. Changing Interval, Timer = 1000
  26. Time = 150015, TimerCount = 229
  27. Time = 160016, TimerCount = 239
  28. Changing Interval, Timer = 500
  29. Time = 170017, TimerCount = 259
  30. Time = 180018, TimerCount = 279

3. ISR_16_Timers_Array on ESP8266_NODEMCU_ESP12E

The following is the sample terminal output when running example ISR_16_Timers_Array on ESP8266_NODEMCU_ESP12E to demonstrate of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!

  1. Starting ISR_16_Timers_Array on ESP8266_NODEMCU_ESP12E
  2. ESP8266TimerInterrupt v1.6.0
  3. CPU Frequency = 160 MHz
  4. Starting ITimer OK, millis() = 175
  5. 1s: Delta ms = 1003, ms = 1178
  6. 1s: Delta ms = 999, ms = 2177
  7. 2s: Delta ms = 2002, ms = 2177
  8. 1s: Delta ms = 1000, ms = 3177
  9. 3s: Delta ms = 3002, ms = 3177
  10. 1s: Delta ms = 1000, ms = 4177
  11. 2s: Delta ms = 2000, ms = 4177
  12. 4s: Delta ms = 4002, ms = 4177
  13. 1s: Delta ms = 1000, ms = 5177
  14. 5s: Delta ms = 5002, ms = 5177
  15. 1s: Delta ms = 1000, ms = 6177
  16. 2s: Delta ms = 2001, ms = 6178
  17. 3s: Delta ms = 3001, ms = 6178
  18. 6s: Delta ms = 6003, ms = 6178
  19. 1s: Delta ms = 1000, ms = 7177
  20. 7s: Delta ms = 7002, ms = 7177
  21. 1s: Delta ms = 1000, ms = 8177
  22. 2s: Delta ms = 1999, ms = 8177
  23. 4s: Delta ms = 4000, ms = 8177
  24. 8s: Delta ms = 8002, ms = 8177
  25. 1s: Delta ms = 1000, ms = 9177
  26. 3s: Delta ms = 2999, ms = 9177
  27. 9s: Delta ms = 9002, ms = 9177
  28. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10002
  29. 1s: Delta ms = 1000, ms = 10177
  30. 2s: Delta ms = 2000, ms = 10177
  31. 5s: Delta ms = 5001, ms = 10178
  32. 10s: Delta ms = 10006, ms = 10181
  33. 1s: Delta ms = 1000, ms = 11177
  34. 11s: Delta ms = 11003, ms = 11178
  35. 1s: Delta ms = 1000, ms = 12177
  36. 2s: Delta ms = 2000, ms = 12177
  37. 3s: Delta ms = 3000, ms = 12177
  38. 4s: Delta ms = 4000, ms = 12177
  39. 6s: Delta ms = 5999, ms = 12177
  40. 12s: Delta ms = 12005, ms = 12180
  41. 1s: Delta ms = 1000, ms = 13177
  42. 13s: Delta ms = 13002, ms = 13177
  43. 1s: Delta ms = 1000, ms = 14177
  44. 2s: Delta ms = 2000, ms = 14177
  45. 7s: Delta ms = 7000, ms = 14177
  46. 14s: Delta ms = 14002, ms = 14177
  47. 1s: Delta ms = 1000, ms = 15177
  48. 3s: Delta ms = 3000, ms = 15177
  49. 5s: Delta ms = 4999, ms = 15177
  50. 15s: Delta ms = 15002, ms = 15177
  51. 1s: Delta ms = 1000, ms = 16177
  52. 2s: Delta ms = 2000, ms = 16177
  53. 4s: Delta ms = 4001, ms = 16178
  54. 8s: Delta ms = 8001, ms = 16178
  55. 16s: Delta ms = 16003, ms = 16178
  56. 1s: Delta ms = 1000, ms = 17177
  57. 1s: Delta ms = 1000, ms = 18177
  58. 2s: Delta ms = 2000, ms = 18177
  59. 3s: Delta ms = 3000, ms = 18177
  60. 6s: Delta ms = 6000, ms = 18177
  61. 9s: Delta ms = 9001, ms = 18178
  62. 1s: Delta ms = 1000, ms = 19177
  63. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000

4. ISR_16_Timers_Array_Complex on ESP8266_NODEMCU_ESP12E

The following is the sample terminal output when running example ISR_16_Timers_Array_Complex on ESP8266_NODEMCU_ESP12E to demonstrate the ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!

  1. Starting ISR_16_Timers_Array_Complex on ESP8266_NODEMCU_ESP12E
  2. ESP8266TimerInterrupt v1.6.0
  3. CPU Frequency = 160 MHz
  4. Starting ITimer OK, millis() = 177
  5. SimpleTimer : 2, ms : 10179, Dms : 10000
  6. Timer : 0, programmed : 5000, actual : 5008
  7. Timer : 1, programmed : 10000, actual : 0
  8. Timer : 2, programmed : 15000, actual : 0
  9. Timer : 3, programmed : 20000, actual : 0
  10. Timer : 4, programmed : 25000, actual : 0
  11. Timer : 5, programmed : 30000, actual : 0
  12. Timer : 6, programmed : 35000, actual : 0
  13. Timer : 7, programmed : 40000, actual : 0
  14. Timer : 8, programmed : 45000, actual : 0
  15. Timer : 9, programmed : 50000, actual : 0
  16. Timer : 10, programmed : 55000, actual : 0
  17. Timer : 11, programmed : 60000, actual : 0
  18. Timer : 12, programmed : 65000, actual : 0
  19. Timer : 13, programmed : 70000, actual : 0
  20. Timer : 14, programmed : 75000, actual : 0
  21. Timer : 15, programmed : 80000, actual : 0
  22. SimpleTimer : 2, ms : 20232, Dms : 10053
  23. Timer : 0, programmed : 5000, actual : 5000
  24. Timer : 1, programmed : 10000, actual : 10000
  25. Timer : 2, programmed : 15000, actual : 15008
  26. Timer : 3, programmed : 20000, actual : 20008
  27. Timer : 4, programmed : 25000, actual : 0
  28. Timer : 5, programmed : 30000, actual : 0
  29. Timer : 6, programmed : 35000, actual : 0
  30. Timer : 7, programmed : 40000, actual : 0
  31. Timer : 8, programmed : 45000, actual : 0
  32. Timer : 9, programmed : 50000, actual : 0
  33. Timer : 10, programmed : 55000, actual : 0
  34. Timer : 11, programmed : 60000, actual : 0
  35. Timer : 12, programmed : 65000, actual : 0
  36. Timer : 13, programmed : 70000, actual : 0
  37. Timer : 14, programmed : 75000, actual : 0
  38. Timer : 15, programmed : 80000, actual : 0
  39. SimpleTimer : 2, ms : 30286, Dms : 10054
  40. Timer : 0, programmed : 5000, actual : 5000
  41. Timer : 1, programmed : 10000, actual : 10000
  42. Timer : 2, programmed : 15000, actual : 15000
  43. Timer : 3, programmed : 20000, actual : 20008
  44. Timer : 4, programmed : 25000, actual : 25008
  45. Timer : 5, programmed : 30000, actual : 30008
  46. Timer : 6, programmed : 35000, actual : 0
  47. Timer : 7, programmed : 40000, actual : 0
  48. Timer : 8, programmed : 45000, actual : 0
  49. Timer : 9, programmed : 50000, actual : 0
  50. Timer : 10, programmed : 55000, actual : 0
  51. Timer : 11, programmed : 60000, actual : 0
  52. Timer : 12, programmed : 65000, actual : 0
  53. Timer : 13, programmed : 70000, actual : 0
  54. Timer : 14, programmed : 75000, actual : 0
  55. Timer : 15, programmed : 80000, actual : 0
  56. SimpleTimer : 2, ms : 40341, Dms : 10055
  57. Timer : 0, programmed : 5000, actual : 5000
  58. Timer : 1, programmed : 10000, actual : 10000
  59. Timer : 2, programmed : 15000, actual : 15000
  60. Timer : 3, programmed : 20000, actual : 20000
  61. Timer : 4, programmed : 25000, actual : 25008
  62. Timer : 5, programmed : 30000, actual : 30008
  63. Timer : 6, programmed : 35000, actual : 35008
  64. Timer : 7, programmed : 40000, actual : 40008
  65. Timer : 8, programmed : 45000, actual : 0
  66. Timer : 9, programmed : 50000, actual : 0
  67. Timer : 10, programmed : 55000, actual : 0
  68. Timer : 11, programmed : 60000, actual : 0
  69. Timer : 12, programmed : 65000, actual : 0
  70. Timer : 13, programmed : 70000, actual : 0
  71. Timer : 14, programmed : 75000, actual : 0
  72. Timer : 15, programmed : 80000, actual : 0
  73. SimpleTimer : 2, ms : 50396, Dms : 10055
  74. Timer : 0, programmed : 5000, actual : 5000
  75. Timer : 1, programmed : 10000, actual : 10000
  76. Timer : 2, programmed : 15000, actual : 15000
  77. Timer : 3, programmed : 20000, actual : 20000
  78. Timer : 4, programmed : 25000, actual : 25000
  79. Timer : 5, programmed : 30000, actual : 30008
  80. Timer : 6, programmed : 35000, actual : 35008
  81. Timer : 7, programmed : 40000, actual : 40008
  82. Timer : 8, programmed : 45000, actual : 45008
  83. Timer : 9, programmed : 50000, actual : 50008
  84. Timer : 10, programmed : 55000, actual : 0
  85. Timer : 11, programmed : 60000, actual : 0
  86. Timer : 12, programmed : 65000, actual : 0
  87. Timer : 13, programmed : 70000, actual : 0
  88. Timer : 14, programmed : 75000, actual : 0
  89. Timer : 15, programmed : 80000, actual : 0
  90. SimpleTimer : 2, ms : 60452, Dms : 10056
  91. Timer : 0, programmed : 5000, actual : 5000
  92. Timer : 1, programmed : 10000, actual : 10000
  93. Timer : 2, programmed : 15000, actual : 15000
  94. Timer : 3, programmed : 20000, actual : 20000
  95. Timer : 4, programmed : 25000, actual : 25000
  96. Timer : 5, programmed : 30000, actual : 30000
  97. Timer : 6, programmed : 35000, actual : 35008
  98. Timer : 7, programmed : 40000, actual : 40008
  99. Timer : 8, programmed : 45000, actual : 45008
  100. Timer : 9, programmed : 50000, actual : 50008
  101. Timer : 10, programmed : 55000, actual : 55008
  102. Timer : 11, programmed : 60000, actual : 60008
  103. Timer : 12, programmed : 65000, actual : 0
  104. Timer : 13, programmed : 70000, actual : 0
  105. Timer : 14, programmed : 75000, actual : 0
  106. Timer : 15, programmed : 80000, actual : 0
  107. SimpleTimer : 2, ms : 70509, Dms : 10057
  108. Timer : 0, programmed : 5000, actual : 5000
  109. Timer : 1, programmed : 10000, actual : 10000
  110. Timer : 2, programmed : 15000, actual : 15000
  111. Timer : 3, programmed : 20000, actual : 20000
  112. Timer : 4, programmed : 25000, actual : 25000
  113. Timer : 5, programmed : 30000, actual : 30000
  114. Timer : 6, programmed : 35000, actual : 35000
  115. Timer : 7, programmed : 40000, actual : 40008
  116. Timer : 8, programmed : 45000, actual : 45008
  117. Timer : 9, programmed : 50000, actual : 50008
  118. Timer : 10, programmed : 55000, actual : 55008
  119. Timer : 11, programmed : 60000, actual : 60008
  120. Timer : 12, programmed : 65000, actual : 65008
  121. Timer : 13, programmed : 70000, actual : 70008
  122. Timer : 14, programmed : 75000, actual : 0
  123. Timer : 15, programmed : 80000, actual : 0
  124. SimpleTimer : 2, ms : 80566, Dms : 10057
  125. Timer : 0, programmed : 5000, actual : 5000
  126. Timer : 1, programmed : 10000, actual : 10000
  127. Timer : 2, programmed : 15000, actual : 15000
  128. Timer : 3, programmed : 20000, actual : 20000
  129. Timer : 4, programmed : 25000, actual : 25000
  130. Timer : 5, programmed : 30000, actual : 30000
  131. Timer : 6, programmed : 35000, actual : 35000
  132. Timer : 7, programmed : 40000, actual : 40000
  133. Timer : 8, programmed : 45000, actual : 45008
  134. Timer : 9, programmed : 50000, actual : 50008
  135. Timer : 10, programmed : 55000, actual : 55008
  136. Timer : 11, programmed : 60000, actual : 60008
  137. Timer : 12, programmed : 65000, actual : 65008
  138. Timer : 13, programmed : 70000, actual : 70008
  139. Timer : 14, programmed : 75000, actual : 75008
  140. Timer : 15, programmed : 80000, actual : 80008

5. ISR_16_Timers_Array_OneShot on ESP8266_NODEMCU_ESP12E

The following is the sample terminal output when running example ISR_16_Timers_Array_OneShot on ESP8266_NODEMCU_ESP12E to demonstrate the One-Shot ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!

  1. Starting ISR_16_Timers_Array_OneShot on ESP8266_NODEMCU_ESP12E
  2. ESP8266TimerInterrupt v1.6.0
  3. CPU Frequency = 160 MHz
  4. Starting ITimer OK, millis() = 365
  5. 1s: Delta ms = 1002, ms = 1367
  6. 2s: Delta ms = 2002, ms = 2367
  7. 3s: Delta ms = 3002, ms = 3367
  8. 4s: Delta ms = 4002, ms = 4367
  9. 5s: Delta ms = 5002, ms = 5367
  10. 6s: Delta ms = 6002, ms = 6367
  11. 7s: Delta ms = 7002, ms = 7367
  12. 8s: Delta ms = 8002, ms = 8367
  13. 9s: Delta ms = 9002, ms = 9367
  14. 10s: Delta ms = 10002, ms = 10367
  15. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10002
  16. 11s: Delta ms = 11002, ms = 11367
  17. 12s: Delta ms = 12002, ms = 12367
  18. 13s: Delta ms = 13002, ms = 13367
  19. 14s: Delta ms = 14002, ms = 14367
  20. 15s: Delta ms = 15002, ms = 15367
  21. 16s: Delta ms = 16002, ms = 16367
  22. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000
  23. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000
  24. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10001
  25. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000
  26. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000
  27. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000
  28. simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000

—-

Debug

Debug is enabled by default on Serial.

You can also change the debugging level (TIMERINTERRUPT_LOGLEVEL) from 0 to 4

  1. // These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
  2. // _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
  3. // Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
  4. #define TIMER_INTERRUPT_DEBUG 0
  5. #define _TIMERINTERRUPT_LOGLEVEL_ 0

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.

—-

Issues

Submit issues to: ESP8266TimerInterrupt issues

—-

TO DO

  1. Search for bug and improvement.

DONE

  1. Basic hardware timers for ESP8266.
  2. More hardware-initiated software-enabled timers
  3. Longer time interval
  4. Similar features for remaining Arduino boards such as AVR, Teensy, SAMD21, SAMD51, SAM-DUE, nRF52, ESP32, STM32, etc.
  5. Update to match new ESP8266 core v3.0.0
  6. Fix compiler errors due to conflict to some libraries.
  7. Add complex examples.
  8. Update to match new ESP8266 core v3.0.2
  9. Fix multiple-definitions linker error. Drop src_cpp and src_h directories
    1. Add feature to select among highest, medium or lowest accuracy for Timers for shortest, medium or longest time
    2. Convert to h-only style.
    3. Optimize code by using passing by reference instead of by value
    4. Add example multiFileProject to demo for multiple-file project
    5. Add example ISR_16_Timers_Array_OneShot to demo how to use one-shot ISR-based timer

—-

Contributions and thanks

  1. Thanks to Holger Lembke to report ESP8266TimerInterrupt Issue 8: ESP8266Timer and PWM —> wdt reset, leading to the HOWTO Use PWM analogWrite() with ESP8266 running Timer1 Interrupt notes.
  2. Thanks to Eugene to make bug-fixing PR and discussion in bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19, leading to v1.5.0
  3. Thanks to absalom-muc to make enhancement request in One shot operating mode #20, leading to v1.6.0 to add example to demo how to use one-shot ISR-based timer






holgerlembke
Holger Lembke

RushOnline
Eugene

absalom-muc
absalom-muc


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed under MIT

Copyright 2019- Khoi Hoang