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

Commit 71d1efd

Browse files
authored
Add files via upload
1 parent 8d3b887 commit 71d1efd

File tree

9 files changed

+1086
-0
lines changed

9 files changed

+1086
-0
lines changed

README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
## 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+
58+
## Supported Boards
59+
60+
- ESP8266
61+
62+
## Usage
63+
64+
How to use:
65+
66+
```
67+
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
68+
#define TIMER_INTERRUPT_DEBUG 1
69+
70+
#include "ESP8266TimerInterrupt.h"
71+
72+
#ifndef LED_BUILTIN
73+
#define LED_BUILTIN 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
74+
#endif
75+
76+
volatile uint32_t lastMillis = 0;
77+
78+
void ICACHE_RAM_ATTR TimerHandler(void)
79+
{
80+
static bool toggle = false;
81+
static bool started = false;
82+
83+
if (!started)
84+
{
85+
started = true;
86+
pinMode(LED_BUILTIN, OUTPUT);
87+
}
88+
89+
#if (TIMER_INTERRUPT_DEBUG > 0)
90+
if (lastMillis != 0)
91+
Serial.println("Delta ms = " + String(millis() - lastMillis));
92+
lastMillis = millis();
93+
#endif
94+
95+
//timer interrupt toggles pin LED_BUILTIN
96+
digitalWrite(LED_BUILTIN, toggle);
97+
toggle = !toggle;
98+
}
99+
100+
#define TIMER_INTERVAL_MS 1000
101+
102+
// Init ESP8266 timer 0
103+
ESP8266Timer ITimer;
104+
105+
106+
void setup()
107+
{
108+
Serial.begin(115200);
109+
Serial.println("\nStarting");
110+
111+
// Interval in microsecs
112+
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
113+
Serial.println("Starting ITimer OK, millis() = " + String(millis()));
114+
else
115+
Serial.println("Can't set ITimer correctly. Select another freq. or interval");
116+
117+
}
118+
119+
void loop()
120+
{
121+
122+
}
123+
124+
```
125+
## TO DO
126+
127+
1. Search for bug and improvement.
128+
2. Similar features for Arduino (UNO, Mega, etc...) and ESP32
129+
130+
131+
## DONE
132+
133+
For current version v1.0.2
134+
135+
1. Basic hardware timers for ESP8266.
136+
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
139+
140+
141+
## Contributing
142+
If you want to contribute to this project:
143+
- Report bugs and errors
144+
- Ask for enhancements
145+
- Create issues and pull requests
146+
- Tell other people about this library
147+
148+
## Copyright
149+
Copyright 2019- Khoi Hoang
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/****************************************************************************************************************************
2+
* examples/ISR_MultiServos.ino
3+
* For ESP8266 boards
4+
* Written by Khoi Hoang
5+
*
6+
* Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo
7+
* Licensed under MIT license
8+
* Version: v1.0.0
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 these new 16 ISR-based PWM servo contro uses only 1 hardware timer.
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+
* Version Modified By Date Comments
31+
* ------- ----------- ---------- -----------
32+
* 1.0.0 K Hoang 04/12/2019 Initial release
33+
*****************************************************************************************************************************/
34+
35+
/****************************************************************************************************************************
36+
* This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
37+
* Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
38+
* and Blynk services. You can also have many (up to 16) timers to use.
39+
* This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
40+
* You'll see blynkTimer is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
41+
* In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
42+
* written
43+
*
44+
* From ESP32 Servo Example Using Arduino ESP32 Servo Library
45+
* John K. Bennett
46+
* March, 2017
47+
*
48+
* Different servos require different pulse widths to vary servo angle, but the range is
49+
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
50+
* sweep 180 degrees, so the lowest number in the published range for a particular servo
51+
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
52+
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
53+
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
54+
* degrees.
55+
*
56+
* Circuit:
57+
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
58+
* the ground wire is typically black or brown, and the signal wire is typically yellow,
59+
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
60+
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
61+
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
62+
*
63+
* We could also connect servo power to a separate external
64+
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
65+
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
66+
* connect to any available GPIO pins on the ESP32 (in this example, we use pins
67+
* 22, 19, 23, & 18).
68+
*
69+
* In this example, we assume four Tower Pro SG90 small servos.
70+
* The published min and max for this servo are 500 and 2400, respectively.
71+
* These values actually drive the servos a little past 0 and 180, so
72+
* if you are particular, adjust the min and max values to match your needs.
73+
* Experimentally, 550 and 2350 are pretty close to 0 and 180.*
74+
*****************************************************************************************************************************/
75+
76+
#define TIMER_INTERRUPT_DEBUG 1
77+
#define ISR_SERVO_DEBUG 1
78+
79+
#include "ESP8266_ISR_Servo.h"
80+
81+
82+
int servoIndex1 = -1;
83+
int servoIndex2 = -1;
84+
85+
void setup()
86+
{
87+
Serial.begin(115200);
88+
Serial.println("\nStarting");
89+
90+
servoIndex1 = ISR_Servo.setupServo(D8);
91+
servoIndex2 = ISR_Servo.setupServo(D7);
92+
93+
if (servoIndex1 != -1)
94+
Serial.println("Setup Servo1 OK");
95+
else
96+
Serial.println("Setup Servo1 failed");
97+
98+
if (servoIndex2 != -1)
99+
Serial.println("Setup Servo2 OK");
100+
else
101+
Serial.println("Setup Servo2 failed");
102+
}
103+
104+
void loop()
105+
{
106+
int position;
107+
108+
if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) )
109+
{
110+
for (position = 0; position <= 180; position++)
111+
{
112+
// goes from 0 degrees to 180 degrees
113+
// in steps of 1 degree
114+
ISR_Servo.setPosition(servoIndex1, position);
115+
ISR_Servo.setPosition(servoIndex2, 180 - position);
116+
// waits 15ms for the servo to reach the position
117+
delay(50 /*15*/);
118+
}
119+
120+
for (position = 180; position >= 0; position--)
121+
{
122+
// goes from 180 degrees to 0 degrees
123+
ISR_Servo.setPosition(servoIndex1, position);
124+
ISR_Servo.setPosition(servoIndex2, 180 - position);
125+
// waits 15ms for the servo to reach the position
126+
delay(50 /*15*/);
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)