for real powermeter

This commit is contained in:
hidaba
2023-12-23 16:54:56 +01:00
parent ea400ca61f
commit c2d5bae2e9
25 changed files with 4357 additions and 0 deletions

View File

@ -0,0 +1,104 @@
/****************************************************************************************************************************
Argument_None.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/* Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
#endif
volatile uint32_t lastMillis = 0;
void IRAM_ATTR TimerHandler()
{
static bool toggle = false;
static bool started = false;
if (!started)
{
started = true;
pinMode(LED_BUILTIN, OUTPUT);
}
#if (TIMER_INTERRUPT_DEBUG > 0)
Serial.print("Delta ms = "); Serial.println(millis() - lastMillis);
lastMillis = millis();
#endif
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
#define TIMER_INTERVAL_MS 1000
// Init ESP8266 timer 1
ESP8266Timer ITimer;
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting Argument_None on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
lastMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(lastMillis);
}
else
Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));
}
void loop()
{
}

View File

@ -0,0 +1,130 @@
/****************************************************************************************************************************
Change_Interval.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/*
Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN D4 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
#endif
#define TIMER_INTERVAL_MS 500 //1000
volatile uint32_t TimerCount = 0;
// Init ESP8266 timer 1
ESP8266Timer ITimer;
void printResult(uint32_t currTime)
{
Serial.print(F("Time = ")); Serial.print(currTime);
Serial.print(F(", TimerCount = ")); Serial.println(TimerCount);
}
void TimerHandler()
{
static bool toggle = false;
// Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
TimerCount++;
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting Change_Interval on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
}
#define CHECK_INTERVAL_MS 10000L
#define CHANGE_INTERVAL_MS 20000L
void loop()
{
static uint32_t lastTime = 0;
static uint32_t lastChangeTime = 0;
static uint32_t currTime;
static uint32_t multFactor = 0;
currTime = millis();
if (currTime - lastTime > CHECK_INTERVAL_MS)
{
printResult(currTime);
lastTime = currTime;
if (currTime - lastChangeTime > CHANGE_INTERVAL_MS)
{
//setInterval(unsigned long interval, timerCallback callback)
multFactor = (multFactor + 1) % 2;
ITimer.setInterval(TIMER_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler);
Serial.print(F("Changing Interval, Timer = ")); Serial.println(TIMER_INTERVAL_MS * (multFactor + 1));
lastChangeTime = currTime;
}
}
}

View File

@ -0,0 +1,438 @@
/****************************************************************************************************************************
ISR_16_Timers_Array.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/*
Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
Asssuming LOW is active.
For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
If the time between active state is less than 8ms => consider noise.
RPM = 60000 / (rotation time in ms)
We use interrupt to detect whenever the SW is active, set a flag then use timer to count the time between active state
This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
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 is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
written
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 2
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#include "ESP8266_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#ifndef LED_BLUE
#define LED_BLUE 25
#endif
#ifndef LED_RED
#define LED_RED 27
#endif
#define HW_TIMER_INTERVAL_MS 1L
volatile uint32_t startMillis = 0;
// Init ESP8266 timer 1
ESP8266Timer ITimer;
// Init BlynkTimer
ESP8266_ISR_Timer ISR_Timer;
#define LED_TOGGLE_INTERVAL_MS 2000L
void IRAM_ATTR TimerHandler()
{
static bool toggle = false;
static bool started = false;
static int timeRun = 0;
ISR_Timer.run();
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s
if (++timeRun == (LED_TOGGLE_INTERVAL_MS / HW_TIMER_INTERVAL_MS) )
{
timeRun = 0;
if (!started)
{
started = true;
pinMode(LED_BUILTIN, OUTPUT);
}
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
}
#define NUMBER_ISR_TIMERS 16
// You can assign any interval for any timer here, in milliseconds
uint32_t TimerInterval[NUMBER_ISR_TIMERS] =
{
1000L, 2000L, 3000L, 4000L, 5000L, 6000L, 7000L, 8000L,
9000L, 10000L, 11000L, 12000L, 13000L, 14000L, 15000L, 16000L
};
typedef void (*irqCallback) ();
#if (TIMER_INTERRUPT_DEBUG > 0)
void printStatus(uint16_t index, unsigned long deltaMillis, unsigned long currentMillis)
{
Serial.print(TimerInterval[index] / 1000); Serial.print("s: Delta ms = "); Serial.print(deltaMillis);
Serial.print(", ms = "); Serial.println(currentMillis);
}
#endif
// In SAMD, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething0()
{
#if (TIMER_INTERRUPT_DEBUG > 0)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(0, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething1()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(1, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething2()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(2, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething3()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(3, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething4()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(4, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething5()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(5, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething6()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(6, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething7()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(7, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething8()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(8, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething9()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(9, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething10()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(10, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething11()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(11, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething12()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(12, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething13()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(13, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething14()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(14, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething15()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(15, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] =
{
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
doingSomething8, doingSomething9, doingSomething10, doingSomething11,
doingSomething12, doingSomething13, doingSomething14, doingSomething15
};
////////////////////////////////////////////////
#define SIMPLE_TIMER_MS 2000L
// Init SimpleTimer
SimpleTimer simpleTimer;
// Here is software Timer, you can do somewhat fancy stuffs without many issues.
// But always avoid
// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead
// 2. Very long "do", "while", "for" loops without predetermined exit time.
void simpleTimerDoingSomething2s()
{
static unsigned long previousMillis = startMillis;
Serial.print(F("simpleTimerDoingSomething2s: Delta programmed ms = ")); Serial.print(SIMPLE_TIMER_MS);
Serial.print(F(", actual = ")); Serial.println(millis() - previousMillis);
previousMillis = millis();
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting ISR_16_Timers_Array on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
{
startMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis);
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each SAMD_ISR_Timer
for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++)
{
ISR_Timer.setInterval(TimerInterval[i], irqCallbackFunc[i]);
}
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s);
}
#define BLOCKING_TIME_MS 10000L
void loop()
{
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
// You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
// While that of ISR_Timer is still prefect.
delay(BLOCKING_TIME_MS);
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
// You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer.
simpleTimer.run();
}

View File

@ -0,0 +1,367 @@
/****************************************************************************************************************************
ISR_16_Timers_Array_Complex.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/*
Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
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 is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
written
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#include "ESP8266_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#ifndef LED_BLUE
#define LED_BLUE 25
#endif
#ifndef LED_RED
#define LED_RED 27
#endif
#define HW_TIMER_INTERVAL_US 10000L
volatile uint32_t startMillis = 0;
// Init ESP8266 timer 1
ESP8266Timer ITimer;
// Init ESP8266_ISR_Timer
ESP8266_ISR_Timer ISR_Timer;
#define LED_TOGGLE_INTERVAL_MS 2000L
void IRAM_ATTR TimerHandler()
{
static bool toggle = false;
static int timeRun = 0;
ISR_Timer.run();
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s
if (++timeRun == ((LED_TOGGLE_INTERVAL_MS * 1000) / HW_TIMER_INTERVAL_US) )
{
timeRun = 0;
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
}
/////////////////////////////////////////////////
#define NUMBER_ISR_TIMERS 16
typedef void (*irqCallback) ();
/////////////////////////////////////////////////
#define USE_COMPLEX_STRUCT true
#if USE_COMPLEX_STRUCT
typedef struct
{
irqCallback irqCallbackFunc;
uint32_t TimerInterval;
unsigned long deltaMillis;
unsigned long previousMillis;
} ISRTimerData;
// In ESP8266, avoid doing something fancy in ISR, for example Serial.print()
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething(int index);
#else
volatile unsigned long deltaMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// You can assign any interval for any timer here, in milliseconds
uint32_t TimerInterval[NUMBER_ISR_TIMERS] =
{
5000L, 10000L, 15000L, 20000L, 25000L, 30000L, 35000L, 40000L,
45000L, 50000L, 55000L, 60000L, 65000L, 70000L, 75000L, 80000L
};
void doingSomething(int index)
{
unsigned long currentMillis = millis();
deltaMillis[index] = currentMillis - previousMillis[index];
previousMillis[index] = currentMillis;
}
#endif
////////////////////////////////////
// Shared
////////////////////////////////////
void doingSomething0()
{
doingSomething(0);
}
void doingSomething1()
{
doingSomething(1);
}
void doingSomething2()
{
doingSomething(2);
}
void doingSomething3()
{
doingSomething(3);
}
void doingSomething4()
{
doingSomething(4);
}
void doingSomething5()
{
doingSomething(5);
}
void doingSomething6()
{
doingSomething(6);
}
void doingSomething7()
{
doingSomething(7);
}
void doingSomething8()
{
doingSomething(8);
}
void doingSomething9()
{
doingSomething(9);
}
void doingSomething10()
{
doingSomething(10);
}
void doingSomething11()
{
doingSomething(11);
}
void doingSomething12()
{
doingSomething(12);
}
void doingSomething13()
{
doingSomething(13);
}
void doingSomething14()
{
doingSomething(14);
}
void doingSomething15()
{
doingSomething(15);
}
#if USE_COMPLEX_STRUCT
ISRTimerData curISRTimerData[NUMBER_ISR_TIMERS] =
{
//irqCallbackFunc, TimerInterval, deltaMillis, previousMillis
{ doingSomething0, 5000L, 0, 0 },
{ doingSomething1, 10000L, 0, 0 },
{ doingSomething2, 15000L, 0, 0 },
{ doingSomething3, 20000L, 0, 0 },
{ doingSomething4, 25000L, 0, 0 },
{ doingSomething5, 30000L, 0, 0 },
{ doingSomething6, 35000L, 0, 0 },
{ doingSomething7, 40000L, 0, 0 },
{ doingSomething8, 45000L, 0, 0 },
{ doingSomething9, 50000L, 0, 0 },
{ doingSomething10, 55000L, 0, 0 },
{ doingSomething11, 60000L, 0, 0 },
{ doingSomething12, 65000L, 0, 0 },
{ doingSomething13, 70000L, 0, 0 },
{ doingSomething14, 75000L, 0, 0 },
{ doingSomething15, 80000L, 0, 0 }
};
void doingSomething(int index)
{
unsigned long currentMillis = millis();
curISRTimerData[index].deltaMillis = currentMillis - curISRTimerData[index].previousMillis;
curISRTimerData[index].previousMillis = currentMillis;
}
#else
irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] =
{
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
doingSomething8, doingSomething9, doingSomething10, doingSomething11,
doingSomething12, doingSomething13, doingSomething14, doingSomething15
};
#endif
///////////////////////////////////////////
#define SIMPLE_TIMER_MS 2000L
// Init SimpleTimer
SimpleTimer simpleTimer;
// Here is software Timer, you can do somewhat fancy stuffs without many issues.
// But always avoid
// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead
// 2. Very long "do", "while", "for" loops without predetermined exit time.
void simpleTimerDoingSomething2s()
{
static unsigned long previousMillis = startMillis;
unsigned long currMillis = millis();
Serial.print(F("SimpleTimer : ")); Serial.print(SIMPLE_TIMER_MS / 1000);
Serial.print(F(", ms : ")); Serial.print(currMillis);
Serial.print(F(", Dms : ")); Serial.println(currMillis - previousMillis);
for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++)
{
#if USE_COMPLEX_STRUCT
Serial.print(F("Timer : ")); Serial.print(i);
Serial.print(F(", programmed : ")); Serial.print(curISRTimerData[i].TimerInterval);
Serial.print(F(", actual : ")); Serial.println(curISRTimerData[i].deltaMillis);
#else
Serial.print(F("Timer : ")); Serial.print(i);
Serial.print(F(", programmed : ")); Serial.print(TimerInterval[i]);
Serial.print(F(", actual : ")); Serial.println(deltaMillis[i]);
#endif
}
previousMillis = currMillis;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting ISR_16_Timers_Array_Complex on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler))
{
startMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis);
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
startMillis = millis();
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each ISR_Timer
for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++)
{
#if USE_COMPLEX_STRUCT
curISRTimerData[i].previousMillis = startMillis;
ISR_Timer.setInterval(curISRTimerData[i].TimerInterval, curISRTimerData[i].irqCallbackFunc);
#else
previousMillis[i] = millis();
ISR_Timer.setInterval(TimerInterval[i], irqCallbackFunc[i]);
#endif
}
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s);
}
#define BLOCKING_TIME_MS 10000L
void loop()
{
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
// You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
// While that of ISR_Timer is still prefect.
delay(BLOCKING_TIME_MS);
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
// You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer.
simpleTimer.run();
}

View File

@ -0,0 +1,440 @@
/****************************************************************************************************************************
ISR_16_Timers_Array_OneShot.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/*
Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
Asssuming LOW is active.
For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
If the time between active state is less than 8ms => consider noise.
RPM = 60000 / (rotation time in ms)
We use interrupt to detect whenever the SW is active, set a flag then use timer to count the time between active state
This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
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 is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
written
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 2
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#include "ESP8266_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#ifndef LED_BLUE
#define LED_BLUE 25
#endif
#ifndef LED_RED
#define LED_RED 27
#endif
#define HW_TIMER_INTERVAL_MS 1L
volatile uint32_t startMillis = 0;
// Init ESP8266 timer 1
ESP8266Timer ITimer;
// Init BlynkTimer
ESP8266_ISR_Timer ISR_Timer;
#define LED_TOGGLE_INTERVAL_MS 2000L
void IRAM_ATTR TimerHandler()
{
static bool toggle = false;
static bool started = false;
static int timeRun = 0;
ISR_Timer.run();
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s
if (++timeRun == (LED_TOGGLE_INTERVAL_MS / HW_TIMER_INTERVAL_MS) )
{
timeRun = 0;
if (!started)
{
started = true;
pinMode(LED_BUILTIN, OUTPUT);
}
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
}
#define NUMBER_ISR_TIMERS 16
// You can assign any interval for any timer here, in milliseconds
uint32_t TimerInterval[NUMBER_ISR_TIMERS] =
{
1000L, 2000L, 3000L, 4000L, 5000L, 6000L, 7000L, 8000L,
9000L, 10000L, 11000L, 12000L, 13000L, 14000L, 15000L, 16000L
};
typedef void (*irqCallback) ();
#if (TIMER_INTERRUPT_DEBUG > 0)
void printStatus(uint16_t index, unsigned long deltaMillis, unsigned long currentMillis)
{
Serial.print(TimerInterval[index] / 1000); Serial.print("s: Delta ms = "); Serial.print(deltaMillis);
Serial.print(", ms = "); Serial.println(currentMillis);
}
#endif
// In SAMD, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething0()
{
#if (TIMER_INTERRUPT_DEBUG > 0)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(0, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething1()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(1, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething2()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(2, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething3()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(3, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething4()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(4, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething5()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(5, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething6()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(6, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething7()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(7, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething8()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(8, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething9()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(9, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething10()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(10, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething11()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(11, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething12()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(12, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething13()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(13, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething14()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(14, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
void doingSomething15()
{
#if (TIMER_INTERRUPT_DEBUG > 1)
static unsigned long previousMillis = startMillis;
unsigned long currentMillis = millis();
unsigned long deltaMillis = currentMillis - previousMillis;
printStatus(15, deltaMillis, currentMillis);
previousMillis = currentMillis;
#endif
}
irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] =
{
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
doingSomething8, doingSomething9, doingSomething10, doingSomething11,
doingSomething12, doingSomething13, doingSomething14, doingSomething15
};
////////////////////////////////////////////////
#define SIMPLE_TIMER_MS 2000L
// Init SimpleTimer
SimpleTimer simpleTimer;
// Here is software Timer, you can do somewhat fancy stuffs without many issues.
// But always avoid
// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead
// 2. Very long "do", "while", "for" loops without predetermined exit time.
void simpleTimerDoingSomething2s()
{
static unsigned long previousMillis = startMillis;
Serial.print(F("simpleTimerDoingSomething2s: Delta programmed ms = ")); Serial.print(SIMPLE_TIMER_MS);
Serial.print(F(", actual = ")); Serial.println(millis() - previousMillis);
previousMillis = millis();
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting ISR_16_Timers_Array_OneShot on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
{
startMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(startMillis);
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each SAMD_ISR_Timer
for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++)
{
//ISR_Timer.setInterval(TimerInterval[i], irqCallbackFunc[i]);
// Use this for one shot ISR TImer
ISR_Timer.setTimeout(TimerInterval[i], irqCallbackFunc[i]);
}
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s);
}
#define BLOCKING_TIME_MS 10000L
void loop()
{
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
// You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
// While that of ISR_Timer is still prefect.
delay(BLOCKING_TIME_MS);
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
// You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer.
simpleTimer.run();
}

View File

@ -0,0 +1,159 @@
/****************************************************************************************************************************
ISR_RPM_Measure.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/* Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
Asssuming LOW is active.
For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
If the time between active state is less than 8ms => consider noise.
RPM = 60000 / (rotation time in ms)
We use interrupt to detect whenever the SW is active, set a flag
then use timer to count the time between active state
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 2
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#define PIN_D1 5 // Pin D1 mapped to pin GPIO5 of ESP8266
unsigned int interruptPin = PIN_D1;
#define TIMER_INTERVAL_MS 1
#define DEBOUNCING_INTERVAL_MS 80
#define LOCAL_DEBUG 1
// Init ESP8266 timer 1
ESP8266Timer ITimer;
volatile unsigned long rotationTime = 0;
float RPM = 0.00;
float avgRPM = 0.00;
volatile int debounceCounter;
volatile bool activeState = false;
void IRAM_ATTR detectRotation()
{
activeState = true;
}
void IRAM_ATTR TimerHandler()
{
if ( activeState )
{
// Reset to prepare for next round of interrupt
activeState = false;
if (debounceCounter >= DEBOUNCING_INTERVAL_MS / TIMER_INTERVAL_MS )
{
//min time between pulses has passed
RPM = (float) ( 60000.0f / ( rotationTime * TIMER_INTERVAL_MS ) );
avgRPM = ( 2 * avgRPM + RPM) / 3,
#if (TIMER_INTERRUPT_DEBUG > 1)
Serial.print("RPM = "); Serial.print(avgRPM);
Serial.print(", rotationTime ms = "); Serial.println(rotationTime * TIMER_INTERVAL_MS);
#endif
rotationTime = 0;
debounceCounter = 0;
}
else
debounceCounter++;
}
else
{
debounceCounter++;
}
if (rotationTime >= 5000)
{
// If idle, set RPM to 0, don't increase rotationTime
RPM = 0;
#if (TIMER_INTERRUPT_DEBUG > 1)
Serial.print("RPM = "); Serial.print(RPM); Serial.print(", rotationTime = "); Serial.println(rotationTime);
#endif
rotationTime = 0;
}
else
{
rotationTime++;
}
}
void setup()
{
pinMode(interruptPin, INPUT_PULLUP);
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting ISR_RPM_Measure on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
// Assumming the interruptPin will go LOW
attachInterrupt(digitalPinToInterrupt(interruptPin), detectRotation, FALLING);
}
void loop()
{
}

View File

@ -0,0 +1,146 @@
/****************************************************************************************************************************
RPM_Measure.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/* Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
Asssuming LOW is active.
For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
If the time between active state is less than 8ms => consider noise.
RPM = 60000 / (rotation time in ms)
You can also use interrupt to detect whenever the SW is active, set a flag
then use timer to count the time between active state
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 2
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#define PIN_D1 5 // Pin D1 mapped to pin GPIO5 of ESP8266
unsigned int SWPin = PIN_D1;
#define TIMER_INTERVAL_MS 1
#define DEBOUNCING_INTERVAL_MS 80
#define LOCAL_DEBUG 1
// Init ESP8266 timer 1
ESP8266Timer ITimer;
volatile unsigned long rotationTime = 0;
float RPM = 0.00;
float avgRPM = 0.00;
volatile int debounceCounter;
void IRAM_ATTR TimerHandler()
{
static bool started = false;
if (!started)
{
started = true;
pinMode(SWPin, INPUT_PULLUP);
}
if ( !digitalRead(SWPin) && (debounceCounter >= DEBOUNCING_INTERVAL_MS / TIMER_INTERVAL_MS ) )
{
//min time between pulses has passed
RPM = (float) ( 60000.0f / ( rotationTime * TIMER_INTERVAL_MS ) );
avgRPM = ( 2 * avgRPM + RPM) / 3,
#if (LOCAL_DEBUG > 0)
Serial.print("RPM = "); Serial.print(avgRPM);
Serial.print(", rotationTime ms = "); Serial.println(rotationTime * TIMER_INTERVAL_MS);
#endif
rotationTime = 0;
debounceCounter = 0;
}
else
{
debounceCounter++;
}
if (rotationTime >= 5000)
{
// If idle, set RPM to 0, don't increase rotationTime
RPM = 0;
#if (LOCAL_DEBUG > 0)
Serial.print("RPM = "); Serial.print(RPM); Serial.print(", rotationTime = "); Serial.println(rotationTime);
#endif
rotationTime = 0;
}
else
{
rotationTime++;
}
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStarting RPM_Measure on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
}
void loop()
{
}

View File

@ -0,0 +1,179 @@
/****************************************************************************************************************************
SwitchDebounce.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/* Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
Switch Debouncing uses high frequency hardware timer 50Hz == 20ms) to measure the time from the SW is pressed,
debouncing time is 100ms => SW is considered pressed if timer count is > 5, then call / flag SW is pressed
When the SW is released, timer will count (debounce) until more than 50ms until consider SW is released.
We can set to flag or call a function whenever SW is pressed more than certain predetermined time, even before
SW is released.
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 0
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#define PIN_D1 5 // Pin D1 mapped to pin GPIO5 of ESP8266
unsigned int SWPin = PIN_D1;
#define TIMER_INTERVAL_MS 20
#define DEBOUNCING_INTERVAL_MS 100
#define LONG_PRESS_INTERVAL_MS 5000
//#define LOCAL_DEBUG 1
// Init ESP8266 timer 1
ESP8266Timer ITimer;
volatile bool SWPressed = false;
volatile bool SWLongPressed = false;
void IRAM_ATTR TimerHandler()
{
static unsigned int debounceCountSWPressed = 0;
static unsigned int debounceCountSWReleased = 0;
#if (TIMER_INTERRUPT_DEBUG > 0)
static unsigned long SWPressedTime;
static unsigned long SWReleasedTime;
static unsigned long currentMillis;
#endif
currentMillis = millis();
if ( (!digitalRead(SWPin)) )
{
// Start debouncing counting debounceCountSWPressed and clear debounceCountSWReleased
debounceCountSWReleased = 0;
if (++debounceCountSWPressed >= DEBOUNCING_INTERVAL_MS / TIMER_INTERVAL_MS)
{
// Call and flag SWPressed
if (!SWPressed)
{
#if (TIMER_INTERRUPT_DEBUG > 0)
SWPressedTime = currentMillis;
Serial.print("SW Press, from millis() = "); Serial.println(SWPressedTime - DEBOUNCING_INTERVAL_MS);
#endif
SWPressed = true;
// Do something for SWPressed here in ISR
// But it's better to use outside software timer to do your job instead of inside ISR
//Your_Response_To_Press();
}
if (debounceCountSWPressed >= LONG_PRESS_INTERVAL_MS / TIMER_INTERVAL_MS)
{
// Call and flag SWLongPressed
if (!SWLongPressed)
{
#if (TIMER_INTERRUPT_DEBUG > 0)
Serial.print("SW Long Pressed, total time ms = "); Serial.print(currentMillis);
Serial.print(" - "); Serial.print(SWPressedTime - DEBOUNCING_INTERVAL_MS);
Serial.print(" = "); Serial.println(currentMillis - SWPressedTime + DEBOUNCING_INTERVAL_MS);
#endif
SWLongPressed = true;
// Do something for SWLongPressed here in ISR
// But it's better to use outside software timer to do your job instead of inside ISR
//Your_Response_To_Long_Press();
}
}
}
}
else
{
// Start debouncing counting debounceCountSWReleased and clear debounceCountSWPressed
if ( SWPressed && (++debounceCountSWReleased >= DEBOUNCING_INTERVAL_MS / TIMER_INTERVAL_MS))
{
// Call and flag SWPressed
#if (TIMER_INTERRUPT_DEBUGEBUG > 0)
SWReleasedTime = millis();
// Call and flag SWPressed
Serial.print("SW Released, from millis() = "); Serial.println(millis());
#endif
SWPressed = false;
SWLongPressed = false;
// Do something for !SWPressed here in ISR
// But it's better to use outside software timer to do your job instead of inside ISR
//Your_Response_To_Release();
// Call and flag SWPressed
#if (TIMER_INTERRUPT_DEBUG > 0)
Serial.print("SW Pressed total time ms = "); Serial.println(millis() - SWPressedTime);
#endif
debounceCountSWPressed = 0;
}
}
}
void setup()
{
pinMode(SWPin, INPUT_PULLUP);
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStarting SwitchDebounce on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
}
void loop()
{
}

View File

@ -0,0 +1,106 @@
/****************************************************************************************************************************
TimerInterruptTest.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
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 !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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.
*****************************************************************************************************************************/
/* Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
*/
#if !defined(ESP8266)
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 1
// Select a Timer Clock
#define USING_TIM_DIV1 false // for shortest and most accurate timer
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
#define USING_TIM_DIV256 true // for longest timer but least accurate. Default
#include "ESP8266TimerInterrupt.h"
#define BUILTIN_LED 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
volatile bool statusLed = false;
volatile uint32_t lastMillis = 0;
#define TIMER_INTERVAL_MS 1000
// Init ESP8266 timer 1
ESP8266Timer ITimer;
//=======================================================================
void IRAM_ATTR TimerHandler()
{
static bool started = false;
if (!started)
{
started = true;
pinMode(BUILTIN_LED, OUTPUT);
}
digitalWrite(BUILTIN_LED, statusLed); //Toggle LED Pin
statusLed = !statusLed;
#if (TIMER_INTERRUPT_DEBUG > 0)
Serial.println("Delta ms = " + String(millis() - lastMillis));
lastMillis = millis();
#endif
}
//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(300);
Serial.print(F("\nStarting TimerInterruptTest on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
lastMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(lastMillis);
}
else
Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));
}
//=======================================================================
// MAIN LOOP
//=======================================================================
void loop()
{
}

View File

@ -0,0 +1,12 @@
/****************************************************************************************************************************
multiFileProject.cpp
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
*****************************************************************************************************************************/
// To demo how to include files in multi-file Projects
#include "multiFileProject.h"

View File

@ -0,0 +1,16 @@
/****************************************************************************************************************************
multiFileProject.h
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
*****************************************************************************************************************************/
// To demo how to include files in multi-file Projects
#pragma once
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "ESP8266TimerInterrupt.h"
#include "ESP8266_ISR_Timer.hpp"

View File

@ -0,0 +1,46 @@
/****************************************************************************************************************************
multiFileProject.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
Licensed under MIT license
*****************************************************************************************************************************/
// To demo how to include files in multi-file Projects
#ifndef ESP8266
#error This code is designed to run on ESP8266 platform! Please check your Tools->Board setting.
#endif
#define ESP8266_TIMER_INTERRUPT_VERSION_MIN_TARGET "ESP8266TimerInterrupt v1.6.0"
#define ESP8266_TIMER_INTERRUPT_VERSION_MIN 1006000
#include "multiFileProject.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "ESP8266TimerInterrupt.h"
#include "ESP8266_ISR_Timer.h"
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("\nStart multiFileProject");
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
#if defined(ESP8266_TIMER_INTERRUPT_VERSION_MIN)
if (ESP8266_TIMER_INTERRUPT_VERSION_INT < ESP8266_TIMER_INTERRUPT_VERSION_MIN)
{
Serial.print("Warning. Must use this example on Version equal or later than : ");
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION_MIN_TARGET);
}
#endif
}
void loop()
{
// put your main code here, to run repeatedly:
}