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

Commit de54c1a

Browse files
authored
Add files via upload
1 parent 8be2871 commit de54c1a

File tree

6 files changed

+754
-4
lines changed

6 files changed

+754
-4
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
4040

4141
The timer1 counters can be configured to support automatic reload.
4242

43+
## New from v1.0.2
44+
45+
Now with these new `16 ISR-based timers`, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
46+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
47+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
48+
This important feature is absolutely necessary for mission-critical tasks.
49+
50+
The `ISR_Timer_Complex` example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual
51+
elapsed millisecs of each type of timers.
52+
Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
53+
and Blynk services. You can also have many `(up to 16)` timers to use.
54+
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
55+
You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
56+
in loop(), using delay() function as an example. The elapsed time then is very unaccurate
57+
4358
## Supported Boards
4459

4560
- ESP8266
@@ -109,16 +124,18 @@ void loop()
109124
```
110125
## TO DO
111126

112-
1. More hardware-initiated software-enabled timers
113-
2. Longer time interval
127+
1. Search for bug and improvement.
128+
2. Similar features for Arduino (UNO, Mega, etc...) and ESP32
114129

115130

116131
## DONE
117132

118-
For current version v1.0.1
133+
For current version v1.0.2
119134

120135
1. Basic hardware timers for ESP8266.
121136
2. Fix compatibility issue causing compiler error while using Arduino IDEs before 1.8.10 and ESP8266 cores 2.5.2 and before
137+
3. More hardware-initiated software-enabled timers
138+
4. Longer time interval
122139

123140

124141
## Contributing
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/****************************************************************************************************************************
2+
* examples/ISR_Timer_Complex.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.2
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+
* Notes:
21+
* Special design is necessary to share data between interrupt code and the rest of your program.
22+
* Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
23+
* variable can not spontaneously change. Because your function may change variables while your program is using them,
24+
* the compiler needs this hint. But volatile alone is often not enough.
25+
* When accessing shared variables, usually interrupts must be disabled. Even with volatile,
26+
* if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
27+
* If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
28+
* or the entire sequence of your code which accesses the data.
29+
*
30+
*****************************************************************************************************************************/
31+
32+
/****************************************************************************************************************************
33+
* This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
34+
* Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
35+
* and Blynk services. You can also have many (up to 16) timers to use.
36+
* This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
37+
* You'll see blynkTimer is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
38+
* In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
39+
* written
40+
*****************************************************************************************************************************/
41+
42+
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
43+
#define TIMER_INTERRUPT_DEBUG 1
44+
45+
#define BLYNK_PRINT Serial
46+
//#define BLYNK_DEBUG true
47+
48+
#include <ESP8266WiFi.h>
49+
50+
//#define USE_BLYNK_WM true
51+
#define USE_BLYNK_WM false
52+
53+
#define USE_SSL false
54+
55+
#if USE_BLYNK_WM
56+
#if USE_SSL
57+
#include <BlynkSimpleEsp8266_SSL_WM.h> //https://github.com/khoih-prog/Blynk_WM
58+
#else
59+
#include <BlynkSimpleEsp8266_WM.h> //https://github.com/khoih-prog/Blynk_WM
60+
#endif
61+
#else
62+
#if USE_SSL
63+
#include <BlynkSimpleEsp8266_SSL.h>
64+
#define BLYNK_HARDWARE_PORT 9443
65+
#else
66+
#include <BlynkSimpleEsp8266.h>
67+
#define BLYNK_HARDWARE_PORT 8080
68+
#endif
69+
#endif
70+
71+
#if !USE_BLYNK_WM
72+
#define USE_LOCAL_SERVER true
73+
74+
// If local server
75+
#if USE_LOCAL_SERVER
76+
char blynk_server[] = "khoih.duckdns.org";
77+
//char blynk_server[] = "192.168.2.110";
78+
#else
79+
char blynk_server[] = "";
80+
#endif
81+
82+
char auth[] = "un9Sv8k1q5xKpl5Lihs7a22Ixeq8MJ2J";
83+
char ssid[] = "HueNet1";
84+
char pass[] = "jenniqqs";
85+
86+
#endif
87+
88+
#include "ESP8266TimerInterrupt.h"
89+
#include "ESP8266_ISR_Timer.h"
90+
91+
#ifndef LED_BUILTIN
92+
#define LED_BUILTIN 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
93+
#endif
94+
95+
#define HW_TIMER_INTERVAL_MS 50
96+
97+
#define WIFI_TIMEOUT 20000L
98+
99+
volatile uint32_t lastMillis = 0;
100+
101+
// Init ESP32 timer 0
102+
ESP8266Timer ITimer;
103+
104+
// Init BlynkTimer
105+
ESP8266_ISR_Timer ISR_Timer;
106+
107+
BlynkTimer blynkTimer;
108+
109+
void ICACHE_RAM_ATTR TimerHandler(void)
110+
{
111+
static bool toggle = false;
112+
static bool started = false;
113+
114+
static int timeRun = 0;
115+
116+
ISR_Timer.run();
117+
118+
// Toggle LED every 50 x 100 = 5000ms = 5s
119+
if (++timeRun == 100)
120+
{
121+
timeRun = 0;
122+
123+
if (!started)
124+
{
125+
started = true;
126+
pinMode(LED_BUILTIN, OUTPUT);
127+
}
128+
129+
#if (TIMER_INTERRUPT_DEBUG > 0)
130+
Serial.println("Delta ms = " + String(millis() - lastMillis));
131+
lastMillis = millis();
132+
#endif
133+
134+
//timer interrupt toggles pin LED_BUILTIN
135+
digitalWrite(LED_BUILTIN, toggle);
136+
toggle = !toggle;
137+
}
138+
}
139+
140+
void ICACHE_RAM_ATTR doingSomething2s()
141+
{
142+
static unsigned long previousMillis = lastMillis;
143+
Serial.println("doingSomething2s: Delta ms = " + String(millis() - previousMillis));
144+
previousMillis = millis();
145+
}
146+
147+
void ICACHE_RAM_ATTR doingSomething5s()
148+
{
149+
static unsigned long previousMillis = lastMillis;
150+
Serial.println("doingSomething5s: Delta ms = " + String(millis() - previousMillis));
151+
previousMillis = millis();
152+
}
153+
154+
void ICACHE_RAM_ATTR doingSomething10s()
155+
{
156+
static unsigned long previousMillis = lastMillis;
157+
Serial.println("doingSomething10s: Delta ms = " + String(millis() - previousMillis));
158+
previousMillis = millis();
159+
}
160+
161+
void ICACHE_RAM_ATTR doingSomething50s()
162+
{
163+
static unsigned long previousMillis = lastMillis;
164+
Serial.println("doingSomething50s: Delta ms = " + String(millis() - previousMillis));
165+
previousMillis = millis();
166+
}
167+
168+
#define BLYNK_TIMER_MS 2000L
169+
170+
void blynkDoingSomething2s()
171+
{
172+
static unsigned long previousMillis = lastMillis;
173+
Serial.println("blynkDoingSomething2s: Delta programmed ms = " + String(BLYNK_TIMER_MS) + ", actual = " + String(millis() - previousMillis));
174+
previousMillis = millis();
175+
}
176+
177+
void setup()
178+
{
179+
Serial.begin(115200);
180+
Serial.println("\nStarting");
181+
182+
// Interval in microsecs
183+
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
184+
{
185+
lastMillis = millis();
186+
Serial.println("Starting ITimer OK, millis() = " + String(lastMillis));
187+
}
188+
else
189+
Serial.println("Can't set ITimer correctly. Select another freq. or interval");
190+
191+
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
192+
ISR_Timer.setInterval(2000L, doingSomething2s);
193+
ISR_Timer.setInterval(5000L, doingSomething5s);
194+
ISR_Timer.setInterval(10000L, doingSomething10s);
195+
ISR_Timer.setInterval(50000L, doingSomething50s);
196+
197+
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
198+
blynkTimer.setInterval(BLYNK_TIMER_MS, blynkDoingSomething2s);
199+
200+
201+
#if USE_BLYNK_WM
202+
Blynk.begin();
203+
#else
204+
unsigned long startWiFi = millis();
205+
206+
WiFi.begin(ssid, pass);
207+
208+
do
209+
{
210+
delay(200);
211+
if ( (WiFi.status() == WL_CONNECTED) || (millis() > startWiFi + WIFI_TIMEOUT) )
212+
break;
213+
} while (WiFi.status() != WL_CONNECTED);
214+
215+
Blynk.config(auth, blynk_server, BLYNK_HARDWARE_PORT);
216+
Blynk.connect();
217+
218+
if (Blynk.connected())
219+
Serial.println("Blynk connected");
220+
else
221+
Serial.println("Blynk not connected yet");
222+
#endif
223+
224+
}
225+
226+
#define BLOCKING_TIME_MS 3000L
227+
228+
void loop()
229+
{
230+
static unsigned long previousMillis = lastMillis;
231+
232+
Blynk.run();
233+
234+
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
235+
// You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer
236+
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
237+
// While that of ISR_Timer is still prefect.
238+
delay(BLOCKING_TIME_MS);
239+
240+
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
241+
// You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer.
242+
blynkTimer.run();
243+
}

keywords.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
ESP8266TimerInterrupt KEYWORD1
22
ESP8266Timer KEYWORD1
3+
ESP8266_ISR_Timer KEYWORD1
4+
ISRTimer KEYWORD1
35
setFrequency KEYWORD2
46
setInterval KEYWORD2
57
attachInterrupt KEYWORD2
@@ -10,3 +12,19 @@ reattachInterrupt KEYWORD2
1012
enableTimer KEYWORD2
1113
stopTimer KEYWORD2
1214
restartTimer KEYWORD2
15+
init KEYWORD2
16+
run KEYWORD2
17+
setTimeout KEYWORD2
18+
setTimer KEYWORD2
19+
changeInterval KEYWORD2
20+
deleteTimer KEYWORD2
21+
restartTimer KEYWORD2
22+
isEnabled KEYWORD2
23+
enable KEYWORD2
24+
disable KEYWORD2
25+
enableAll KEYWORD2
26+
disableAll KEYWORD2
27+
toggle KEYWORD2
28+
getNumTimers KEYWORD2
29+
getNumAvailableTimers KEYWORD2
30+

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP8266TimerInterrupt
2-
version=1.0.1
2+
version=1.0.2
33
author=Khoi Hoang
44
maintainer=Khoi Hoang
55
license=MIT

0 commit comments

Comments
 (0)