Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 7e7995c

Browse files
authored
v1.0.3
### Releases v1.0.3 1. Restructure code. 2. Fix example. 3. Enhance README.
1 parent 2683921 commit 7e7995c

File tree

16 files changed

+1541
-784
lines changed

16 files changed

+1541
-784
lines changed

README.md

Lines changed: 107 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,115 @@
66
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
77
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESP8266TimerInterrupt.svg)](http://github.com/khoih-prog/ESP8266TimerInterrupt/issues)
88

9+
### Releases v1.0.3
10+
11+
1. Restructure code.
12+
2. Fix example.
13+
3. Enhance README.
14+
15+
### Releases v1.0.2
16+
17+
1. Basic hardware timers for ESP8266.
18+
2. Fix compatibility issue causing compiler error while using Arduino IDEs before 1.8.10 and ESP8266 cores 2.5.2 and before
19+
3. More hardware-initiated software-enabled timers
20+
4. Longer time interval
21+
22+
## Features
23+
924
This library enables you to use Interrupt from Hardware Timers on an ESP8266-based board.
1025

11-
Why do we need this Hardware Timer Interrupt?
26+
#### Why do we need this Hardware Timer Interrupt?
1227

13-
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().
28+
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().
1429

15-
So your function might not be executed, and the result would be disastrous.
30+
So your function ***might not be executed, and the result would be disastrous.***
1631

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

19-
The correct choice is to use a Hardware Timer with Interrupt to call your function.
34+
The correct choice is to use a Hardware Timer with ***Interrupt*** to call your function.
2035

21-
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.
36+
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.
2237

2338
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.
2439

25-
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:
40+
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:
2641

27-
[Attach Interrupt](https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/)
42+
[Howto Attach Interrupt](https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/)
2843

29-
**Important Notes:**
30-
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.
44+
#### Important Notes:
45+
46+
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.***
3147

3248
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.
3349

50+
## Prerequisite
51+
1. [`Arduino IDE 1.8.12 or later` for Arduino](https://www.arduino.cc/en/Main/Software)
52+
2. [`ESP8266 core 2.6.3 or later` for Arduino](https://github.com/esp8266/Arduino#installing-with-boards-manager) for ESP8266 boards.
53+
3454
### Installation
3555

56+
The suggested way to install is to:
57+
3658
#### Use Arduino Library Manager
3759

38-
The suggested and easiest way is to use `Arduino Library Manager`. Search for `ESP8266TimerInterrupt`, then select / install the latest version.
60+
The best 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](https://www.ardu-badge.com/badge/ESP8266TimerInterrupt.svg?)](https://www.ardu-badge.com/ESP8266TimerInterrupt) for more detailed instructions.
3961

4062
#### Manual Install
41-
1. Navigate to [ESP8266TimerInterrupt](https://github.com/khoih-prog/ESP8266TimerInterrupt).
63+
64+
1. Navigate to [ESP8266TimerInterrupt](https://github.com/khoih-prog/ESP8266TimerInterrupt) page.
4265
2. Download the latest release `ESP8266TimerInterrupt-master.zip`.
4366
3. Extract the zip file to `ESP8266TimerInterrupt-master` directory
44-
4. Copy whole folder to Arduino libraries' directory such as `.Arduino/libraries/ESP8266TimerInterrupt-master`.
67+
4. Copy whole
68+
- `ESP8266TimerInterrupt-master/src` folder to Arduino libraries' directory such as `~/Arduino/libraries/`.
69+
4570

4671
## More useful Information
4772

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

5378
The timer1 counters can be configured to support automatic reload.
5479

5580
## New from v1.0.2
5681

57-
Now with these new `16 ISR-based timers`, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
58-
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
59-
Therefore, their executions are not blocked by bad-behaving functions / tasks.
82+
Now with these new ***`16 ISR-based timers`***, the maximum interval is ***practically unlimited*** (limited only by unsigned long miliseconds).
83+
84+
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.
85+
6086
This important feature is absolutely necessary for mission-critical tasks.
6187

62-
The `ISR_Timer_Complex` example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual
63-
elapsed millisecs of each type of timers.
64-
Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
65-
and Blynk services. You can also have many `(up to 16)` timers to use.
66-
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
67-
You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
68-
in loop(), using delay() function as an example. The elapsed time then is very unaccurate
88+
The [ISR_Timer_Complex example](examples/ISR_Timer_Complex) will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.
89+
90+
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.
91+
92+
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
6993

70-
## Supported Boards
94+
### Also see examples:
95+
96+
1. [Argument_None](examples/Argument_None)
97+
2. [ISR_RPM_Measure](examples/ISR_RPM_Measure)
98+
3. [ISR_Switch](examples/ISR_Switch)
99+
4. [ISR_Timer_4_Switches](examples/ISR_Timer_4_Switches)
100+
5. [ISR_Timer_Complex](examples/ISR_Timer_Complex)
101+
6. [ISR_Timer_Switch](examples/ISR_Timer_Switch)
102+
7. [ISR_Timer_Switches](examples/ISR_Timer_Switches)
103+
8. [RPM_Measure](examples/RPM_Measure)
104+
9. [SwitchDebounce](examples/SwitchDebounce)
105+
10. [TimerInterruptTest](examples/TimerInterruptTest)
106+
107+
### Supported Boards
71108

72109
- ESP8266
73110

74-
## Usage
111+
### Example [Argument_None](examples/Argument_None)
75112

76-
How to use:
113+
```cpp
114+
#if !defined(ESP8266)
115+
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
116+
#endif
77117

78-
```
79118
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
80119
#define TIMER_INTERRUPT_DEBUG 1
81120

@@ -98,58 +137,83 @@ void ICACHE_RAM_ATTR TimerHandler(void)
98137
pinMode(LED_BUILTIN, OUTPUT);
99138
}
100139

101-
#if (TIMER_INTERRUPT_DEBUG > 0)
102-
if (lastMillis != 0)
103-
Serial.println("Delta ms = " + String(millis() - lastMillis));
140+
#if (TIMER_INTERRUPT_DEBUG > 0)
141+
Serial.println("Delta ms = " + String(millis() - lastMillis));
104142
lastMillis = millis();
105-
#endif
106-
143+
#endif
144+
107145
//timer interrupt toggles pin LED_BUILTIN
108146
digitalWrite(LED_BUILTIN, toggle);
109147
toggle = !toggle;
110148
}
111149

112-
#define TIMER_INTERVAL_MS 1000
150+
#define TIMER_INTERVAL_MS 1000
113151

114-
// Init ESP8266 timer 0
152+
// Init ESP32 timer 0
115153
ESP8266Timer ITimer;
116154

117155

118156
void setup()
119157
{
120158
Serial.begin(115200);
121-
Serial.println("\nStarting");
159+
while (!Serial);
122160

161+
Serial.println("\nStarting Argument_None");
162+
123163
// Interval in microsecs
124164
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
125-
Serial.println("Starting ITimer OK, millis() = " + String(millis()));
165+
{
166+
lastMillis = millis();
167+
Serial.println("Starting ITimer OK, millis() = " + String(lastMillis));
168+
}
126169
else
127170
Serial.println("Can't set ITimer correctly. Select another freq. or interval");
128171

129172
}
130173

131174
void loop()
132175
{
133-
176+
134177
}
135178

136179
```
137-
## TO DO
138180
139-
1. Search for bug and improvement.
181+
### Releases v1.0.3
140182
141-
## DONE
183+
1. Restructure code.
184+
2. Fix example.
185+
3. Enhance README.
142186
143-
For current version v1.0.2
187+
### Releases v1.0.2
144188
145189
1. Basic hardware timers for ESP8266.
146190
2. Fix compatibility issue causing compiler error while using Arduino IDEs before 1.8.10 and ESP8266 cores 2.5.2 and before
147191
3. More hardware-initiated software-enabled timers
148192
4. Longer time interval
149-
5. Similar features for Arduino (UNO, Mega, etc...) and ESP32
150193
194+
### Releases v1.0.1
195+
196+
1. Fixing compiler error
197+
198+
### Releases v1.0.0
199+
200+
1. Initial coding
201+
202+
203+
## TO DO
204+
205+
1. Search for bug and improvement.
206+
2. Similar features for remaining Arduino boards suh as SAMD21, SAMD51, SAM-DUE, nRF52
207+
208+
## DONE
209+
210+
1. Similar features for Arduino (UNO, Mega, etc...) and ESP32
211+
212+
213+
### Contributions and thanks
151214
152215
## Contributing
216+
153217
If you want to contribute to this project:
154218
- Report bugs and errors
155219
- Ask for enhancements
Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
1-
/************************************************
2-
* examples/Argument_None.ino
3-
* For ESP8266 boards
4-
* Written by Khoi Hoang
5-
*
6-
* Built by Khoi Hoang https://github.com/khoih-prog/ESP32TimerInterrupt
7-
* Licensed under MIT license
8-
* Version: v1.0.1
9-
*
10-
* Notes:
11-
* Special design is necessary to share data between interrupt code and the rest of your program.
12-
* Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
13-
* variable can not spontaneously change. Because your function may change variables while your program is using them,
14-
* the compiler needs this hint. But volatile alone is often not enough.
15-
* When accessing shared variables, usually interrupts must be disabled. Even with volatile,
16-
* if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
17-
* If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
18-
* or the entire sequence of your code which accesses the data.
19-
*
20-
************************************************/
1+
/****************************************************************************************************************************
2+
Argument_None.ino
3+
For ESP8266 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
7+
Licensed under MIT license
8+
Version: 1.0.3
9+
10+
The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
11+
The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
12+
The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
13+
Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
14+
15+
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
16+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
17+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
18+
This important feature is absolutely necessary for mission-critical tasks.
19+
20+
Based on SimpleTimer - A timer library for Arduino.
21+
Author: mromani@ottotecnica.com
22+
Copyright (c) 2010 OTTOTECNICA Italy
23+
24+
Based on BlynkTimer.h
25+
Author: Volodymyr Shymanskyy
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 26/11/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
32+
1.0.3 K.Hoang 17/05/2020 Restructure code. Fix example. Enhance README.
33+
*****************************************************************************************************************************/
34+
35+
/* Notes:
36+
Special design is necessary to share data between interrupt code and the rest of your program.
37+
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
38+
variable can not spontaneously change. Because your function may change variables while your program is using them,
39+
the compiler needs this hint. But volatile alone is often not enough.
40+
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
41+
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
42+
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
43+
or the entire sequence of your code which accesses the data.
44+
*/
45+
46+
#if !defined(ESP8266)
47+
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
48+
#endif
49+
2150
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
2251
#define TIMER_INTERRUPT_DEBUG 1
2352

@@ -40,17 +69,17 @@ void ICACHE_RAM_ATTR TimerHandler(void)
4069
pinMode(LED_BUILTIN, OUTPUT);
4170
}
4271

43-
#if (TIMER_INTERRUPT_DEBUG > 0)
72+
#if (TIMER_INTERRUPT_DEBUG > 0)
4473
Serial.println("Delta ms = " + String(millis() - lastMillis));
4574
lastMillis = millis();
46-
#endif
47-
75+
#endif
76+
4877
//timer interrupt toggles pin LED_BUILTIN
4978
digitalWrite(LED_BUILTIN, toggle);
5079
toggle = !toggle;
5180
}
5281

53-
#define TIMER_INTERVAL_MS 1000
82+
#define TIMER_INTERVAL_MS 1000
5483

5584
// Init ESP32 timer 0
5685
ESP8266Timer ITimer;
@@ -59,8 +88,10 @@ ESP8266Timer ITimer;
5988
void setup()
6089
{
6190
Serial.begin(115200);
62-
Serial.println("\nStarting");
91+
while (!Serial);
6392

93+
Serial.println("\nStarting Argument_None");
94+
6495
// Interval in microsecs
6596
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
6697
{
@@ -74,5 +105,5 @@ void setup()
74105

75106
void loop()
76107
{
77-
108+
78109
}

0 commit comments

Comments
 (0)