khoih-prog / NRF52_TimerInterrupt

This library enables you to use Interrupt from Hardware Timers on an nRF52-based board. These nRF52 Hardware Timers, using Interrupt, still work even if other functions are blocking. Moreover, they are much more precise than other software timers using millis() or micros(). Now supports `Sparkfun Pro nRF52840 Mini`

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NRF52_TimerInterrupt Library

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Important Change from v1.4.0

Please have a look at HOWTO Fix Multiple Definitions Linker Error

Why do we need this NRF52_TimerInterrupt library

Features

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

Why using ISR-based Hardware Timer Interrupt is better

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:

HOWTO Attach Interrupt


Currently supported Boards

  1. AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox etc.
  2. Sparkfun Pro nRF52840 Mini
  3. Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc. using Seeeduino nRF%2 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 as volatile 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. 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. GitHub release
  3. Seeeduino nRF52 core 1.0.0+ for Seeeduino nRF52840-based boards such as Seeed_XIAO_NRF52840 and Seeed_XIAO_NRF52840_SENSE. GitHub release
  4. Blynk library 1.1.0. Latest release to use with certain example.
  5. To use with certain example, depending on which Ethernet card you're using:
  1. WiFiNINA_Generic library v1.8.15-1+ to use WiFiNINA modules/shields. To install. check arduino-library-badge if using WiFiNINA for boards such as nRF52, etc.
  2. Blynk_WiFiNINA_WM library 1.1.2+ to use with Blynk-WiFiNINA-related example. To install. check arduino-library-badge
  3. To use with certain example


Installation

Use Arduino Library Manager

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 arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

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

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install NRF52_TimerInterrupt library by using Library Manager. Search for NRF52_TimerInterrupt in Platform.io Author's Libraries
  4. 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


Packages' Patches

1. For Adafruit nRF52840 and nRF52832 boards

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+



Libraries' Patches

Notes: These patches are totally optional and necessary only when you use the related Ethernet library and get certain error or issues.

1. For application requiring 2K+ HTML page

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.

2. For Ethernet library

To fix Ethernet library, just copy these following files into the Ethernet library directory to overwrite the old files:

3. For EthernetLarge library

To fix EthernetLarge library, just copy these following files into the EthernetLarge library directory to overwrite the old files:

4. For Ethernet2 library

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:

5. For Ethernet3 library

  1. To fix Ethernet3 library, just copy these following files into the Ethernet3 library directory to overwrite the old files:

6. For UIPEthernet library

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:



HOWTO Fix Multiple Definitions Linker Error

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


New from v1.0.0

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



Usage

Before using any Timer, you have to make sure the Timer has not been used by any other purpose.

1. Using only Hardware Timer directly

1.1 Init Hardware Timer

// 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);

1.2 Set Hardware Timer Interval and attach Timer Interrupt Handler function

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));
}  

1.3 Set Hardware Timer Frequency and attach Timer Interrupt Handler function

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));
}  

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

2.2 Init Hardware Timer and ISR-based Timer

/// 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;

2.3 Set Hardware Timer Interval and attach Timer Interrupt Handler functions

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);
}  


Examples:

  1. Argument_None
  2. ISR_16_Timers_Array
  3. ISR_16_Timers_Array_Complex
  4. ISR_RPM_Measure
  5. ISR_Timer_Complex_Ethernet
  6. ISR_Timer_Complex_WiFiNINA
  7. RPM_Measure
  8. SwitchDebounce
  9. TimerInterruptTest
  10. TimerInterruptLEDDemo
  11. FakeAnalogWrite.
  12. Change_Interval.
  13. multiFileProject. New


// 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.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 3
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "NRF52TimerInterrupt.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "NRF52_ISR_Timer.h"
#include <SimpleTimer.h> // https://github.com/schinken/SimpleTimer
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
#ifndef LED_BLUE_PIN
#if defined(LED_BLUE)
#define LED_BLUE_PIN LED_BLUE
#else
#define LED_BLUE_PIN 7
#endif
#endif
#ifndef LED_GREEN_PIN
#if defined(LED_GREEN)
#define LED_GREEN_PIN LED_GREEN
#else
#define LED_GREEN_PIN 8
#endif
#endif
#define HW_TIMER_INTERVAL_US 10000L
volatile uint32_t startMillis = 0;
// 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_2);
// Init NRF52_ISR_Timer
// Each NRF52_ISR_Timer can service 16 different ISR-based timers
NRF52_ISR_Timer ISR_Timer;
#define LED_TOGGLE_INTERVAL_MS 2000L
void TimerHandler()
{
static bool toggle = false;
static int timeRun = 0;
ISR_Timer.run();
// Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s
if (++timeRun == ((LED_TOGGLE_INTERVAL_MS * 1000) / HW_TIMER_INTERVAL_US) )
{
timeRun = 0;
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
}
/////////////////////////////////////////////////
#define NUMBER_ISR_TIMERS 16
typedef void (*irqCallback) ();
/////////////////////////////////////////////////
#define USE_COMPLEX_STRUCT true
#if USE_COMPLEX_STRUCT
typedef struct
{
irqCallback irqCallbackFunc;
uint32_t TimerInterval;
unsigned long deltaMillis;
unsigned long previousMillis;
} ISRTimerData;
// In NRF52, avoid doing something fancy in ISR, for example Serial.print()
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething(int index);
#else
volatile unsigned long deltaMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMillis [NUMBER_ISR_TIMERS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// You can assign any interval for any timer here, in milliseconds
uint32_t TimerInterval[NUMBER_ISR_TIMERS] =
{
5000L, 10000L, 15000L, 20000L, 25000L, 30000L, 35000L, 40000L,
45000L, 50000L, 55000L, 60000L, 65000L, 70000L, 75000L, 80000L
};
void doingSomething(int index)
{
unsigned long currentMillis = millis();
deltaMillis[index] = currentMillis - previousMillis[index];
previousMillis[index] = currentMillis;
}
#endif
////////////////////////////////////
// Shared
////////////////////////////////////
void doingSomething0()
{
doingSomething(0);
}
void doingSomething1()
{
doingSomething(1);
}
void doingSomething2()
{
doingSomething(2);
}
void doingSomething3()
{
doingSomething(3);
}
void doingSomething4()
{
doingSomething(4);
}
void doingSomething5()
{
doingSomething(5);
}
void doingSomething6()
{
doingSomething(6);
}
void doingSomething7()
{
doingSomething(7);
}
void doingSomething8()
{
doingSomething(8);
}
void doingSomething9()
{
doingSomething(9);
}
void doingSomething10()
{
doingSomething(10);
}
void doingSomething11()
{
doingSomething(11);
}
void doingSomething12()
{
doingSomething(12);
}
void doingSomething13()
{
doingSomething(13);
}
void doingSomething14()
{
doingSomething(14);
}
void doingSomething15()
{
doingSomething(15);
}
#if USE_COMPLEX_STRUCT
ISRTimerData curISRTimerData[NUMBER_ISR_TIMERS] =
{
//irqCallbackFunc, TimerInterval, deltaMillis, previousMillis
{ doingSomething0, 5000L, 0, 0 },
{ doingSomething1, 10000L, 0, 0 },
{ doingSomething2, 15000L, 0, 0 },
{ doingSomething3, 20000L, 0, 0 },
{ doingSomething4, 25000L, 0, 0 },
{ doingSomething5, 30000L, 0, 0 },
{ doingSomething6, 35000L, 0, 0 },
{ doingSomething7, 40000L, 0, 0 },
{ doingSomething8, 45000L, 0, 0 },
{ doingSomething9, 50000L, 0, 0 },
{ doingSomething10, 55000L, 0, 0 },
{ doingSomething11, 60000L, 0, 0 },
{ doingSomething12, 65000L, 0, 0 },
{ doingSomething13, 70000L, 0, 0 },
{ doingSomething14, 75000L, 0, 0 },
{ doingSomething15, 80000L, 0, 0 }
};
void doingSomething(int index)
{
unsigned long currentMillis = millis();
curISRTimerData[index].deltaMillis = currentMillis - curISRTimerData[index].previousMillis;
curISRTimerData[index].previousMillis = currentMillis;
}
#else
irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] =
{
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
doingSomething8, doingSomething9, doingSomething10, doingSomething11,
doingSomething12, doingSomething13, doingSomething14, doingSomething15
};
#endif
///////////////////////////////////////////
#define SIMPLE_TIMER_MS 2000L
// Init SimpleTimer
SimpleTimer simpleTimer;
// Here is software Timer, you can do somewhat fancy stuffs without many issues.
// But always avoid
// 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead
// 2. Very long "do", "while", "for" loops without predetermined exit time.
void simpleTimerDoingSomething2s()
{
static unsigned long previousMillis = startMillis;
unsigned long currMillis = millis();
Serial.print(F("SimpleTimer : "));
Serial.print(SIMPLE_TIMER_MS / 1000);
Serial.print(F(", ms : "));
Serial.print(currMillis);
Serial.print(F(", Dms : "));
Serial.println(currMillis - previousMillis);
for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++)
{
#if USE_COMPLEX_STRUCT
Serial.print(F("Timer : "));
Serial.print(i);
Serial.print(F(", programmed : "));
Serial.print(curISRTimerData[i].TimerInterval);
Serial.print(F(", actual : "));
Serial.println(curISRTimerData[i].deltaMillis);
#else
Serial.print(F("Timer : "));
Serial.print(i);
Serial.print(F(", programmed : "));
Serial.print(TimerInterval[i]);
Serial.print(F(", actual : "));
Serial.println(deltaMillis[i]);
#endif
}
previousMillis = currMillis;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(100);
Serial.print(F("\nStarting ISR_16_Timers_Array_Complex on "));
Serial.println(BOARD_NAME);
Serial.println(NRF52_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = "));
Serial.print(F_CPU / 1000000);
Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler))
{
startMillis = millis();
Serial.print(F("Starting ITimer OK, millis() = "));
Serial.println(startMillis);
}
else
Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each ISR_Timer
for (uint16_t i = 0; i < NUMBER_ISR_TIMERS; i++)
{
#if USE_COMPLEX_STRUCT
curISRTimerData[i].previousMillis = startMillis;
ISR_Timer.setInterval(curISRTimerData[i].TimerInterval, curISRTimerData[i].irqCallbackFunc);
#else
previousMillis[i] = startMillis;
ISR_Timer.setInterval(TimerInterval[i], irqCallbackFunc[i]);
#endif
}
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s);
}
#define BLOCKING_TIME_MS 10000L
void loop()
{
// This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer
// You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer
// The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS)
// While that of ISR_Timer is still prefect.
delay(BLOCKING_TIME_MS);
// You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary
// You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer.
simpleTimer.run();
}



Debug Terminal Output Samples

1. ISR_Timer_Complex_Ethernet on Adafruit NRF52840_FEATHER using W5500 Ethernet

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

2. TimerInterruptTest on Adafruit NRF52840_FEATHER

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

3. Argument_None on Adafruit NRF52840_FEATHER

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


4. ISR_16_Timers_Array_Complex on Adafruit NRF52840_FEATHER

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

5. SwitchDebounce on Adafruit NRF52840_FEATHER

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

6. Change_Interval on Adafruit NRF52840_FEATHER

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

7. FakeAnalogWrite on Adafruit NRF52840_FEATHER

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

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

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: NRF52_TimerInterrupt issues


TO DO

  1. Search for bug and improvement.

DONE

  1. Basic hardware timers for NRF52832 and NRF52840
  2. More hardware-initiated software-enabled timers
  3. Longer time interval
  4. Similar features for remaining Arduino boards such as AVR, ESP32, ESP8266, STM32, SAM-DUE, SAMD21/SAMD51, mbed-nRF52, Teensy, etc.
  5. Add Table of Contents
  6. Fix multiple-definitions linker error
  7. Optimize library code by using reference-passing instead of value-passing
  8. Add support to Sparkfun Pro nRF52840 Mini
  9. Add support to Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc. using Seeeduino nRF%2 core
  10. Add astyle using allman style. Restyle the library


Contributions and Thanks

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

  1. Thanks to good work of Miguel Wisintainer for working with, developing, debugging and testing.
tcpipchip
Miguel Wisintainer


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 under MIT

Copyright

Copyright 2020- Khoi Hoang

About

This library enables you to use Interrupt from Hardware Timers on an nRF52-based board. These nRF52 Hardware Timers, using Interrupt, still work even if other functions are blocking. Moreover, they are much more precise than other software timers using millis() or micros(). Now supports `Sparkfun Pro nRF52840 Mini`

License:MIT License


Languages

Language:C++ 79.9%Language:C 20.0%Language:Shell 0.0%