- Notifications
You must be signed in to change notification settings - Fork1
This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO. These MBED_RPI_PICO_TimerInterrupt 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 oth…
License
khoih-prog/MBED_RPI_PICO_TimerInterrupt
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- Important Change from v1.1.0
- Why do we need this MBED_RPI_PICO_TimerInterrupt library
- Changelog
- Prerequisites
- Installation
- HOWTO Fix
Multiple DefinitionsLinker Error - More useful Information
- Usage
- Examples
- Example ISR_Timers_Array_Simple
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Please have a look atHOWTO FixMultiple Definitions Linker Error
Why do we need thisMBED_RPI_PICO_TimerInterrupt library
This library enables you to use Interrupt from Hardware Timers onMBED RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, usingArduino-mbed RP2040 core
AsHardware Timers are rare, and very precious assets of any board, this library now enables you to use up to16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).
Now with these new16 ISR-based timers, the maximum interval ispractically unlimited (limited only by unsigned long milliseconds) whilethe accuracy is nearly perfect compared to software timers.
The most important feature is they're ISR-based timers. Therefore, their executions arenot blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
TheISR_16_Timers_Array_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 taskinloop(), using delay() function as an example. The elapsed time then is very unaccurate
Imagine you have a system with amission-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 inloop(). But what if another function isblocking theloop() orsetup().
So your functionmight 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 withInterrupt to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much moreprecise (certainly depending on clock frequency accuracy) than other software timers usingmillis() ormicros(). That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software timers, relying onloop() and callingmillis(), won't work if theloop() orsetup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
The catch isyour function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
- RP2040-based boards such asNano_RP2040_Connect, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. usingArduino-mbed RP2040 core
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 asvolatile 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.
For this MBED-based core,never use Serial.print(ln) inside ISR or the system will hang.
Arduino IDE 1.8.19+for Arduino.Arduino mbed_rp2040 core 3.4.1+for Arduino (Use Arduino Board Manager) RP2040-based boards, such asArduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc..- To use with certain example, depending on which Ethernet card you're using:
Ethernet_Generic library v2.7.1+for W5100, W5200 and W5500.EthernetENC library v2.0.3+for ENC28J60..New and Better
UIPEthernet library v2.0.12+for ENC28J60.
- To use with certain example
SimpleTimer libraryforISR_16_Timers_Array andISR_16_Timers_Array_Complex examples.
The best and easiest way is to useArduino Library Manager. Search forMBED_RPI_PICO_TimerInterrupt, then select / install the latest version.You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate toMBED_RPI_PICO_TimerInterrupt page.
- Download the latest release
MBED_RPI_PICO_TimerInterrupt-main.zip. - Extract the zip file to
MBED_RPI_PICO_TimerInterrupt-maindirectory - Copy whole
MBED_RPI_PICO_TimerInterrupt-mainfolder to Arduino libraries' directory such as~/Arduino/libraries/.
- InstallVS Code
- InstallPlatformIO
- InstallMBED_RPI_PICO_TimerInterrupt library by usingLibrary Manager. Search forMBED_RPI_PICO_TimerInterrupt inPlatform.io Author's Libraries
- Use includedplatformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples atProject Configuration File
The current library implementation, usingxyz-Impl.h instead of standardxyz.cpp, possibly creates certainMultiple Definitions Linker error in certain use cases.
You can include these.hpp files
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error#include"MBED_RPi_Pico_TimerInterrupt.hpp"//https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt// Can be included as many times as necessary, without `Multiple Definitions` Linker Error#include"MBED_RPi_Pico_ISR_Timer.hpp"//https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt
in many files. But be sure to use the following.h filesin just 1.h,.cpp or.ino file, which mustnot be included in any other file, to avoidMultiple Definitions Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error#include"MBED_RPi_Pico_TimerInterrupt.h"//https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error#include"MBED_RPi_Pico_ISR_Timer.h"//https://github.com/khoih-prog/MBED_RPI_PICO_TimerInterrupt
Check the newmultiFileProject example for aHOWTO demo.
The RPI_PICO system timer peripheral provides a global microsecond timebase for the system, and generates interrupts based on this timebase. It supports the following features:• A single 64-bit counter, incrementing once per microsecond• This counter can be read from a pair of latching registers, for race-free reads over a 32-bit bus.• Four alarms: match on the lower 32 bits of counter, IRQ on match:TIMER_IRQ_0-TIMER_IRQ_3
Now with these new16 ISR-based timers (while consuming only1 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.
TheISR_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.TIMER_IRQ_0, TIMER_IRQ_1, TIMER_IRQ_2 and TIMER_IRQ_3 are supported for RP2040-based boards.
// Select the timer you're using, from ITimer0(0)-ITimer3(3)// Init MBED_RPI_PICO_TimerMBED_RPI_PICO_TimerITimer1(1);
Use one of these functions withinterval in unsigned long microseconds
// interval (in us), callback is ISRboolsetInterval(unsignedlong interval, pico_timer_callback callback);// interval (in us), callback is ISRboolattachInterruptInterval(unsignedlong interval, pico_timer_callback callback)
as follows
// Never use Serial.print inside this mbed ISR. Will hang the systemvoidTimerHandler(uint alarm_num){// Doing something here inside ISR}#defineTIMER_INTERVAL_MS5000L// Init MBED_RPI_PICO_TimerMBED_RPI_PICO_TimerITimer(0);voidsetup(){ ....// Interval in unsigned long microsecondsif (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS *1000, TimerHandler)) Serial.println("Starting ITimer OK, millis() =" +String(millis()));else Serial.println("Can't set ITimer. Select another freq. or timer");}
Use one of these functions withfrequency in float Hz
// frequency (in Hz), callback is ISRboolsetFrequency(float frequency, pico_timer_callback callback)// frequency (in Hz), callback is ISRbool attachInterrupt(float frequency, timer_callback callback);
as follows
// Never use Serial.print inside this mbed ISR. Will hang the systemvoidTimerHandler(uint alarm_num){///////////////////////////////////////////////////////////// Always call this for MBED RP2040 before processing ISRTIMER_ISR_START(alarm_num);///////////////////////////////////////////////////////////// Doing something here inside ISR////////////////////////////////////////////////////////////// Always call this for MBED RP2040 after processing ISRTIMER_ISR_END(alarm_num);////////////////////////////////////////////////////////////}#defineTIMER_FREQ_HZ5555.555// Init MBED_RPI_PICO_TimerMBED_RPI_PICO_TimerITimer(0);voidsetup(){ ....// Frequency in float Hzif (ITimer.attachInterrupt(TIMER_FREQ_HZ, TimerHandler)) Serial.println("Starting ITimer OK, millis() =" +String(millis()));else Serial.println("Can't set ITimer. Select another freq. or timer");}
The 16 ISR_based Timers, designed for long timer intervals, only support usingunsigned long millisec intervals. If you have to use much higher frequency or sub-millisecond interval, you have to use the Hardware Timers directly as in1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function
// Init MBED_RPI_PICO_TimerMBED_RPI_PICO_TimerITimer1(1);// Init ISR_Timer// Each ISR_Timer can service 16 different ISR-based timersMBED_RPI_PICO_ISR_Timer ISR_timer;
// Never use Serial.print inside this mbed ISR. Will hang the systemvoidTimerHandler(uint alarm_num){///////////////////////////////////////////////////////////// Always call this for MBED RP2040 before processing ISRTIMER_ISR_START(alarm_num);/////////////////////////////////////////////////////////// ISR_timer.run();////////////////////////////////////////////////////////////// Always call this for MBED RP2040 after processing ISRTIMER_ISR_END(alarm_num);////////////////////////////////////////////////////////////}#defineHW_TIMER_INTERVAL_MS50L#defineTIMER_INTERVAL_2S2000L#defineTIMER_INTERVAL_5S5000L#defineTIMER_INTERVAL_11S11000L#defineTIMER_INTERVAL_101S101000L// In AVR, 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 / crashvoiddoingSomething2s(){// Doing something here inside ISR every 2 seconds}voiddoingSomething5s(){// Doing something here inside ISR every 5 seconds}voiddoingSomething11s(){// Doing something here inside ISR every 11 seconds}voiddoingSomething101s(){// Doing something here inside ISR every 101 seconds}voidsetup(){ ....if (ITimer1.attachInterruptInterval(HW_TIMER_INTERVAL_MS *1000, TimerHandler)) { Serial.print(F("Starting ITimer1 OK, millis() =")); Serial.println(millis()); }else Serial.println(F("Can't set ITimer1. 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 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_Complex
- Argument_None
- Argument_Simple
- Change_Interval
- ISR_16_Timers_Array_Complex
- ISR_Timers_Array_Simple
- SwitchDebounce
- TimerInterruptTest
- 50ms_HWTimerNew
- multiFileProjectNew
ExampleISR_Timers_Array_Simple
MBED_RPI_PICO_TimerInterrupt/examples/ISR_Timers_Array_Simple/ISR_Timers_Array_Simple.ino
Lines 22 to 177 inde94afe
| #if ( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \ | |
| defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED) | |
| #define USING_MBED_RPI_PICO_TIMER_INTERRUPTtrue | |
| #else | |
| #error This code is intended to run on the MBED RASPBERRY_PI_PICO platform! Please check your Tools->Board setting. | |
| #endif | |
| // These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h" | |
| // _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 | |
| #define_TIMERINTERRUPT_LOGLEVEL_4 | |
| // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error | |
| #include"MBED_RPi_Pico_TimerInterrupt.h" | |
| // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error | |
| #include"MBED_RPi_Pico_ISR_Timer.h" | |
| #include<SimpleTimer.h>// https://github.com/schinken/SimpleTimer | |
| // Init MBED_RPI_PICO_Timer | |
| MBED_RPI_PICO_TimerITimer1(1); | |
| MBED_RPI_PICO_ISRTimer ISR_timer; | |
| #ifndef LED_BUILTIN | |
| #defineLED_BUILTIN25 | |
| #endif | |
| #defineLED_TOGGLE_INTERVAL_MS1000L | |
| // You have to use longer time here if having problem because Arduino AVR clock is low, 16MHz => lower accuracy. | |
| // Tested OK with 1ms when not much load => higher accuracy. | |
| #defineTIMER_INTERVAL_MS1L | |
| volatileuint32_t startMillis =0; | |
| volatileuint32_t deltaMillis2s =0; | |
| volatileuint32_t deltaMillis5s =0; | |
| volatileuint32_t previousMillis2s =0; | |
| volatileuint32_t previousMillis5s =0; | |
| // Never use Serial.print inside this mbed ISR. Will hang the system | |
| voidTimerHandler(uint alarm_num) | |
| { | |
| staticbool toggle =false; | |
| staticint timeRun =0; | |
| /////////////////////////////////////////////////////////// | |
| // Always call this for MBED RP2040 before processing ISR | |
| TIMER_ISR_START(alarm_num); | |
| /////////////////////////////////////////////////////////// | |
| ISR_timer.run(); | |
| // Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s | |
| if (++timeRun == ((LED_TOGGLE_INTERVAL_MS) / TIMER_INTERVAL_MS) ) | |
| { | |
| timeRun =0; | |
| //timer interrupt toggles pin LED_BUILTIN | |
| digitalWrite(LED_BUILTIN, toggle); | |
| toggle = !toggle; | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // Always call this for MBED RP2040 after processing ISR | |
| TIMER_ISR_END(alarm_num); | |
| //////////////////////////////////////////////////////////// | |
| } | |
| voiddoingSomething2s() | |
| { | |
| unsignedlong currentMillis =millis(); | |
| deltaMillis2s = currentMillis - previousMillis2s; | |
| previousMillis2s = currentMillis; | |
| } | |
| voiddoingSomething5s() | |
| { | |
| unsignedlong currentMillis =millis(); | |
| deltaMillis5s = currentMillis - previousMillis5s; | |
| previousMillis5s = currentMillis; | |
| } | |
| ///////////////////////////////////////////////// | |
| #defineSIMPLE_TIMER_MS2000L | |
| // 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. | |
| voidsimpleTimerDoingSomething2s() | |
| { | |
| staticunsignedlong previousMillis = startMillis; | |
| unsignedlong currMillis =millis(); | |
| Serial.print(F("SimpleTimer : programmed")); Serial.print(SIMPLE_TIMER_MS); | |
| Serial.print(F("ms, current time ms :")); Serial.print(currMillis); | |
| Serial.print(F(", Delta ms :")); Serial.println(currMillis - previousMillis); | |
| Serial.print(F("Timer2s actual :")); Serial.println(deltaMillis2s); | |
| Serial.print(F("Timer5s actual :")); Serial.println(deltaMillis5s); | |
| previousMillis = currMillis; | |
| } | |
| //////////////////////////////////////////////// | |
| voidsetup() | |
| { | |
| pinMode(LED_BUILTIN, OUTPUT); | |
| Serial.begin(115200); | |
| while (!Serial); | |
| Serial.print(F("\nStarting ISR_Timers_Array_Simple on")); Serial.println(BOARD_NAME); | |
| Serial.println(MBED_RPI_PICO_TIMER_INTERRUPT_VERSION); | |
| if (ITimer1.attachInterruptInterval(TIMER_INTERVAL_MS *1000, TimerHandler)) | |
| { | |
| Serial.print(F("Starting ITimer1 OK, millis() =")); Serial.println(millis()); | |
| } | |
| else | |
| Serial.println(F("Can't set ITimer1. Select another freq. or timer")); | |
| previousMillis5s = previousMillis2s =millis(); | |
| ISR_timer.setInterval(2000L, doingSomething2s); | |
| ISR_timer.setInterval(5000L, doingSomething5s); | |
| // You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary. | |
| simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s); | |
| } | |
| #defineBLOCKING_TIME_MS10000L | |
| voidloop() | |
| { | |
| // 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 exampleISR_Timers_Array_Simple to demonstrate the accuracy of ISR Hardware Timer,especially when system is very busy. The ISR timer isprogrammed for 2s, is activated exactly after 2.000s !!!
While software timer,programmed for 2s, is activated after more than 10.000s !!!
Starting ISR_Timers_Array_Simple on RaspberryPi PicoMBED_RPi_Pico_TimerInterrupt v1.2.0[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00[TISR] _count = 0-1000[TISR] hardware_alarm_set_target, uS = 1000Starting ITimer1 OK, millis() = 1690SimpleTimer : programmed 2000ms, current time ms : 11290, Delta ms : 11290Timer2s actual : 2000Timer5s actual : 5000SimpleTimer : programmed 2000ms, current time ms : 20890, Delta ms : 9600Timer2s actual : 2000Timer5s actual : 5000SimpleTimer : programmed 2000ms, current time ms : 30490, Delta ms : 9600Timer2s actual : 2000Timer5s actual : 5000The following is the sample terminal output when running exampleTimerInterruptTest to demonstrate how to start/stop Hardware Timers on MBED RP2040-based boards.
Starting TimerInterruptTest on RaspberryPi PicoMBED_RPi_Pico_TimerInterrupt v1.2.0[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 1.00[TISR] _count = 0-1000000[TISR] hardware_alarm_set_target, uS = 1000000Starting ITimer0 OK, millis() = 1787[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.33[TISR] _count = 0-3000000[TISR] hardware_alarm_set_target, uS = 3000000Starting ITimer1 OK, millis() = 1789Stop ITimer0, millis() = 5001Start ITimer0, millis() = 10002Stop ITimer1, millis() = 15001Stop ITimer0, millis() = 15003Start ITimer0, millis() = 20004Stop ITimer0, millis() = 25005Start ITimer1, millis() = 30002Start ITimer0, millis() = 30006The following is the sample terminal output when running exampleChange_Interval to demonstrate how to change Timer Interval on-the-fly on MBED RP2040-based boards.
Starting Change_Interval on RaspberryPi PicoMBED_RPi_Pico_TimerInterrupt v1.2.0[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 0.50[TISR] _count = 0-2000000[TISR] hardware_alarm_set_target, uS = 2000000Starting ITimer0 OK, millis() = 1282[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.20[TISR] _count = 0-5000000[TISR] hardware_alarm_set_target, uS = 5000000Starting ITimer1 OK, millis() = 1284Time = 10001, Timer0Count = 4, Timer1Count = 1Time = 20002, Timer0Count = 9, Timer1Count = 3[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00[TISR] _count = 0 - 4000000[TISR] hardware_alarm_set_target, uS = 4000000[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00[TISR] _count = 0 - 10000000[TISR] hardware_alarm_set_target, uS = 10000000Changing Interval, Timer0 = 4000, Timer1 = 10000Time = 30003, Timer0Count = 11, Timer1Count = 3Time = 40004, Timer0Count = 14, Timer1Count = 4[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00[TISR] _count = 0 - 2000000[TISR] hardware_alarm_set_target, uS = 2000000[TISR] MBED_RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00[TISR] _count = 0 - 5000000[TISR] hardware_alarm_set_target, uS = 5000000Changing Interval, Timer0 = 2000, Timer1 = 5000Time = 50005, Timer0Count = 18, Timer1Count = 6The following is the sample terminal output when running exampleSwitchDebounce
Starting SwitchDebounce on RaspberryPi PicoMBED_RPi_Pico_TimerInterrupt v1.2.0[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00[TISR] _count = 0-1000[TISR] hardware_alarm_set_target, uS = 1000Starting ITimer1 OK, millis() = 1185SW Pressed, from millis() = 4537SW Released, from millis() = 4888SW Pressed total time ms = 351SW Pressed, from millis() = 5266SW Released, from millis() = 5604SW Pressed total time ms = 338SW Pressed, from millis() = 6600SW Long Pressed, total time ms = 11589 - 6600 = 4989SW Long Pressed, total time ms = 11640 - 6600 = 5040SW Long Pressed, total time ms = 11691 - 6600 = 5091SW Long Pressed, total time ms = 11742 - 6600 = 5142SW Long Pressed, total time ms = 11793 - 6600 = 5193SW Long Pressed, total time ms = 11844 - 6600 = 5244SW Long Pressed, total time ms = 11895 - 6600 = 5295SW Released, from millis() = 11996SW Pressed total time ms = 5396SW Pressed, from millis() = 13007SW Released, from millis() = 13155SW Pressed total time ms = 148The following is the sample terminal output when running exampleISR_16_Timers_Array_Complex to demonstrate the accuracy of ISR Hardware Timer,especially when system is very busy. The SimpleTimer,programmed for 2s, is activated only after nearly 10s !!!
Starting ISR_16_Timers_Array_Complex on RaspberryPi PicoMBED_RPi_Pico_TimerInterrupt v1.2.0[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 100.00[TISR] _count = 0-10000[TISR] hardware_alarm_set_target, uS = 10000Starting ITimer OK, millis() = 1621SimpleTimer : 2, ms : 11622, Dms : 10000Timer : 0, programmed : 5000, actual : 5009Timer : 1, programmed : 10000, actual : 0Timer : 2, programmed : 15000, actual : 0Timer : 3, programmed : 20000, actual : 0Timer : 4, programmed : 25000, actual : 0Timer : 5, programmed : 30000, actual : 0Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 21628, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10009Timer : 2, programmed : 15000, actual : 15009Timer : 3, programmed : 20000, actual : 0Timer : 4, programmed : 25000, actual : 0Timer : 5, programmed : 30000, actual : 0Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 31634, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20009Timer : 4, programmed : 25000, actual : 25009Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 41640, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25009Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 51646, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 61652, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 71658, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 65009Timer : 13, programmed : 70000, actual : 70009Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 81664, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 65009Timer : 13, programmed : 70000, actual : 70009Timer : 14, programmed : 75000, actual : 75009Timer : 15, programmed : 80000, actual : 80009...SimpleTimer : 2, ms : 161712, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45000Timer : 9, programmed : 50000, actual : 50000Timer : 10, programmed : 55000, actual : 55000Timer : 11, programmed : 60000, actual : 60000Timer : 12, programmed : 65000, actual : 65000Timer : 13, programmed : 70000, actual : 70000Timer : 14, programmed : 75000, actual : 75000Timer : 15, programmed : 80000, actual : 80000SimpleTimer : 2, ms : 171718, Dms : 10006Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45000Timer : 9, programmed : 50000, actual : 50000Timer : 10, programmed : 55000, actual : 55000Timer : 11, programmed : 60000, actual : 60000Timer : 12, programmed : 65000, actual : 65000Timer : 13, programmed : 70000, actual : 70000Timer : 14, programmed : 75000, actual : 75000Timer : 15, programmed : 80000, actual : 80000Debug 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 "MBED_RPI_PICO_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_TIMERINTERRUPT_LOGLEVEL_0
If 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:MBED_RPI_PICO_TimerInterrupt issues
- Search for bug and improvement.
- Basic hardware timers forRP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, usingArduino-mbed RP2040 core
- More hardware-initiated software-enabled timers
- Longer time interval
- Add Version String
- Add Table of Contents
- Fix
multiple-definitionslinker error - Optimize library code by using
reference-passinginstead ofvalue-passing - Using
floatinstead ofulongfor interval for better accuracy
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
- Neil Baylis to report issuePoor accuracy on timer interrupt frequency or interval. #4 leading to version v1.2.0 to fix
poor-timer-accuracybug
![]() Neil Baylis |
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 underMIT
Copyright 2021- Khoi Hoang
About
This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO. These MBED_RPI_PICO_TimerInterrupt 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 oth…
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.

