- Important Change from v1.4.0
- Why do we need this NRF52_TimerInterrupt library
- Changelog
- Prerequisites
- Installation
- Packages' Patches
- Libraries' Patches
- HOWTO Fix
Multiple Definitions
Linker Error - New from v1.0.0
- Usage
- Examples
- Example ISR_16_Timers_Array_Complex
- Debug Terminal Output Samples
- 1. ISR_Timer_Complex_Ethernet on Adafruit NRF52840_FEATHER using W5500 Ethernet
- 2. TimerInterruptTest on Adafruit NRF52840_FEATHER
- 3. Argument_None on Adafruit NRF52840_FEATHER
- 4. ISR_16_Timers_Array_Complex on Adafruit NRF52840_FEATHER
- 5. SwitchDebounce on Adafruit NRF52840_FEATHER
- 6. Change_Interval on Adafruit NRF52840_FEATHER
- 7. FakeAnalogWrite on Adafruit NRF52840_FEATHER
- Debug
- Troubleshooting
- Releases
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Please have a look at HOWTO Fix Multiple Definitions
Linker Error
Why do we need this NRF52_TimerInterrupt library
This library enables you to use Interrupt from Hardware Timers on an nRF52-based board, such as AdaFruit Itsy-Bitsy nRF52840, Feather nRF52840 Express, etc.
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:
- AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox etc.
- Sparkfun Pro nRF52840 Mini
- Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc. using Seeeduino
nRF%2
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 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.Adafruit nRF52 v1.3.0+
for nRF52 boards such as Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, NINA_B302_ublox, etc.Seeeduino nRF52 core 1.0.0+
for Seeeduino nRF52840-based boards such as Seeed_XIAO_NRF52840 and Seeed_XIAO_NRF52840_SENSE.Blynk library 1.1.0
. to use with certain example.- 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 BetterUIPEthernet library v2.0.12+
for ENC28J60.
WiFiNINA_Generic library v1.8.15-1+
to use WiFiNINA modules/shields. To install. check if using WiFiNINA for boards such as nRF52, etc.Blynk_WiFiNINA_WM library 1.1.2+
to use with Blynk-WiFiNINA-related example. To install. check- To use with certain example
SimpleTimer library
for ISR_16_Timers_Array and ISR_16_Timers_Array_Complex examples.
The best and easiest way is to use Arduino Library Manager
. Search for NRF52_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 NRF52_TimerInterrupt page.
- Download the latest release
NRF52_TimerInterrupt-main.zip
. - Extract the zip file to
NRF52_TimerInterrupt-main
directory - Copy whole
NRF52_TimerInterrupt-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install NRF52_TimerInterrupt library by using Library Manager. Search for NRF52_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
To be able to compile, run and automatically detect and display BOARD_NAME on nRF52840/nRF52832 boards, you have to copy the whole nRF52 1.3.0 directory into Adafruit nRF52 directory (~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0).
Supposing the Adafruit nRF52 version is 1.3.0. These files must be copied into the directory:
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/platform.txt
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/boards.txt
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B302_ublox/variant.h
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B302_ublox/variant.cpp
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B112_ublox/variant.h
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/variants/NINA_B112_ublox/variant.cpp
~/.arduino15/packages/adafruit/hardware/nrf52/1.3.0/cores/nRF5/Udp.h
Whenever a new version is installed, remember to copy these files into the new version directory. For example, new version is x.yy.z These files must be copied into the directory:
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/platform.txt
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/boards.txt
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B302_ublox/variant.h
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B302_ublox/variant.cpp
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B112_ublox/variant.h
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/variants/NINA_B112_ublox/variant.cpp
~/.arduino15/packages/adafruit/hardware/nrf52/x.yy.z/cores/nRF5/Udp.h
To use Sparkfun Pro nRF52840 Mini
, you must install Packages_Patches
and use Adafruit nrf52 core v1.0.0+
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 library
directory 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:
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 include .hpp
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "NRF52TimerInterrupt.hpp" //https://github.com/khoih-prog/NRF52_TimerInterrupt
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "NRF52_ISR_Timer.hpp" //https://github.com/khoih-prog/NRF52_TimerInterrupt
in many files. But be sure to use the following .h
files 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 "NRF52TimerInterrupt.h" //https://github.com/khoih-prog/NRF52_TimerInterrupt
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "NRF52_ISR_Timer.h" //https://github.com/khoih-prog/NRF52_TimerInterrupt
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_16_Timers_Array, ISR_Timer_Complex_Ethernet and ISR_Timer_Complex_WiFiNINA examples 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.
// Depending on the board, you can select NRF52 Hardware Timer from NRF_TIMER_1-NRF_TIMER_4 (1 to 4)
// If you select the already-used NRF_TIMER_0, it'll be auto modified to use NRF_TIMER_1
// Init NRF52 timer NRF_TIMER1
NRF52Timer ITimer(NRF_TIMER_1);
Use one of these functions with interval in unsigned long milliseconds
// interval (in microseconds).
// No params and duration now. To be added in the future by adding similar functions here or to NRF52-hal-timer.c
bool setInterval(unsigned long interval, timerCallback callback);
// interval (in microseconds).
// No params and duration now. To be added in the future by adding similar functions here or to NRF52-hal-timer.c
bool attachInterruptInterval(unsigned long interval, timerCallback callback);
as follows
void TimerHandler(void)
{
// Doing something here inside ISR
}
#define TIMER_INTERVAL_MS 1000 // 1s = 1000ms
void setup()
{
....
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler0))
{
Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer0. Select another freq. or timer));
}
Use one of these functions with frequency in float Hz
// frequency (in hertz).
// No params and duration now. To be added in the future by adding similar functions here or to NRF52-hal-timer.c
bool setFrequency(float frequency, timerCallback callback);
// frequency (in hertz).
bool attachInterrupt(float frequency, timerCallback callback);
as follows
void TimerHandler0()
{
// Doing something here inside ISR
}
#define TIMER0_FREQ_HZ 5555.555
void setup()
{
....
// Frequency in float Hz
if (ITimer0.attachInterrupt(TIMER0_FREQ_HZ, TimerHandler0))
{
Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer0. Select another freq. or timer));
}
The 16 ISR_based Timers, designed for long timer intervals, only support using unsigned long millisec intervals. If you have to use much higher frequency or sub-millisecond interval, you have to use the Hardware Timers directly as in 1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function
/// Depending on the board, you can select NRF52 Hardware Timer from NRF_TIMER_1-NRF_TIMER_4 (1 to 4)
// If you select the already-used NRF_TIMER_0, it'll be auto modified to use NRF_TIMER_1
// Init NRF52 timer NRF_TIMER2
NRF52Timer ITimer(NRF_TIMER_2);
// Init NRF52_ISR_Timer
// Each NRF52_ISR_Timer can service 16 different ISR-based timers
NRF52_ISR_Timer ISR_Timer;
void TimerHandler(void)
{
ISR_Timer.run();
}
#define HW_TIMER_INTERVAL_MS 50L
#define TIMER_INTERVAL_2S 2000L
#define TIMER_INTERVAL_5S 5000L
#define TIMER_INTERVAL_11S 11000L
#define TIMER_INTERVAL_101S 101000L
// In NRF52, 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_MS * 1000, TimerHandler))
{
lastMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(lastMillis);
}
else
Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));
// 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_16_Timers_Array_Complex
- ISR_RPM_Measure
- ISR_Timer_Complex_Ethernet
- ISR_Timer_Complex_WiFiNINA
- RPM_Measure
- SwitchDebounce
- TimerInterruptTest
- TimerInterruptLEDDemo
- FakeAnalogWrite.
- Change_Interval.
- multiFileProject. New
Example ISR_16_Timers_Array_Complex
The following is the sample terminal output when running example ISR_Timer_Complex_Ethernet on *Adafruit NRF52840_FEATHER EXPRESS using W5500 Ethernet 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 4.867s !!!. Then in loop(), it's also activated every 3s.
Starting ISR_Timer_Complex_Ethernet on NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER2, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 20.00, _count = 10000
Starting ITimer OK, millis() = 1419
[1419] Getting IP...
[1419] MAC: FE-BE-97-DA-C3-EA
_pinCS = 0
W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 10
W5100::init: W5500, SSIZE =4096
[3104] IP:192.168.2.129
[3104]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on ARDUINO_NRF52_ADAFRUIT
[3106] BlynkArduinoClient.connect: Connecting to account.duckdns.org:8080
[3218] Ready (ping: 8ms).
IP = 192.168.2.129
2s: Delta ms = 2000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 4867
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
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
2s: Delta ms = 2000
5s: Delta ms = 5000
2s: Delta ms = 2000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
2s: Delta ms = 2000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
2s: Delta ms = 2000
5s: Delta ms = 5000
2s: Delta ms = 2000
11s: Delta ms = 11000
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
21s: Delta ms = 21000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
2s: Delta ms = 2000
11s: Delta ms = 11000
5s: Delta ms = 5000
2s: Delta ms = 2000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
2s: Delta ms = 2000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
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
11s: Delta ms = 11000
blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
The following is the sample terminal output when running example TimerInterruptTest on Adafruit NRF52840_FEATHER to demonstrate the accuracy and how to start/stop Hardware Timers.
Starting TimerInterruptTest on NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER1, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 1.00, _count = 1000000
Starting ITimer0 OK, millis() = 1020
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER4, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 0.33, _count = 3000000
Starting ITimer1 OK, millis() = 1021
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
The following is the sample terminal output when running example Argument_None on Adafruit NRF52840_FEATHER to demonstrate the accuracy of Hardware Timers.
Starting Argument_None on NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER1, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 1.00, _count = 1000000
Starting ITimer0 OK, millis() = 1024
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER2, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 0.20, _count = 5000000
Starting ITimer1 OK, millis() = 1025
Time = 10001, Timer0Count = 8, , Timer1Count = 1
Time = 20002, Timer0Count = 18, , Timer1Count = 3
Time = 30003, Timer0Count = 28, , Timer1Count = 5
Time = 40004, Timer0Count = 38, , Timer1Count = 7
Time = 50005, Timer0Count = 48, , Timer1Count = 9
Time = 60006, Timer0Count = 58, , Timer1Count = 11
Time = 70007, Timer0Count = 68, , Timer1Count = 13
Time = 80008, Timer0Count = 78, , Timer1Count = 15
Time = 90009, Timer0Count = 88, , Timer1Count = 17
The following is the sample terminal output when running new example ISR_16_Timers_Array_Complex on Adafruit NRF52840_FEATHER 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 NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER2, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 100.00, _count = 10000
Starting ITimer OK, millis() = 1518
SimpleTimer : 2s, ms = 11409, Dms : 10000
Timer : 0, programmed : 5000, actual : 5005
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 = 21415, Dms : 10006
Timer : 0, programmed : 5000, actual : 4992
Timer : 1, programmed : 10000, actual : 9993
Timer : 2, programmed : 15000, actual : 15008
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 : 2s, ms = 31416, Dms : 10001
Timer : 0, programmed : 5000, actual : 4994
Timer : 1, programmed : 10000, actual : 10001
Timer : 2, programmed : 15000, actual : 14993
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25007
Timer : 5, programmed : 30000, actual : 30001
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 = 41417, Dms : 10001
Timer : 0, programmed : 5000, actual : 4994
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 14993
Timer : 3, programmed : 20000, actual : 20001
Timer : 4, programmed : 25000, actual : 25007
Timer : 5, programmed : 30000, actual : 30001
Timer : 6, programmed : 35000, actual : 35007
Timer : 7, programmed : 40000, actual : 40001
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 = 51438, Dms : 10021
Timer : 0, programmed : 5000, actual : 5005
Timer : 1, programmed : 10000, actual : 10006
Timer : 2, programmed : 15000, actual : 15001
Timer : 3, programmed : 20000, actual : 20001
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30001
Timer : 6, programmed : 35000, actual : 35007
Timer : 7, programmed : 40000, actual : 40001
Timer : 8, programmed : 45000, actual : 45002
Timer : 9, programmed : 50000, actual : 50007
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 = 61440, Dms : 10002
Timer : 0, programmed : 5000, actual : 4998
Timer : 1, programmed : 10000, actual : 9996
Timer : 2, programmed : 15000, actual : 15001
Timer : 3, programmed : 20000, actual : 20002
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30002
Timer : 6, programmed : 35000, actual : 35007
Timer : 7, programmed : 40000, actual : 40001
Timer : 8, programmed : 45000, actual : 45002
Timer : 9, programmed : 50000, actual : 50007
Timer : 10, programmed : 55000, actual : 55005
Timer : 11, programmed : 60000, actual : 60003
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 = 71444, Dms : 10004
Timer : 0, programmed : 5000, actual : 4994
Timer : 1, programmed : 10000, actual : 9998
Timer : 2, programmed : 15000, actual : 15001
Timer : 3, programmed : 20000, actual : 20002
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30002
Timer : 6, programmed : 35000, actual : 34994
Timer : 7, programmed : 40000, actual : 40001
Timer : 8, programmed : 45000, actual : 45002
Timer : 9, programmed : 50000, actual : 50007
Timer : 10, programmed : 55000, actual : 55005
Timer : 11, programmed : 60000, actual : 60003
Timer : 12, programmed : 65000, actual : 65007
Timer : 13, programmed : 70000, actual : 70001
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2s, ms = 81448, Dms : 10004
Timer : 0, programmed : 5000, actual : 4993
Timer : 1, programmed : 10000, actual : 9999
Timer : 2, programmed : 15000, actual : 15004
Timer : 3, programmed : 20000, actual : 19997
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30002
Timer : 6, programmed : 35000, actual : 34994
Timer : 7, programmed : 40000, actual : 39999
Timer : 8, programmed : 45000, actual : 45002
Timer : 9, programmed : 50000, actual : 50007
Timer : 10, programmed : 55000, actual : 55005
Timer : 11, programmed : 60000, actual : 60003
Timer : 12, programmed : 65000, actual : 65007
Timer : 13, programmed : 70000, actual : 70001
Timer : 14, programmed : 75000, actual : 75007
Timer : 15, programmed : 80000, actual : 80000
The following is the sample terminal output when running example SwitchDebounce on Adafruit NRF52840_FEATHER to demonstrate the usage of Hardware Timers for Switch Debouncing.
Starting SwitchDebounce on NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER1, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 100.00, _count = 10000
Starting ITimer OK, millis() = 1560
Time = 1560, Switch = Released
Time = 2561, Switch = Released
Time = 3562, Switch = Released
Time = 4563, Switch = Released
...
Time = 73632, Switch = Released
Time = 74633, Switch = Released
Time = 75634, Switch = Released
Time = 76635, Switch = Released
Time = 77636, Switch = Pressed
Time = 78637, Switch = Pressed
Time = 79638, Switch = Pressed
Time = 80639, Switch = Pressed
Time = 81640, Switch = Pressed
Time = 82641, Switch = LongPressed
Time = 83642, Switch = LongPressed
Time = 84643, Switch = LongPressed
Time = 85644, Switch = Released
Time = 86645, Switch = Released
Time = 87646, Switch = Released
The following is the sample terminal output when running example Change_Interval on Adafruit NRF52840_FEATHER to demonstrate how to change Timer Interval on-the-fly
Starting Change_Interval on NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER4, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 2.00, _count = 500000
Starting ITimer0 OK, millis() = 1321
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER3, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 0.50, _count = 2000000
Starting ITimer1 OK, millis() = 1322
Time = 10001, Timer0Count = 17, , Timer1Count = 4
Time = 20002, Timer0Count = 37, , Timer1Count = 9
Changing Interval, Timer0 = 1000, Timer1 = 4000
Time = 30003, Timer0Count = 47, , Timer1Count = 11
Time = 40004, Timer0Count = 57, , Timer1Count = 14
Changing Interval, Timer0 = 500, Timer1 = 2000
Time = 50005, Timer0Count = 77, , Timer1Count = 19
Time = 60006, Timer0Count = 97, , Timer1Count = 24
Changing Interval, Timer0 = 1000, Timer1 = 4000
Time = 70007, Timer0Count = 107, , Timer1Count = 26
Time = 80008, Timer0Count = 117, , Timer1Count = 29
Changing Interval, Timer0 = 500, Timer1 = 2000
Time = 90009, Timer0Count = 137, , Timer1Count = 34
Time = 100010, Timer0Count = 157, , Timer1Count = 39
Changing Interval, Timer0 = 1000, Timer1 = 4000
Time = 110011, Timer0Count = 167, , Timer1Count = 41
Time = 120012, Timer0Count = 177, , Timer1Count = 44
Changing Interval, Timer0 = 500, Timer1 = 2000
Time = 130013, Timer0Count = 197, , Timer1Count = 49
Time = 140014, Timer0Count = 217, , Timer1Count = 54
Changing Interval, Timer0 = 1000, Timer1 = 4000
Time = 150015, Timer0Count = 227, , Timer1Count = 56
Time = 160016, Timer0Count = 237, , Timer1Count = 59
Changing Interval, Timer0 = 500, Timer1 = 2000
Time = 170017, Timer0Count = 257, , Timer1Count = 64
Time = 180018, Timer0Count = 277, , Timer1Count = 69
The following is the sample terminal output when running example FakeAnalogWrite on Adafruit NRF52840_FEATHER to demonstrate how to use analogWrite to many pins to overcome the limitation of nRF52 analogWrite to only 4 pins or crash. Check Arduino Nano 33 BLE mbed os crashes when PWM on more than 3 digital pins
Starting FakeAnalogWrite on NRF52840_FEATHER
NRF52TimerInterrupt v1.4.2
CPU Frequency = 64 MHz
[TISR] F_CPU (MHz) = 64, Timer = NRF_TIMER3, Timer Clock (Hz) = 1000000.00
[TISR] Frequency = 10000.00, _count = 100
Starting ITimer OK, millis() = 1024
Add index = 0, pin = 2, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 1, pin = 3, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 2, pin = 4, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 3, pin = 5, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 4, pin = 6, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 5, pin = 7, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 6, pin = 8, input PWM_Value = 0, mapped PWM_Value = 0
Add index = 7, pin = 9, input PWM_Value = 0, mapped PWM_Value = 0
Test PWM_Value = 0, max = 255
Update index 0, pin = 2, input PWM_Value=5, mapped PWM_Value= 14
Update index 1, pin = 3, input PWM_Value=5, mapped PWM_Value= 14
Update index 2, pin = 4, input PWM_Value=5, mapped PWM_Value= 14
Update index 3, pin = 5, input PWM_Value=5, mapped PWM_Value= 14
Update index 4, pin = 6, input PWM_Value=5, mapped PWM_Value= 14
Update index 5, pin = 7, input PWM_Value=5, mapped PWM_Value= 14
Update index 6, pin = 8, input PWM_Value=5, mapped PWM_Value= 14
Update index 7, pin = 9, input PWM_Value=5, mapped PWM_Value= 14
Test PWM_Value = 5, max = 255
Update index 0, pin = 2, input PWM_Value=10, mapped PWM_Value= 27
Update index 1, pin = 3, input PWM_Value=10, mapped PWM_Value= 27
Update index 2, pin = 4, input PWM_Value=10, mapped PWM_Value= 27
Update index 3, pin = 5, input PWM_Value=10, mapped PWM_Value= 27
Update index 4, pin = 6, input PWM_Value=10, mapped PWM_Value= 27
Update index 5, pin = 7, input PWM_Value=10, mapped PWM_Value= 27
Update index 6, pin = 8, input PWM_Value=10, mapped PWM_Value= 27
Update index 7, pin = 9, input PWM_Value=10, mapped PWM_Value= 27
...
Test PWM_Value = 145, max = 255
Update index 0, pin = 2, input PWM_Value=150, mapped PWM_Value= 135
Update index 1, pin = 3, input PWM_Value=150, mapped PWM_Value= 135
Update index 2, pin = 4, input PWM_Value=150, mapped PWM_Value= 135
Update index 3, pin = 5, input PWM_Value=150, mapped PWM_Value= 135
Update index 4, pin = 6, input PWM_Value=150, mapped PWM_Value= 135
Update index 5, pin = 7, input PWM_Value=150, mapped PWM_Value= 135
Update index 6, pin = 8, input PWM_Value=150, mapped PWM_Value= 135
Update index 7, pin = 9, input PWM_Value=150, mapped PWM_Value= 135
Test PWM_Value = 150, max = 255
Update index 0, pin = 2, input PWM_Value=155, mapped PWM_Value= 137
Update index 1, pin = 3, input PWM_Value=155, mapped PWM_Value= 137
Update index 2, pin = 4, input PWM_Value=155, mapped PWM_Value= 137
Update index 3, pin = 5, input PWM_Value=155, mapped PWM_Value= 137
Update index 4, pin = 6, input PWM_Value=155, mapped PWM_Value= 137
Update index 5, pin = 7, input PWM_Value=155, mapped PWM_Value= 137
Update index 6, pin = 8, input PWM_Value=155, mapped PWM_Value= 137
Update index 7, pin = 9, input PWM_Value=155, mapped PWM_Value= 137
Test PWM_Value = 155, max = 255
Update index 0, pin = 2, input PWM_Value=160, mapped PWM_Value= 138
Update index 1, pin = 3, input PWM_Value=160, mapped PWM_Value= 138
Update index 2, pin = 4, input PWM_Value=160, mapped PWM_Value= 138
Update index 3, pin = 5, input PWM_Value=160, mapped PWM_Value= 138
Update index 4, pin = 6, input PWM_Value=160, mapped PWM_Value= 138
Update index 5, pin = 7, input PWM_Value=160, mapped PWM_Value= 138
Update index 6, pin = 8, input PWM_Value=160, mapped PWM_Value= 138
Update index 7, pin = 9, input PWM_Value=160, mapped PWM_Value= 138
Test PWM_Value = 160, max = 255
Update index 0, pin = 2, input PWM_Value=165, mapped PWM_Value= 141
Update index 1, pin = 3, input PWM_Value=165, mapped PWM_Value= 141
Update index 2, pin = 4, input PWM_Value=165, mapped PWM_Value= 141
Update index 3, pin = 5, input PWM_Value=165, mapped PWM_Value= 141
Update index 4, pin = 6, input PWM_Value=165, mapped PWM_Value= 141
Update index 5, pin = 7, input PWM_Value=165, mapped PWM_Value= 141
Update index 6, pin = 8, input PWM_Value=165, mapped PWM_Value= 141
Update index 7, pin = 9, input PWM_Value=165, mapped PWM_Value= 141
Test PWM_Value = 165, max = 255
Update index 0, pin = 2, input PWM_Value=170, mapped PWM_Value= 143
Update index 1, pin = 3, input PWM_Value=170, mapped PWM_Value= 143
Update index 2, pin = 4, input PWM_Value=170, mapped PWM_Value= 143
Update index 3, pin = 5, input PWM_Value=170, mapped PWM_Value= 143
Update index 4, pin = 6, input PWM_Value=170, mapped PWM_Value= 143
Update index 5, pin = 7, input PWM_Value=170, mapped PWM_Value= 143
Update index 6, pin = 8, input PWM_Value=170, mapped PWM_Value= 143
Update index 7, pin = 9, input PWM_Value=170, mapped PWM_Value= 143
Test PWM_Value = 170, max = 255
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 "NRF52TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 0
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: NRF52_TimerInterrupt issues
- Search for bug and improvement.
- Basic hardware timers for
NRF52832
andNRF52840
- More hardware-initiated software-enabled timers
- Longer time interval
- Similar features for remaining Arduino boards such as AVR, ESP32, ESP8266, STM32, SAM-DUE, SAMD21/SAMD51, mbed-nRF52, Teensy, etc.
- Add Table of Contents
- Fix
multiple-definitions
linker error - Optimize library code by using
reference-passing
instead ofvalue-passing
- Add support to
Sparkfun Pro nRF52840 Mini
- Add support to Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc. using Seeeduino
nRF%2
core - Add astyle using
allman
style. Restyle the library
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
- Thanks to good work of Miguel Wisintainer for working with, developing, debugging and testing.
Miguel Wisintainer |
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