- Why do we need this STM32_TimerInterrupt library
- Changelog
- Prerequisites
- Installation
- Libraries' Patches
- HOWTO Fix
Multiple DefinitionsLinker Error - More useful Information about STM32 Timers
- New from v1.0.0
- Usage
- Examples
- Example ISR_16_Timers_Array_Complex
- Debug Terminal Output Samples
- 1. ISR_Timer_Complex on STM32F7 Nucleo-144 NUCLEO_F767ZI using Built-in LAN8742A
- 2. TimerInterruptTest on STM32F7 Nucleo-144 NUCLEO_F767ZI
- 3. Argument_None on STM32F7 Nucleo-144 NUCLEO_F767ZI
- 4. Change_Interval on STM32F7 Nucleo-144 NUCLEO_F767ZI
- 5. ISR_16_Timers_Array_Complex on STM32F7 Nucleo-144 NUCLEO_F767ZI
- 6. ISR_16_Timers_Array_Complex on STM32F1 BLUEPILL_F103C8
- 7. ISR_16_Timers_Array_Complex on STM32H7 NUCLEO_H743ZI2
- 8. ISR_16_Timers_Array_Complex on STM32L5 NUCLEO_L552ZE_Q
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this STM32_TimerInterrupt library
This library enables you to use Interrupt from Hardware Timers on an STM32-based board, such as STM32F/L/H/G/WB/MP1.
As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long milliseconds) while 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.
The ISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.
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 Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task in loop(), using delay() function as an example. The elapsed time then is very unaccurate
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().
So your function might not be executed, and the result would be disastrous.
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
The correct choice is to use a Hardware Timer with Interrupt to call your function.
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.
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.
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:
- STM32 boards with built-in Ethernet LAN8742A such as :
- Nucleo-144 (F429ZI, F767ZI, NUCLEO_H743ZI2, NUCLEO_L552ZE_Q)
- Discovery (STM32F746G-DISCOVERY)
- All STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet
- STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 or ENC28J60 shields)
- Nucleo-144
- Nucleo-64
- Discovery
- Generic STM32F0, STM32F1, STM32F2, STM32F3, STM32F4, STM32F7 (with 64+K Flash): x8 and up
- STM32L0, STM32L1, STM32L4, STM32L5
- STM32G0, STM32G4
- STM32H7
- STM32WB
- STM32MP1
- LoRa boards
- 3-D printer boards
- Generic Flight Controllers
- Midatronics boards
-
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.
-
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.
Arduino IDE 1.8.19+for Arduino.Arduino Core for STM32 v2.3.0+for STM32F/L/H/G/WB/MP1 boards.Blynk library 1.1.0+.to use with certain example.
- For built-in LAN8742A Ethernet:
STM32Ethernet library v1.3.0+for built-in LAN8742A Ethernet on (Nucleo-144, Discovery).LwIP library v2.1.2+for built-in LAN8742A Ethernet on (Nucleo-144, Discovery).
- For W5x00 Ethernet:
Ethernet_Generic library v2.7.1+for W5100, W5200 and W5500.
- For ENC28J60 Ethernet:
EthernetENC library v2.0.3+for ENC28J60.. New and Better
UIPEthernet library v2.0.12+for ENC28J60.
- To use with certain example
The best and easiest way is to use Arduino Library Manager. Search for STM32_TimerInterrupt, then select / install the latest version. You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate to STM32_TimerInterrupt page.
- Download the latest release
STM32_TimerInterrupt-master.zip. - Extract the zip file to
STM32_TimerInterrupt-masterdirectory - Copy whole
STM32_TimerInterrupt-masterfolder to Arduino libraries' directory such as~/Arduino/libraries/.
- Install VS Code
- Install PlatformIO
- Install STM32_TimerInterrupt library by using Library Manager. Search for STM32_TimerInterrupt in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
Notes: These patches are totally optional and necessary only when you use the related Ethernet library and get certain error or issues.
If your application requires 2K+ HTML page, the current Ethernet library must be modified if you are using W5200/W5500 Ethernet shields. W5100 is not supported for 2K+ buffer. If you use boards requiring different CS/SS pin for W5x00 Ethernet shield, for example ESP32, ESP8266, nRF52, etc., you also have to modify the following libraries to be able to specify the CS/SS pin correctly.
To fix Ethernet library, just copy these following files into the Ethernet library directory to overwrite the old files:
To fix EthernetLarge library, just copy these following files into the EthernetLarge library directory to overwrite the old files:
To fix Ethernet2 library, just copy these following files into the Ethernet2 library directory to overwrite the old files:
To add UDP Multicast support, necessary for the UPnP_Generic library:
- To fix
Ethernet3 library, just copy these following files into theEthernet3 librarydirectory to overwrite the old files:
To be able to compile and run on nRF52 boards with ENC28J60 using UIPEthernet library, you have to copy these following files into the UIPEthernet utility directory to overwrite the old files:
To fix ESP32 compile error, just copy the following file into the ESP32 cores/esp32 directory (e.g. ./arduino-1.8.12/hardware/espressif/cores/esp32) to overwrite the old file:
Check if you need to install the UIPEthernet patch new STM32 core F3/F4 compatibility to avoid errors #include HardwareSPI.h on some STM32 boards (Nucleo-32 F303K8, etc.)
The current library implementation, using xyz-Impl.h instead of standard xyz.cpp, possibly creates certain Multiple Definitions Linker error in certain use cases.
You can use
#include <STM32_ISR_Timer.hpp> //https://github.com/khoih-prog/STM32_TimerInterruptin many files. But be sure to use the following #include <STM32_ISR_Timer.h> in just 1 .h, .cpp or .ino file, which must not be included in any other file, to avoid Multiple Definitions Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include <STM32_ISR_Timer.h> //https://github.com/khoih-prog/STM32_TimerInterruptThe Timers of STM32s are numerous, yet very sophisticated and powerful.
In general, across the STM32 microcontrollers families, the timer peripherals that have the same name also have the same features set, but there are a few exceptions.
For example, the TIM1 timer peripheral is shared across the STM32F1 Series, STM32F2 Series and STM32F4 Series, but for the specific case of STM32F30x microcontrollers family, the TIM1 timer peripheral features a bit richer features set than the TIM1 present in the other families.
The general purpose timers embedded by the STM32 microcontrollers share the same backbone structure; they differ only on the level of features embedded by a given timer peripheral.
The level of features integration for a given timer peripheral is decided based on the applications field that it targets.
The timer peripherals can be classified as: • Advanced-configuration timers like TIM1 and TIM8 among others. • General-purpose configuration timers like TIM2 and TIM3 among others • Lite-configuration timers like TIM9, TIM10, TIM12 and TIM16 among others • Basic-configuration timers like TIM6 and TIM7 among others.
For example, STM32F103C8T6 has one advance timer, while STM32F103VET6 has two advanced timers. Nucleo-144 STM32F767ZI boards have 14 Timers, TIM1-TIM14.
More information can be found at Embedded-Lab STM32 TIMERS
To be sure which Timer is available for the board you're using, check the Core Package's related files. For example, for Nucleo-144 STM32F767ZI, check these files:
~/.arduino15/packages/STM32/hardware/stm32/1.9.0/system/Drivers/CMSIS/Device/ST/STM32F7xx/Include/stm32f7xx.h~/.arduino15/packages/STM32/hardware/stm32/1.9.0/system/Drivers/CMSIS/Device/ST/STM32F7xx/Include/stm32f767xx.h
The information will be as follows:
typedef struct { __IO uint32_t CR1; /*!< TIM control register 1, Address offset: 0x00 */ __IO uint32_t CR2; /*!< TIM control register 2, Address offset: 0x04 */ __IO uint32_t SMCR; /*!< TIM slave mode control register, Address offset: 0x08 */ __IO uint32_t DIER; /*!< TIM DMA/interrupt enable register, Address offset: 0x0C */ __IO uint32_t SR; /*!< TIM status register, Address offset: 0x10 */ __IO uint32_t EGR; /*!< TIM event generation register, Address offset: 0x14 */ __IO uint32_t CCMR1; /*!< TIM capture/compare mode register 1, Address offset: 0x18 */ __IO uint32_t CCMR2; /*!< TIM capture/compare mode register 2, Address offset: 0x1C */ __IO uint32_t CCER; /*!< TIM capture/compare enable register, Address offset: 0x20 */ __IO uint32_t CNT; /*!< TIM counter register, Address offset: 0x24 */ __IO uint32_t PSC; /*!< TIM prescaler, Address offset: 0x28 */ __IO uint32_t ARR; /*!< TIM auto-reload register, Address offset: 0x2C */ __IO uint32_t RCR; /*!< TIM repetition counter register, Address offset: 0x30 */ __IO uint32_t CCR1; /*!< TIM capture/compare register 1, Address offset: 0x34 */ __IO uint32_t CCR2; /*!< TIM capture/compare register 2, Address offset: 0x38 */ __IO uint32_t CCR3; /*!< TIM capture/compare register 3, Address offset: 0x3C */ __IO uint32_t CCR4; /*!< TIM capture/compare register 4, Address offset: 0x40 */ __IO uint32_t BDTR; /*!< TIM break and dead-time register, Address offset: 0x44 */ __IO uint32_t DCR; /*!< TIM DMA control register, Address offset: 0x48 */ __IO uint32_t DMAR; /*!< TIM DMA address for full transfer, Address offset: 0x4C */ __IO uint32_t OR; /*!< TIM option register, Address offset: 0x50 */ __IO uint32_t CCMR3; /*!< TIM capture/compare mode register 3, Address offset: 0x54 */ __IO uint32_t CCR5; /*!< TIM capture/compare mode register5, Address offset: 0x58 */ __IO uint32_t CCR6; /*!< TIM capture/compare mode register6, Address offset: 0x5C */ __IO uint32_t AF1; /*!< TIM Alternate function option register 1, Address offset: 0x60 */ __IO uint32_t AF2; /*!< TIM Alternate function option register 2, Address offset: 0x64 */ } TIM_TypeDef;and
#define PERIPH_BASE 0x40000000UL /*!< Base address of : AHB/ABP Peripherals /*!< Peripheral memory map */ #define APB1PERIPH_BASE PERIPH_BASE /*!< APB1 peripherals */ #define TIM2_BASE (APB1PERIPH_BASE + 0x0000UL) #define TIM3_BASE (APB1PERIPH_BASE + 0x0400UL) #define TIM4_BASE (APB1PERIPH_BASE + 0x0800UL) #define TIM5_BASE (APB1PERIPH_BASE + 0x0C00UL) #define TIM6_BASE (APB1PERIPH_BASE + 0x1000UL) #define TIM7_BASE (APB1PERIPH_BASE + 0x1400UL) #define TIM12_BASE (APB1PERIPH_BASE + 0x1800UL) #define TIM13_BASE (APB1PERIPH_BASE + 0x1C00UL) #define TIM14_BASE (APB1PERIPH_BASE + 0x2000UL) /*!< APB2 peripherals */ #define TIM1_BASE (APB2PERIPH_BASE + 0x0000UL) #define TIM8_BASE (APB2PERIPH_BASE + 0x0400UL) #define TIM9_BASE (APB2PERIPH_BASE + 0x4000UL) #define TIM10_BASE (APB2PERIPH_BASE + 0x4400UL) #define TIM11_BASE (APB2PERIPH_BASE + 0x4800UL) ... #define TIM2 ((TIM_TypeDef *) TIM2_BASE) #define TIM3 ((TIM_TypeDef *) TIM3_BASE) #define TIM4 ((TIM_TypeDef *) TIM4_BASE) #define TIM5 ((TIM_TypeDef *) TIM5_BASE) #define TIM6 ((TIM_TypeDef *) TIM6_BASE) #define TIM7 ((TIM_TypeDef *) TIM7_BASE) #define TIM12 ((TIM_TypeDef *) TIM12_BASE) #define TIM13 ((TIM_TypeDef *) TIM13_BASE) #define TIM14 ((TIM_TypeDef *) TIM14_BASE) ... #define TIM1 ((TIM_TypeDef *) TIM1_BASE) #define TIM8 ((TIM_TypeDef *) TIM8_BASE) ... #define TIM9 ((TIM_TypeDef *) TIM9_BASE) #define TIM10 ((TIM_TypeDef *) TIM10_BASE) #define TIM11 ((TIM_TypeDef *) TIM11_BASE) Now with these new 16 ISR-based timers (while consuming only 1 hardware timer), the maximum interval is practically unlimited (limited only by unsigned long milliseconds). 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.
The ISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers. 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 Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task in loop(), using delay() function as an example. The elapsed time then is very unaccurate
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
// Init STM32 timer TIM1 STM32Timer ITimer0(TIM1);void TimerHandler0(void) { // Doing something here inside ISR } #define TIMER0_INTERVAL_MS 1000 // 1s = 1000ms void setup() { .... // Interval in microsecs if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0)) Serial.println("Starting ITimer0 OK, millis() = " + String(millis())); else Serial.println("Can't set ITimer0. Select another freq. or timer"); } // Init STM32 timer TIM1 STM32Timer ITimer(TIM1); // Init STM32_ISR_Timer // Each STM32_ISR_Timer can service 16 different ISR-based timers STM32_ISR_Timer ISR_Timer;void TimerHandler(void) { ISR_Timer.run(); } #define HW_TIMER_INTERVAL_US 100L #define TIMER_INTERVAL_2S 2000L #define TIMER_INTERVAL_5S 5000L #define TIMER_INTERVAL_11S 11000L #define TIMER_INTERVAL_101S 101000L // In STM32, 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 doingSomething2s() { // Doing something here inside ISR } void doingSomething5s() { // Doing something here inside ISR } void doingSomething11s() { // Doing something here inside ISR } void doingSomething101s() { // Doing something here inside ISR } void setup() { .... // Interval in microsecs if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) { lastMillis = millis(); Serial.println("Starting ITimer OK, millis() = " + String(lastMillis)); } else Serial.println("Can't set ITimer correctly. Select another freq. or interval"); // 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 ISR_Timer.setInterval(TIMER_INTERVAL_2S, doingSomething2s); ISR_Timer.setInterval(TIMER_INTERVAL_5S, doingSomething5s); ISR_Timer.setInterval(TIMER_INTERVAL_11S, doingSomething11s); ISR_Timer.setInterval(TIMER_INTERVAL_101S, doingSomething101s); } - Argument_None
- ISR_16_Timers_Array
- ISR_RPM_Measure
- ISR_Timer_Complex
- RPM_Measure
- SwitchDebounce
- TimerInterruptTest
- TimerInterruptLEDDemo
- Change_Interval. New
- ISR_16_Timers_Array_Complex. New
Example ISR_16_Timers_Array_Complex
#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) || defined(STM32L5) ) #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif // These define's must be placed at the beginning before #include "STM32TimerInterrupt.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_ 3 #include "STM32TimerInterrupt.h" // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "STM32_ISR_Timer.h" #include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer #ifndef LED_BUILTIN #define LED_BUILTIN 13 #endif #ifndef LED_BLUE #define LED_BLUE 2 #endif #ifndef LED_RED #define LED_RED 3 #endif #define HW_TIMER_INTERVAL_US 10000L volatile uint32_t startMillis = 0; // Depending on the board, you can select STM32 Hardware Timer from TIM1-TIM22 // For example, F767ZI can select Timer from TIM1-TIM14 // If you select a Timer not correctly, you'll get a message from ci[ompiler // 'TIMxx' was not declared in this scope; did you mean 'TIMyy'? // Init STM32 timer TIM1 STM32Timer ITimer(TIM1); // Init STM32_ISR_Timer // Each STM32_ISR_Timer can service 16 different ISR-based timers STM32_ISR_Timer ISR_Timer; #define LED_TOGGLE_INTERVAL_MS 2000L void 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 NRF52, 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(100); Serial.print(F("\nStarting ISR_16_Timers_Array_Complex on ")); Serial.println(BOARD_NAME); Serial.println(STM32_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 correctly. Select another freq. or interval")); // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary // You can use up to 16 timer for each STM32_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] = startMillis; 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(); }The following is the sample terminal output when running example ISR_Timer_Complex on STM32F7 Nucleo-144 NUCLEO_F767ZI using Built-in LAN8742A Ethernet and STM32Ethernet Library to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy. The ISR timer is programmed for 2s, is activated exactly after 2.000s !!!
While software timer, programmed for 2s, is activated after 9.782s !!!. Then in loop(), it's also activated every 3s.
Starting ISR_Timer_Complex on NUCLEO_F767ZI STM32_TimerInterrupt v1.3.0 CPU Frequency = 216 MHz [TISR] Timer Input Freq (Hz) = 216000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 10000.00 , _count = 100 Starting ITimer OK, millis() = 6 [9] MAC:FE-E1-88-EC-DD-95 2s: Delta ms = 2000 2s: Delta ms = 2000 [6626] IP:192.168.2.116 [6626] ___ __ __ / _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ v0.6.1 on STM32 NUCLEO_F767ZI [6636] BlynkArduinoClient.connect: Connecting to account.duckdns.org:8080 [6721] Ready (ping: 6ms). IP = 192.168.2.116 2s: Delta ms = 2000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 9782 2s: Delta ms = 2000 5s: Delta ms = 5000 2s: Delta ms = 2000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 5s: Delta ms = 5000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 2s: Delta ms = 2000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 5s: Delta ms = 5000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 11s: Delta ms = 11000 2s: Delta ms = 2000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 5s: Delta ms = 5000 2s: Delta ms = 2000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 2s: Delta ms = 2000 5s: Delta ms = 5000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 11s: Delta ms = 11000 blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000 2s: Delta ms = 2000 5s: Delta ms = 5000 2s: Delta ms = 2000 The following is the sample terminal output when running example TimerInterruptTest on STM32F7 Nucleo-144 NUCLEO_F767ZI to demonstrate how to start/stop Hardware Timers.
Starting TimerInterruptTest on NUCLEO_F767ZI STM32_TimerInterrupt v1.3.0 CPU Frequency = 216 MHz [TISR] Timer Input Freq (Hz) = 216000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 1.00 , _count = 1000000 Starting ITimer0 OK, millis() = 108 [TISR] Timer Input Freq (Hz) = 108000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 0.33 , _count = 3000000 Starting ITimer1 OK, millis() = 119 Stop ITimer0, millis() = 5001 Start ITimer0, millis() = 10002 Stop ITimer1, millis() = 15001 Stop ITimer0, millis() = 15003 Start ITimer0, millis() = 20004 Stop ITimer0, millis() = 25005 Start ITimer1, millis() = 30002 Start ITimer0, millis() = 30006 Stop ITimer0, millis() = 35007 Start ITimer0, millis() = 40008 Stop ITimer1, millis() = 45003 Stop ITimer0, millis() = 45009 Start ITimer0, millis() = 50010 Stop ITimer0, millis() = 55011 Start ITimer1, millis() = 60004 Start ITimer0, millis() = 60012 Stop ITimer0, millis() = 65013 Start ITimer0, millis() = 70014 Stop ITimer1, millis() = 75005 Stop ITimer0, millis() = 75015 Start ITimer0, millis() = 80016 Stop ITimer0, millis() = 85017 Start ITimer1, millis() = 90006 Start ITimer0, millis() = 90018 Stop ITimer0, millis() = 95019 Start ITimer0, millis() = 100020 Stop ITimer1, millis() = 105007 Stop ITimer0, millis() = 105021 Start ITimer0, millis() = 110022 Stop ITimer0, millis() = 115023 Start ITimer1, millis() = 120008 Start ITimer0, millis() = 120024 Stop ITimer0, millis() = 125025 Start ITimer0, millis() = 130026 Stop ITimer1, millis() = 135009 Stop ITimer0, millis() = 135027 Start ITimer0, millis() = 140028 The following is the sample terminal output when running example Argument_None on STM32G7 Nucleo-144 NUCLEO_F767ZI to demonstrate how to start/stop Multiple Hardware Timers.
Starting Argument_None on NUCLEO_F767ZI STM32TimerInterrupt v1.3.0 CPU Frequency = 216 MHz [TISR] Timer Input Freq (Hz) = 216000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 1.00 , _count = 1000000 Starting ITimer0 OK, millis() = 106 [TISR] Timer Input Freq (Hz) = 108000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 0.50 , _count = 2000000 Starting ITimer1 OK, millis() = 117 [TISR] Timer Input Freq (Hz) = 108000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 0.20 , _count = 5000000 Starting ITimer2 OK, millis() = 129 ITimer0: millis() = 1106, delta = 1000 ITimer0: millis() = 2106, delta = 1000 ITimer0: millis() = 3106, delta = 1000 ITimer0: millis() = 4106, delta = 1000 ITimer0: millis() = 5106, delta = 1000 ITimer1: millis() = 5118, delta = 5000 ITimer0: millis() = 6106, delta = 1000 ITimer0: millis() = 7106, delta = 1000 ITimer0: millis() = 8106, delta = 1000 ITimer0: millis() = 9106, delta = 1000 ITimer0: millis() = 10106, delta = 1000 ITimer1: millis() = 10118, delta = 5000 ITimer0: millis() = 11106, delta = 1000 ITimer0: millis() = 12106, delta = 1000 ITimer0: millis() = 13106, delta = 1000 ITimer0: millis() = 14106, delta = 1000 ITimer0: millis() = 15106, delta = 1000 ITimer1: millis() = 15118, delta = 5000 ITimer0: millis() = 16106, delta = 1000 ITimer0: millis() = 17106, delta = 1000 ITimer0: millis() = 18106, delta = 1000 ITimer0: millis() = 19106, delta = 1000 ITimer0: millis() = 20106, delta = 1000 ITimer1: millis() = 20118, delta = 5000 ITimer2: millis() = 20129, delta = 19999 ITimer0: millis() = 21106, delta = 1000 ITimer0: millis() = 22106, delta = 1000 ITimer0: millis() = 23106, delta = 1000 ITimer0: millis() = 24106, delta = 1000 ITimer0: millis() = 25106, delta = 1000 ITimer1: millis() = 25118, delta = 5000 ITimer0: millis() = 26106, delta = 1000 ITimer0: millis() = 27106, delta = 1000 ITimer0: millis() = 28106, delta = 1000 ITimer0: millis() = 29106, delta = 1000 ITimer0: millis() = 30106, delta = 1000 ITimer1: millis() = 30118, delta = 5000 ITimer0: millis() = 31106, delta = 1000 ITimer0: millis() = 32106, delta = 1000 ITimer0: millis() = 33106, delta = 1000 ITimer0: millis() = 34106, delta = 1000 ITimer0: millis() = 35106, delta = 1000 ITimer1: millis() = 35118, delta = 5000 ITimer0: millis() = 36106, delta = 1000 ITimer0: millis() = 37106, delta = 1000 The following is the sample terminal output when running example Change_Interval on STM32F7 Nucleo-144 NUCLEO_F767ZI to demonstrate how to change Timer Interval on-the-fly
Starting Change_Interval on NUCLEO_F767ZI STM32TimerInterrupt v1.3.0 CPU Frequency = 216 MHz [TISR] Timer Input Freq (Hz) = 216000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 2.00 , _count = 500000 Starting Timer0 OK, millis() = 111 [TISR] Timer Input Freq (Hz) = 108000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 1.00 , _count = 1000000 Starting ITimer1 OK, millis() = 122 Time = 10001, Timer0Count = 20, , Timer1Count = 10 Time = 20002, Timer0Count = 40, , Timer1Count = 20 [TISR] STM32TimerInterrupt: Timer Input Freq (Hz) = 216000000 [TISR] Frequency = 1000000.00 , _count = 1000000 [TISR] STM32TimerInterrupt: Timer Input Freq (Hz) = 108000000 [TISR] Frequency = 1000000.00 , _count = 2000000 Changing Interval, Timer0 = 1000, Timer1 = 2000 Time = 30003, Timer0Count = 50, , Timer1Count = 25 Time = 40004, Timer0Count = 60, , Timer1Count = 30 The following is the sample terminal output when running new example ISR_16_Timers_Array_Complex on STM32F7 Nucleo-144 NUCLEO_F767ZI to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
While software timer, programmed for 2s, is activated after 10.003s in loop()!!!.
In this example, 16 independent ISR Timers are used, yet utilized just one Hardware Timer. The Timer Intervals and Function Pointers are stored in arrays to facilitate the code modification.
Starting ISR_16_Timers_Array_Complex on NUCLEO_F767ZI STM32_TimerInterrupt v1.3.0 CPU Frequency = 216 MHz [TISR] Timer Input Freq (Hz) = 216000000 , Timer Clock Frequency = 1000000.00 [TISR] Timer Frequency = 100.00 , _count = 10000 Starting ITimer OK, millis() = 12 SimpleTimer : 2s, ms = 10015, Dms : 10003 Timer : 0, programmed : 5000, actual : 5010 Timer : 1, programmed : 10000, actual : 0 Timer : 2, programmed : 15000, actual : 0 Timer : 3, programmed : 20000, actual : 0 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 20073, Dms : 10058 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15010 Timer : 3, programmed : 20000, actual : 20010 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 30132, Dms : 10059 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20010 Timer : 4, programmed : 25000, actual : 25010 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 40192, Dms : 10060 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25010 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 50252, Dms : 10060 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 60313, Dms : 10061 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 70375, Dms : 10062 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 65010 Timer : 13, programmed : 70000, actual : 70010 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2s, ms = 80437, Dms : 10062 Timer : 0, programmed : 5000, actual : 4999 Timer : 1, programmed : 10000, actual : 9999 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 19999 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 39999 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 65010 Timer : 13, programmed : 70000, actual : 70010 Timer : 14, programmed : 75000, actual : 75010 Timer : 15, programmed : 80000, actual : 80009 SimpleTimer : 2s, ms = 90500, Dms : 10063 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 14999 Timer : 3, programmed : 20000, actual : 19999 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 29999 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 39999 Timer : 8, programmed : 45000, actual : 44999 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 65010 Timer : 13, programmed : 70000, actual : 70010 Timer : 14, programmed : 75000, actual : 75010 Timer : 15, programmed : 80000, actual : 80009 The following is the sample terminal output when running new example ISR_16_Timers_Array_Complex on STM32F1 BLUEPILL_F103C8 to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
While software timer, programmed for 2s, is activated after 10.000s in loop()!!!.
In this example, 16 independent ISR Timers are used, yet utilized just one Hardware Timer. The Timer Intervals and Function Pointers are stored in arrays to facilitate the code modification.
Starting ISR_16_Timers_Array_Complex on BLUEPILL_F103C8 STM32_TimerInterrupt v1.3.0 CPU Frequency = 72 MHz Starting ITimer OK, millis() = 8880 SimpleTimer : 2, ms : 18880, Dms : 10000 Timer : 0, programmed : 5000, actual : 5001 Timer : 1, programmed : 10000, actual : 0 Timer : 2, programmed : 15000, actual : 0 Timer : 3, programmed : 20000, actual : 0 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 28881, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 38882, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 48883, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40000 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 58884, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40000 Timer : 8, programmed : 45000, actual : 45000 Timer : 9, programmed : 50000, actual : 50000 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 68885, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40000 Timer : 8, programmed : 45000, actual : 45000 Timer : 9, programmed : 50000, actual : 50000 Timer : 10, programmed : 55000, actual : 55000 Timer : 11, programmed : 60000, actual : 60000 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 78886, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40000 Timer : 8, programmed : 45000, actual : 45000 Timer : 9, programmed : 50000, actual : 50000 Timer : 10, programmed : 55000, actual : 55000 Timer : 11, programmed : 60000, actual : 60000 Timer : 12, programmed : 65000, actual : 65000 Timer : 13, programmed : 70000, actual : 70000 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 88887, Dms : 10001 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40000 Timer : 8, programmed : 45000, actual : 45000 Timer : 9, programmed : 50000, actual : 50000 Timer : 10, programmed : 55000, actual : 55000 Timer : 11, programmed : 60000, actual : 60000 Timer : 12, programmed : 65000, actual : 65000 Timer : 13, programmed : 70000, actual : 70000 Timer : 14, programmed : 75000, actual : 75000 Timer : 15, programmed : 80000, actual : 80000 The following is the sample terminal output when running new example ISR_16_Timers_Array_Complex on STM32H7 NUCLEO_H743ZI2 to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
While software timer, programmed for 2s, is activated after 10.000s in loop()!!!.
In this example, 16 independent ISR Timers are used, yet utilized just one Hardware Timer. The Timer Intervals and Function Pointers are stored in arrays to facilitate the code modification.
Starting ISR_16_Timers_Array_Complex on NUCLEO_H743ZI2 STM32_TimerInterrupt v1.3.0 CPU Frequency = 480 MHz Starting ITimer OK, millis() = 109 SimpleTimer : 2, ms : 10112, Dms : 10003 Timer : 0, programmed : 5000, actual : 5010 Timer : 1, programmed : 10000, actual : 10010 Timer : 2, programmed : 15000, actual : 0 Timer : 3, programmed : 20000, actual : 0 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 20176, Dms : 10064 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15010 Timer : 3, programmed : 20000, actual : 20010 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 30241, Dms : 10065 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20010 Timer : 4, programmed : 25000, actual : 25010 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 40306, Dms : 10065 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25010 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 50372, Dms : 10066 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 60439, Dms : 10067 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 9999 Timer : 2, programmed : 15000, actual : 14999 Timer : 3, programmed : 20000, actual : 19999 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 29999 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55009 Timer : 11, programmed : 60000, actual : 60009 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 70506, Dms : 10067 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 14999 Timer : 3, programmed : 20000, actual : 19999 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 29999 Timer : 6, programmed : 35000, actual : 34999 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55009 Timer : 11, programmed : 60000, actual : 60009 Timer : 12, programmed : 65000, actual : 65009 Timer : 13, programmed : 70000, actual : 70009 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 80574, Dms : 10068 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 24999 Timer : 5, programmed : 30000, actual : 29999 Timer : 6, programmed : 35000, actual : 34999 Timer : 7, programmed : 40000, actual : 39999 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55009 Timer : 11, programmed : 60000, actual : 60009 Timer : 12, programmed : 65000, actual : 65009 Timer : 13, programmed : 70000, actual : 70009 Timer : 14, programmed : 75000, actual : 75009 Timer : 15, programmed : 80000, actual : 80009 The following is the sample terminal output when running new example ISR_16_Timers_Array_Complex on STM32L5 NUCLEO_L552ZE_Q to demonstrate the accuracy of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
While software timer, programmed for 2s, is activated after 10.000s in loop()!!!.
In this example, 16 independent ISR Timers are used, yet utilized just one Hardware Timer. The Timer Intervals and Function Pointers are stored in arrays to facilitate the code modification.
Starting ISR_16_Timers_Array_Complex on NUCLEO_L552ZE_Q STM32_TimerInterrupt v1.3.0 CPU Frequency = 110 MHz Starting ITimer OK, millis() = 109 SimpleTimer : 2, ms : 10112, Dms : 10003 Timer : 0, programmed : 5000, actual : 5011 Timer : 1, programmed : 10000, actual : 10011 Timer : 2, programmed : 15000, actual : 0 Timer : 3, programmed : 20000, actual : 0 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 20177, Dms : 10065 Timer : 0, programmed : 5000, actual : 4999 Timer : 1, programmed : 10000, actual : 9999 Timer : 2, programmed : 15000, actual : 15011 Timer : 3, programmed : 20000, actual : 20010 Timer : 4, programmed : 25000, actual : 0 Timer : 5, programmed : 30000, actual : 0 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 30242, Dms : 10065 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 14999 Timer : 3, programmed : 20000, actual : 20010 Timer : 4, programmed : 25000, actual : 25010 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 0 Timer : 7, programmed : 40000, actual : 0 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 40308, Dms : 10066 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 14999 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25010 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 0 Timer : 9, programmed : 50000, actual : 0 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 50375, Dms : 10067 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30010 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 0 Timer : 11, programmed : 60000, actual : 0 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 60443, Dms : 10068 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35010 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 0 Timer : 13, programmed : 70000, actual : 0 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 70511, Dms : 10068 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40010 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 65010 Timer : 13, programmed : 70000, actual : 70010 Timer : 14, programmed : 75000, actual : 0 Timer : 15, programmed : 80000, actual : 0 SimpleTimer : 2, ms : 80580, Dms : 10069 Timer : 0, programmed : 5000, actual : 5000 Timer : 1, programmed : 10000, actual : 10000 Timer : 2, programmed : 15000, actual : 15000 Timer : 3, programmed : 20000, actual : 20000 Timer : 4, programmed : 25000, actual : 25000 Timer : 5, programmed : 30000, actual : 30000 Timer : 6, programmed : 35000, actual : 35000 Timer : 7, programmed : 40000, actual : 40000 Timer : 8, programmed : 45000, actual : 45010 Timer : 9, programmed : 50000, actual : 50010 Timer : 10, programmed : 55000, actual : 55010 Timer : 11, programmed : 60000, actual : 60010 Timer : 12, programmed : 65000, actual : 65010 Timer : 13, programmed : 70000, actual : 70010 Timer : 14, programmed : 75000, actual : 75010 Timer : 15, programmed : 80000, actual : 80010 Debug is enabled by default on Serial.
You can also change the debugging level (TIMERINTERRUPT_LOGLEVEL) from 0 to 4
// These define's must be placed at the beginning before #include "STM32_TimerInterrupt.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_ 0If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to: STM32_TimerInterrupt issues
- Search for bug and improvement.
- Similar features for remaining Arduino boards such as SAM-DUE
- Basic hardware timers for STM32.
- More hardware-initiated software-enabled timers
- Longer time interval
- Similar features for remaining Arduino boards such as ESP32, ESP8266, SAMD21, SAMD51, nRF52, mbed-nRF52, Teensy, etc.
- Add Table of Contents
- Add support to STM32L5 (NUCLEO_L552ZE_Q)
- Fix
multiple-definitionslinker error. Dropsrc_cppandsrc_hdirectories
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under MIT
Copyright 2020- Khoi Hoang

