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

Commit 8f2d309

Browse files
authored
Add files via upload
1 parent e819a37 commit 8f2d309

File tree

10 files changed

+920
-0
lines changed

10 files changed

+920
-0
lines changed

README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.0
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+
************************************************/
21+
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
22+
#define TIMER_INTERRUPT_DEBUG 1
23+
24+
#include "ESP8266TimerInterrupt.h"
25+
26+
#ifndef LED_BUILTIN
27+
#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
28+
#endif
29+
30+
volatile uint32_t lastMillis = 0;
31+
32+
void IRAM_ATTR TimerHandler(void)
33+
{
34+
static bool toggle = false;
35+
static bool started = false;
36+
37+
if (!started)
38+
{
39+
started = true;
40+
pinMode(LED_BUILTIN, OUTPUT);
41+
}
42+
43+
#if (TIMER_INTERRUPT_DEBUG > 0)
44+
Serial.println("Delta ms = " + String(millis() - lastMillis));
45+
lastMillis = millis();
46+
#endif
47+
48+
//timer interrupt toggles pin LED_BUILTIN
49+
digitalWrite(LED_BUILTIN, toggle);
50+
toggle = !toggle;
51+
}
52+
53+
#define TIMER_INTERVAL_MS 1000
54+
55+
// Init ESP32 timer 0
56+
ESP8266Timer ITimer;
57+
58+
59+
void setup()
60+
{
61+
Serial.begin(115200);
62+
Serial.println("\nStarting");
63+
64+
// Interval in microsecs
65+
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
66+
{
67+
lastMillis = millis();
68+
Serial.println("Starting ITimer OK, millis() = " + String(lastMillis));
69+
}
70+
else
71+
Serial.println("Can't set ITimer correctly. Select another freq. or interval");
72+
73+
}
74+
75+
void loop()
76+
{
77+
78+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/************************************************
2+
* RPM_Measure.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: v1.0.0
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+
************************************************/
21+
/* RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
22+
* then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
23+
* Asssuming LOW is active.
24+
* For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
25+
* If the time between active state is less than 8ms => consider noise.
26+
* RPM = 60000 / (rotation time in ms)
27+
*
28+
* We use interrupt to detect whenever the SW is active, set a flag
29+
* then use timer to count the time between active state
30+
*/
31+
32+
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
33+
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
34+
#define TIMER_INTERRUPT_DEBUG 1
35+
36+
#include "ESP8266TimerInterrupt.h"
37+
38+
#define PIN_D1 5 // Pin D1 mapped to pin GPIO5 of ESP8266
39+
40+
unsigned int interruptPin = PIN_D1;
41+
42+
#define TIMER_INTERVAL_MS 1
43+
#define DEBOUNCING_INTERVAL_MS 80
44+
45+
#define LOCAL_DEBUG 1
46+
47+
// Init ESP8266 timer
48+
ESP8266Timer ITimer;
49+
50+
volatile unsigned long rotationTime = 0;
51+
float RPM = 0.00;
52+
float avgRPM = 0.00;
53+
54+
volatile int debounceCounter;
55+
56+
volatile bool activeState = false;
57+
58+
void IRAM_ATTR detectRotation(void)
59+
{
60+
activeState = true;
61+
}
62+
63+
void IRAM_ATTR TimerHandler()
64+
{
65+
static bool started = false;
66+
67+
if ( activeState )
68+
{
69+
// Reset to prepare for next round of interrupt
70+
activeState = false;
71+
72+
if (debounceCounter >= DEBOUNCING_INTERVAL_MS / TIMER_INTERVAL_MS )
73+
{
74+
75+
//min time between pulses has passed
76+
RPM = (float) ( 60000.0f / ( rotationTime * TIMER_INTERVAL_MS ) );
77+
78+
avgRPM = ( 2 * avgRPM + RPM) / 3,
79+
80+
Serial.println("RPM = " + String(avgRPM) + ", rotationTime ms = " + String(rotationTime * TIMER_INTERVAL_MS) );
81+
82+
rotationTime = 0;
83+
debounceCounter = 0;
84+
}
85+
else
86+
debounceCounter++;
87+
}
88+
else
89+
{
90+
debounceCounter++;
91+
}
92+
93+
if (rotationTime >= 5000)
94+
{
95+
// If idle, set RPM to 0, don't increase rotationTime
96+
RPM = 0;
97+
Serial.println("RPM = " + String(RPM) + ", rotationTime = " + String(rotationTime) );
98+
rotationTime = 0;
99+
}
100+
else
101+
{
102+
rotationTime++;
103+
}
104+
}
105+
106+
void setup()
107+
{
108+
Serial.begin(115200);
109+
Serial.println("\nStarting");
110+
111+
pinMode(interruptPin, INPUT_PULLUP);
112+
113+
// Interval in microsecs
114+
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS, TimerHandler))
115+
Serial.println("Starting ITimer OK, millis() = " + String(millis()));
116+
else
117+
Serial.println("Can't set ITimer. Select another freq. or interval");
118+
119+
// Assumming the interruptPin will go LOW
120+
attachInterrupt(digitalPinToInterrupt(interruptPin), detectRotation, FALLING);
121+
}
122+
123+
void loop()
124+
{
125+
126+
}

0 commit comments

Comments
 (0)