Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as RASPBERRY_PI_PICO. These 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 other software timers using m…

License

NotificationsYou must be signed in to change notification settings

khoih-prog/RPI_PICO_TimerInterrupt

Repository files navigation

arduino-library-badgeGitHub releaseGitHubcontributions welcomeGitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Important Change from v1.2.0

Please have a look atHOWTO FixMultiple Definitions Linker Error


Features

This library enables you to use Interrupt from Hardware Timers on on RP2040-based boards such as RASPBERRY_PI_PICO, usingEarle Philhower's arduino-pico core. Support toArduino-mbed RP2040 core will be added in future releases.

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_Timers_Array_Simple 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

Why using ISR-based Hardware Timer Interrupt is better

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:

HOWTO Attach Interrupt


Currently supported Boards

  1. RP2040-based boards such asRASPBERRY_PI_PICO, RASPBERRY_PI_PICO_W, ADAFRUIT_FEATHER_RP2040, Nano_RP2040_Connect, GENERIC_RP2040, etc., usingarduino-pico core

Important Notes about ISR

  1. Inside the attached function,delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare asvolatile any variables that you modify within the attached function.

  2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino.GitHub release
  2. Earle Philhower's arduino-pico core v2.6.3+ for RP2040-based boards such asRASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc.GitHub release
  3. To use with certain example, depending on which Ethernet card you're using:
  4. To use with certain example


Installation

Use Arduino Library Manager

The best and easiest way is to useArduino Library Manager. Search forRPI_PICO_TimerInterrupt, then select / install the latest version.You can also use this linkarduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate toRPI_PICO_TimerInterrupt page.
  2. Download the latest releaseRPI_PICO_TimerInterrupt-main.zip.
  3. Extract the zip file toRPI_PICO_TimerInterrupt-main directory
  4. Copy wholeRPI_PICO_TimerInterrupt-main folder to Arduino libraries' directory such as~/Arduino/libraries/.

VS Code & PlatformIO

  1. InstallVS Code
  2. InstallPlatformIO
  3. InstallRPI_PICO_TimerInterrupt library by usingLibrary Manager. Search forRPI_PICO_TimerInterrupt inPlatform.io Author's Libraries
  4. Please visit documentation for the other options and examples atProject Configuration File


Packages' Patches

1. For RP2040-based boards usingEarle Philhower arduino-pico core

Important: Only necessary if you use core v1.4.0-

1.1 To use BOARD_NAME

To be able to automatically detect and display BOARD_NAME on RP2040-based boards (RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040, GENERIC_RP2040, etc) boards, you have to copy the fileRP2040 platform.txt into rp2040 directory (~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0).

Supposing the rp2040 core version is 1.4.0. This file must be copied into the directory:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0/platform.txt

Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zzThis file must be copied into the directory:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/x.yy.zz/platform.txt

With core after v1.4.0, this step is not necessary anymore thanks to the PRAdd -DBOARD_NAME="{build.board}" #136.

1.2 To avoid compile error relating to microsecondsToClockCycles

Some libraries, such asAdafruit DHT-sensor-library, require the definition of microsecondsToClockCycles().To be able to compile and run on RP2040-based boards, you have to copy the files inRP2040 Arduino.h into rp2040 directory (~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0).

Supposing the rp2040 core version is 1.4.0. This file must be copied to replace:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/1.4.0/cores/rp2040/Arduino.h

Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zzThis file must be copied to replace:

  • ~/.arduino15/packages/rp2040/hardware/rp2040/x.yy.zz/cores/rp2040/Arduino.h

With core after v1.4.0, this step is not necessary anymore thanks to the PRAdd defs for compatibility #142.



HOWTO FixMultiple Definitions Linker Error

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 or.h files

// Can be included as many times as necessary, without `Multiple Definitions` Linker Error#include"RPi_Pico_TimerInterrupt.h"//https://github.com/khoih-prog/RPI_PICO_TimerInterrupt// Can be included as many times as necessary, without `Multiple Definitions` Linker Error#include"RPi_Pico_ISR_Timer.hpp"//https://github.com/khoih-prog/RPI_PICO_TimerInterrupt

in many files. But be sure to use the following.h filein 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"RPi_Pico_ISR_Timer.h"//https://github.com/khoih-prog/RPI_PICO_TimerInterrupt

Check the newmultiFileProject example for aHOWTO demo.



More useful Information

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



Usage

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.

1. Using only Hardware Timer directly

1.1 Init Hardware Timer

// Select the timer you're using, from ITimer0(0)-ITimer3(3)// Init RPI_PICO_TimerRPI_PICO_TimerITimer1(1);

1.2 Set Hardware Timer Interval and attach Timer Interrupt Handler function

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

voidTimerHandler(){// Doing something here inside ISR}#defineTIMER_INTERVAL_MS5000L// Init RPI_PICO_TimerRPI_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");}

1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function

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

voidTimerHandler(){// Doing something here inside ISR}#defineTIMER_FREQ_HZ5555.555// Init RPI_PICO_TimerRPI_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");}

2. Using 16 ISR_based Timers from 1 Hardware Timer

2.1 Important Note

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

2.2 Init Hardware Timer and ISR-based Timer

// Init RPI_PICO_TimerRPI_PICO_TimerITimer1(1);// Init ISR_Timer// Each ISR_Timer can service 16 different ISR-based timersRPI_PICO_ISR_Timer ISR_timer;

2.3 Set Hardware Timer Interval and attach Timer Interrupt Handler functions

voidTimerHandler(){  ISR_timer.run();}#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);}


Examples

  1. Argument_Complex
  2. Argument_None
  3. Argument_Simple
  4. Change_Interval
  5. ISR_16_Timers_Array_Complex
  6. ISR_Timers_Array_Simple
  7. RPM_Measure
  8. SwitchDebounce
  9. TimerInterruptTest
  10. multiFileProject.New


// These define's must be placed at the beginning before #include "TimerInterrupt_Generic.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#defineTIMER_INTERRUPT_DEBUG1
#define_TIMERINTERRUPT_LOGLEVEL_4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include"RPi_Pico_TimerInterrupt.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include"RPi_Pico_ISR_Timer.h"
#include<SimpleTimer.h>// https://github.com/jfturcot/SimpleTimer
// Init RPI_PICO_Timer
RPI_PICO_TimerITimer1(1);
RPI_PICO_ISR_Timer 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;
boolTimerHandler(structrepeating_timer *t)
{
(void) t;
staticbool toggle =false;
staticint timeRun =0;
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;
}
returntrue;
}
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(RPI_PICO_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency =")); Serial.print(F_CPU /1000000); Serial.println(F(" MHz"));
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();
}



Debug Terminal Output Samples

1. ISR_Timers_Array_Simple on RASPBERRY_PI_PICO

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 RASPBERRY_PI_PICORPi_Pico_TimerInterrupt v1.3.1CPU Frequency = 125 MHz[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00[TISR] _count = 0-1000[TISR] add_repeating_timer_us = 1000Starting  ITimer3 OK, millis() = 1707SimpleTimer : programmed 2000ms, current time ms : 11707, Delta ms : 11707Timer2s actual : 2000Timer5s actual : 5000SimpleTimer : programmed 2000ms, current time ms : 21708, Delta ms : 10001Timer2s actual : 2000Timer5s actual : 5000SimpleTimer : programmed 2000ms, current time ms : 31708, Delta ms : 10000Timer2s actual : 2000Timer5s actual : 5000

2. TimerInterruptTest on RASPBERRY_PI_PICO

The following is the sample terminal output when running exampleTimerInterruptTest to demonstrate how to start/stop Hardware Timers on RP2040-based boards.

Starting TimerInterruptTest on RASPBERRY_PI_PICORPi_Pico_TimerInterrupt v1.3.1CPU Frequency = 125 MHz[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 1.00[TISR] _count = 0-1000000[TISR] add_repeating_timer_us = 1000000Starting ITimer0 OK, millis() = 882[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.33[TISR] _count = 0-3000000[TISR] add_repeating_timer_us = 3000000Starting  ITimer1 OK, millis() = 1782ITimer0 called, millis() = 2781ITimer0 called, millis() = 3781ITimer0 called, millis() = 4781ITimer1 called, millis() = 4782Stop ITimer0, millis() = 5001ITimer1 called, millis() = 7782Start ITimer0, millis() = 10002ITimer1 called, millis() = 10782ITimer0 called, millis() = 11002ITimer0 called, millis() = 12002ITimer0 called, millis() = 13002ITimer1 called, millis() = 13782ITimer0 called, millis() = 14002Stop ITimer1, millis() = 15001ITimer0 called, millis() = 15002Stop ITimer0, millis() = 15003Start ITimer0, millis() = 20004ITimer0 called, millis() = 21004ITimer0 called, millis() = 22004ITimer0 called, millis() = 23004ITimer0 called, millis() = 24004ITimer0 called, millis() = 25004Stop ITimer0, millis() = 25005

3. Change_Interval on RASPBERRY_PI_PICO

The following is the sample terminal output when running exampleChange_Interval to demonstrate how to change Timer Interval on-the-fly on RP2040-based boards.

Starting Change_Interval on RASPBERRY_PI_PICORPi_Pico_TimerInterrupt v1.3.1CPU Frequency = 125 MHz[TISR] _timerNo = 0, Clock (Hz) = 1000000.00, _fre (Hz) = 0.50[TISR] _count = 0-2000000[TISR] add_repeating_timer_us = 2000000Starting  ITimer0 OK, millis() = 2363[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 0.20[TISR] _count = 0-5000000[TISR] add_repeating_timer_us = 5000000Starting  ITimer1 OK, millis() = 1544ITimer0: millis() = 3544ITimer0: millis() = 5544ITimer1: millis() = 6544ITimer0: millis() = 7544ITimer0: millis() = 9544Time = 10001, Timer0Count = 4, Timer1Count = 1ITimer0: millis() = 11544ITimer1: millis() = 11544ITimer0: millis() = 13544ITimer0: millis() = 15544ITimer1: millis() = 16544ITimer0: millis() = 17544ITimer0: millis() = 19544Time = 20002, Timer0Count = 9, Timer1Count = 3[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00[TISR] _count = 0 - 4000000[TISR] add_repeating_timer_us = 4000000[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00[TISR] _count = 0 - 10000000[TISR] add_repeating_timer_us = 10000000Changing Interval, Timer0 = 4000,  Timer1 = 10000ITimer0: millis() = 24002ITimer0: millis() = 28002ITimer1: millis() = 30003Time = 30003, Timer0Count = 11, Timer1Count = 4ITimer0: millis() = 32002ITimer0: millis() = 36002ITimer0: millis() = 40002ITimer1: millis() = 40003Time = 40004, Timer0Count = 14, Timer1Count = 5[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00[TISR] _count = 0 - 2000000[TISR] add_repeating_timer_us = 2000000[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00[TISR] _count = 0 - 5000000[TISR] add_repeating_timer_us = 5000000Changing Interval, Timer0 = 2000,  Timer1 = 5000ITimer0: millis() = 42004ITimer0: millis() = 44004ITimer1: millis() = 45004ITimer0: millis() = 46004ITimer0: millis() = 48004ITimer0: millis() = 50004ITimer1: millis() = 50004Time = 50005, Timer0Count = 19, Timer1Count = 7ITimer0: millis() = 52004ITimer0: millis() = 54004ITimer1: millis() = 55005ITimer0: millis() = 56004ITimer0: millis() = 58004ITimer0: millis() = 60004ITimer1: millis() = 60005Time = 60006, Timer0Count = 24, Timer1Count = 9[TISR] RPI_PICO_TimerInterrupt: _timerNo = 0 , _fre = 1000000.00[TISR] _count = 0 - 4000000[TISR] add_repeating_timer_us = 4000000[TISR] RPI_PICO_TimerInterrupt: _timerNo = 1 , _fre = 1000000.00[TISR] _count = 0 - 10000000[TISR] add_repeating_timer_us = 10000000Changing Interval, Timer0 = 4000,  Timer1 = 10000ITimer0: millis() = 64006

4. SwitchDebounce on RASPBERRY_PI_PICO

The following is the sample terminal output when running exampleSwitchDebounce

Starting SwitchDebounce on RASPBERRY_PI_PICORPi_Pico_TimerInterrupt v1.3.1CPU Frequency = 125 MHz[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 50.00[TISR] _count = 0-20000[TISR] add_repeating_timer_us = 20000Starting  ITimer1 OK, millis() = 1302SW Press, from millis() = 77377SW Released, from millis() = 78077SW Pressed total time ms = 700SW Press, from millis() = 78257SW Released, from millis() = 78577SW Pressed total time ms = 320SW Press, from millis() = 79057SW Released, from millis() = 80238SW Pressed total time ms = 1181

5. ISR_Timers_Array_Simple on ADAFRUIT_FEATHER_RP2040

The following is the sample terminal output when running exampleISR_Timers_Array_Simple on ADAFRUIT_FEATHER_RP2040

Starting ISR_Timers_Array_Simple on RASPBERRY_PI_PICORPi_Pico_TimerInterrupt v1.3.1CPU Frequency = 125 MHz[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 1000.00[TISR] _count = 0-1000[TISR] add_repeating_timer_us = 1000Starting ITimer1 OK, millis() = 1701SimpleTimer : programmed 2000ms, current time ms : 11707, Delta ms : 11707Timer2s actual : 2000Timer5s actual : 5000SimpleTimer : programmed 2000ms, current time ms : 21707, Delta ms : 10000Timer2s actual : 2000Timer5s actual : 5000SimpleTimer : programmed 2000ms, current time ms : 31708, Delta ms : 10001Timer2s actual : 2000Timer5s actual : 5000

6. ISR_16_Timers_Array_Complex on ADAFRUIT_ITSYBITSY_RP2040

The following is the sample terminal output when running exampleISR_16_Timers_Array_Complex on ADAFRUIT_ITSYBITSY_RP2040

Starting ISR_16_Timers_Array_Complex on RASPBERRY_PI_PICORPi_Pico_TimerInterrupt v1.3.1CPU Frequency = 125 MHz[TISR] _timerNo = 1, Clock (Hz) = 1000000.00, _fre (Hz) = 100.00[TISR] _count = 0-10000[TISR] add_repeating_timer_us = 10000Starting ITimer OK, millis() = 1797SimpleTimer : 2, ms : 11798, Dms : 10001Timer : 0, programmed : 5000, actual : 5005Timer : 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 : 21800, Dms : 10002Timer : 0, programmed : 5000, actual : 4995Timer : 1, programmed : 10000, actual : 10010Timer : 2, programmed : 15000, actual : 15005Timer : 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 : 31802, Dms : 10002Timer : 0, programmed : 5000, actual : 4995Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15005Timer : 3, programmed : 20000, actual : 20010Timer : 4, programmed : 25000, actual : 25005Timer : 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 : 41804, Dms : 10002Timer : 0, programmed : 5000, actual : 4995Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15005Timer : 3, programmed : 20000, actual : 20010Timer : 4, programmed : 25000, actual : 25005Timer : 5, programmed : 30000, actual : 30010Timer : 6, programmed : 35000, actual : 35005Timer : 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 : 51807, Dms : 10003Timer : 0, programmed : 5000, actual : 4995Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 14995Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25005Timer : 5, programmed : 30000, actual : 30010Timer : 6, programmed : 35000, actual : 35005Timer : 7, programmed : 40000, actual : 40010Timer : 8, programmed : 45000, actual : 45005Timer : 9, programmed : 50000, actual : 50010Timer : 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 : 61809, Dms : 10002Timer : 0, programmed : 5000, actual : 5005Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15005Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25005Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35005Timer : 7, programmed : 40000, actual : 40010Timer : 8, programmed : 45000, actual : 45005Timer : 9, programmed : 50000, actual : 50010Timer : 10, programmed : 55000, actual : 55005Timer : 11, programmed : 60000, actual : 60010Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 71812, Dms : 10003Timer : 0, programmed : 5000, actual : 5005Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15005Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25005Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35005Timer : 7, programmed : 40000, actual : 40010Timer : 8, programmed : 45000, actual : 45005Timer : 9, programmed : 50000, actual : 50010Timer : 10, programmed : 55000, actual : 55005Timer : 11, programmed : 60000, actual : 60010Timer : 12, programmed : 65000, actual : 65005Timer : 13, programmed : 70000, actual : 70010Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 81814, Dms : 10002Timer : 0, programmed : 5000, actual : 5005Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 14995Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 24995Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35005Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45005Timer : 9, programmed : 50000, actual : 50010Timer : 10, programmed : 55000, actual : 55005Timer : 11, programmed : 60000, actual : 60010Timer : 12, programmed : 65000, actual : 65005Timer : 13, programmed : 70000, actual : 70010Timer : 14, programmed : 75000, actual : 75005Timer : 15, programmed : 80000, actual : 80010


Debug

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 "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.#defineTIMER_INTERRUPT_DEBUG0#define_TIMERINTERRUPT_LOGLEVEL_0

Troubleshooting

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.



Issues

Submit issues to:RPI_PICO_TimerInterrupt issues


TO DO

  1. Search for bug and improvement.
  2. Add support to RP2040-based boards such as RASPBERRY_PI_PICO, usingArduino-mbed RP2040 core

DONE

  1. Basic hardware timers forRP2040-based boards such as RASPBERRY_PI_PICO, usingEarle Philhower's arduino-pico core
  2. More hardware-initiated software-enabled timers
  3. Longer time interval
  4. Add Version String
  5. Add Table of Contents
  6. Add support to new boards (ADAFRUIT_ITSYBITSY_RP2040, ADAFRUIT_QTPY_RP2040, ADAFRUIT_STEMMAFRIEND_RP2040, ADAFRUIT_TRINKEYQT_RP2040, ADAFRUIT_MACROPAD_RP2040, SPARKFUN_PROMICRO_RP2040, Nano_RP2040_Connect, etc.) using the arduino-pico core
  7. Fixmultiple-definitions linker error
  8. Optimize library code by usingreference-passing instead ofvalue-passing
  9. Fix severe bug affecting time between the starts. CheckEnable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3
  10. Usingfloat instead ofulong for better interval accuracy


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.

  1. AndreasOKircher to report issueEnable fixed timing between timer calls (vs fixed time btw. end of timer call and next call as implemented) #3 leading to version v1.3.0 to fixsevere bug affecting time between the starts
AndreasOKircher
AndreasOKircher


Contributing

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

License

  • The library is licensed underMIT

Copyright

Copyright 2021- Khoi Hoang

About

This library enables you to use Interrupt from Hardware Timers on RP2040-based boards such as RASPBERRY_PI_PICO. These 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 other software timers using m…

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp