- Important Note for Arduino IDE
- Why do we need this Dx_TimerInterrupt library
- Changelog
- Prerequisites
- Installation
- HOWTO Fix
Multiple Definitions
Linker Error - More useful Information
- Usage
- Examples
- Example ISR_16_Timers_Array_Complex
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
With some Arduino IDE versions, such as v1.8.19, upload directly via USB to some boards, such as Curiosity_AVR128DA48
or Curiosity_AVR128DB48
can't be done without unknown-to-me fix. We'll get the following error when uploading
avrdude: Version 6.3-20201216
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch
System wide configuration file is "/home/kh/.arduino15/packages/DxCore/hardware/megaavr/1.4.10/avrdude.conf"
User configuration file is "/home/kh/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : usb
Using Programmer : curiosity_updi
avrdude: usbdev_open(): Found nEDBG CMSIS-DAP, serno: MCHP3280041800002682
avrdude: usbdev_open(): WARNING: failed to set configuration 1: Device or resource busy
avrdude: Found CMSIS-DAP compliant device, using EDBG protocol
avrdude: usbdev_send(): wrote -5 out of 912 bytes, err = Input/output error
avrdude: jtag3_edbg_prepare(): failed to send command to serial port
avrdude done. Thank you.
the selected serial port
does not exist or your board is not connected
We can use drag-and-drop method to drag-and-drop
the compiled hex file to CURIOSITY
virtual drive.
If success
, The LED blinks slowly for 2 sec. The LED will blinks rapidly for 2 sec if failure
For example, to run Change_Interval example, use Arduino IDE to compile, and get the Change_Interval.ino.hex
file. For Ubuntu Linux, the file is stored in directory /tmp/arduino_build_xxxxxx
After drag-and-drop the Change_Interval.ino.hex
into CURIOSITY
virtual drive, the code will run immediately if successfully loaded (LED blinks slowly)
Why do we need this Dx_TimerInterrupt library
This library enables you to use Interrupt from Hardware Timers on Arduino AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore
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_16_Timers_Array_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.
Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many (up to 16)
timers to use.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking 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:
-
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.
- AVRDA-based boards (AVR128DA, AVR64DA, AVR32DA, etc.) using DxCore
- AVRDB-based boards (AVR128DB, AVR64DB, AVR32DB, etc.) using DxCore
-
AVRDD-based boards (AVR64DD) using DxCore
-
tinyAVR boards using megaTinyCore
Arduino IDE 1.8.19+
for Arduino.SpenceKonde DxCore core 1.4.10+
for Arduino AVRDx boards. . Follow DxCore Installation.- To use with certain example
SimpleTimer library
for ISR_Timers_Array_Simple and ISR_16_Timers_Array_Complex examples.
The best and easiest way is to use Arduino Library Manager
. Search for Dx_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 Dx_TimerInterrupt page.
- Download the latest release
Dx_TimerInterrupt-main.zip
. - Extract the zip file to
Dx_TimerInterrupt-main
directory - Copy whole
Dx_TimerInterrupt-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install Dx_TimerInterrupt library by using Library Manager. Search for Dx_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
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 these .hpp
files
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "Dx_TimerInterrupt.hpp" //https://github.com/khoih-prog/Dx_TimerInterrupt
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "Dx_ISR_Timer.hpp" //https://github.com/khoih-prog/Dx_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 "Dx_TimerInterrupt.h" //https://github.com/khoih-prog/Dx_TimerInterrupt
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Dx_ISR_Timer.h" //https://github.com/khoih-prog/Dx_TimerInterrupt
Check the new multiFileProject example for a HOWTO
demo.
- Arduino 101: Timers and Interrupts
- Getting Started with Timer/Counter Type B (TCB)
- DXCore README.md
- AVR128DA48-Curiosity-Nano-Hardware-User Guide
- AVR128DB48-Curiosity-Nano-Hardware-User Guide
TCB0-TCB4 are 16-bit timers
The AVRDx boards with 14, 20, 28 or 32 pins, such as AVRDx28
, will have only 3 TCB timers, (TCB0-TCB2)
The AVRDx with 48 pins, such as Curiosity Nano AVRDA48
, Curiosity Nano AVRDB48
, will have 4 TCB timers, (TCB0-TCB3)
The AVRDx with 64 pins, such as AVRDA64
, AVRDB64
, will have 5 TCB timers, (TCB0-TCB4)
The number of TCB timers will be automatically configured by the library.
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
// Select USING_FULL_CLOCK == true for 24/16MHz to Timer TCBx => shorter timer, but better accuracy
// Select USING_HALF_CLOCK == true for 12/ 8MHz to Timer TCBx => shorter timer, but better accuracy
// Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
#define USING_FULL_CLOCK true
#define USING_HALF_CLOCK false
#define USING_250KHZ false // Not supported now
#define USE_TIMER_0 false
#define USE_TIMER_1 true
#define USE_TIMER_2 false // Normally used by millis(). Don't use
#define USE_TIMER_3 false
#define USE_TIMER_4 false
#if USE_TIMER_0
#define CurrentTimer ITimer0
#elif USE_TIMER_1
#define CurrentTimer ITimer1
#elif USE_TIMER_2
#define CurrentTimer ITimer2
#elif USE_TIMER_3
#define CurrentTimer ITimer3
#elif USE_TIMER_4
#define CurrentTimer ITimer4
#else
#error You must select one Timer
#endif
// Init timer CurrentTimer
CurrentTimer.init();
Use one of these functions with interval in unsigned long milliseconds
// interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
template<typename TArg> bool setInterval(unsigned long interval, void (*callback)(TArg), TArg params, unsigned long duration = 0);
// interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool setInterval(unsigned long interval, timer_callback callback, unsigned long duration = 0);
// Interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
template<typename TArg> bool attachInterruptInterval(unsigned long interval, void (*callback)(TArg), TArg params, unsigned long duration = 0);
// Interval (in ms) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool attachInterruptInterval(unsigned long interval, timer_callback callback, unsigned long duration = 0)
as follows
void TimerHandler1()
{
// Doing something here inside ISR
}
#define TIMER1_INTERVAL_MS 50L
void setup()
{
....
// Interval in unsigned long millisecs
// Timer TCB2 is used for micros(), millis(), delay(), etc and can't be used
ITimer1.init();
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1))
{
SerialDebug.print(F("Starting ITimer OK, millis() = ")); SerialDebug.println(millis());
}
else
SerialDebug.println(F("Can't set ITimer. Select another freq. or timer"));
}
Use one of these functions with frequency in float Hz
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool setFrequency(float frequency, timer_callback_p callback, /* void* */ uint32_t params, unsigned long duration = 0);
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool setFrequency(float frequency, timer_callback callback, unsigned long duration = 0);
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
template<typename TArg> bool attachInterrupt(float frequency, void (*callback)(TArg), TArg params, unsigned long duration = 0);
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
bool attachInterrupt(float frequency, timer_callback callback, unsigned long duration = 0);
as follows
void TimerHandler1()
{
// Doing something here inside ISR
}
#define TIMER1_FREQ_HZ 5555.555
void setup()
{
....
// Frequency in float Hz
if (ITimer1.attachInterrupt(TIMER1_FREQ_HZ, TimerHandler1))
{
SerialDebug.print(F("Starting ITimer OK, millis() = ")); SerialDebug.println(millis());
}
else
SerialDebug.println("Can't set ITimer. 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
// Select USING_FULL_CLOCK == true for 24/16MHz to Timer TCBx => shorter timer, but better accuracy
// Select USING_HALF_CLOCK == true for 12/ 8MHz to Timer TCBx => shorter timer, but better accuracy
// Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
#define USING_FULL_CLOCK true
#define USING_HALF_CLOCK false
#define USING_250KHZ false // Not supported now
#define USE_TIMER_0 false
#define USE_TIMER_1 true
#define USE_TIMER_2 false // Normally used by millis(). Don't use
#define USE_TIMER_3 false
#define USE_TIMER_4 false
#if USE_TIMER_0
#define CurrentTimer ITimer0
#elif USE_TIMER_1
#define CurrentTimer ITimer1
#elif USE_TIMER_2
#define CurrentTimer ITimer2
#elif USE_TIMER_3
#define CurrentTimer ITimer3
#elif USE_TIMER_4
#define CurrentTimer ITimer4
#else
#error You must select one Timer
#endif
// Init ISR_Timer
// Each ISR_Timer can service 16 different ISR-based timers
ISR_Timer ISR_Timer1;
void TimerHandler()
{
ISR_Timer1.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 AVR, avoid doing something fancy in ISR, for example complex SerialDebug.print with String() argument
// The pure simple SerialDebug.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 every 2 seconds
}
void doingSomething5s()
{
// Doing something here inside ISR every 5 seconds
}
void doingSomething11s()
{
// Doing something here inside ISR every 11 seconds
}
void doingSomething101s()
{
// Doing something here inside ISR every 101 seconds
}
void setup()
{
....
// Timer TCB2 is used for micros(), millis(), delay(), etc and can't be used
CurrentTimer.init();
// Interval in millisecs
if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler))
{
lastMillis = millis();
SerialDebug.print(F("Starting ITimer OK, millis() = ")); SerialDebug.println(millis());
}
else
SerialDebug.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_Timer1.setInterval(TIMER_INTERVAL_2S, doingSomething2s);
ISR_Timer1.setInterval(TIMER_INTERVAL_5S, doingSomething5s);
ISR_Timer1.setInterval(TIMER_INTERVAL_11S, doingSomething11s);
ISR_Timer1.setInterval(TIMER_INTERVAL_101S, doingSomething101s);
}
- Argument_Complex
- Argument_None
- Argument_Simple
- Change_Interval.
- FakeAnalogWrite.
- ISR_16_Timers_Array_Complex.
- ISR_RPM_Measure
- Change_Interval_HF
- ISR_Timers_Array_Simple.
- RPM_Measure
- SwitchDebounce
- TimerDuration
- TimerInterruptTest
- multiFileProject
Example ISR_16_Timers_Array_Complex
The following is the sample terminal output when running example ISR_16_Timers_Array_Complex on Arduino AVR128DA 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 more than 10.000s in loop().
Starting ISR_16_Timers_Array_Complex on AVR128DA
Dx_TimerInterrupt v1.1.3
CPU Frequency = 24 MHz
TCB Clock Frequency = Full clock (24/16MHz, etc) for highest accuracy
Starting ITimer OK, millis() = 13
SimpleTimer : 2, ms : 10013, Dms : 10013
Timer : 0, programmed : 5000, actual : 5017
Timer : 1, programmed : 10000, actual : 10017
Timer : 2, programmed : 15000, actual : 0
Timer : 3, programmed : 20000, actual : 0
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 20072, Dms : 10059
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15017
Timer : 3, programmed : 20000, actual : 20017
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
...
SimpleTimer : 2, ms : 70376, Dms : 10062
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 24996
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40013
Timer : 8, programmed : 45000, actual : 45013
Timer : 9, programmed : 50000, actual : 50013
Timer : 10, programmed : 55000, actual : 55013
Timer : 11, programmed : 60000, actual : 60013
Timer : 12, programmed : 65000, actual : 65013
Timer : 13, programmed : 70000, actual : 70013
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 80439, Dms : 10063
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40000
Timer : 8, programmed : 45000, actual : 45013
Timer : 9, programmed : 50000, actual : 50013
Timer : 10, programmed : 55000, actual : 55013
Timer : 11, programmed : 60000, actual : 60013
Timer : 12, programmed : 65000, actual : 65013
Timer : 13, programmed : 70000, actual : 70013
Timer : 14, programmed : 75000, actual : 75013
Timer : 15, programmed : 80000, actual : 80013
Starting ISR_16_Timers_Array_Complex on AVR128DA
Dx_TimerInterrupt v1.1.3
CPU Frequency = 24 MHz
TCB Clock Frequency = Full clock (24/16MHz, etc) for highest accuracy
Starting ITimer OK, millis() = 13
SimpleTimer : 2, ms : 10013, Dms : 10013
Timer : 0, programmed : 5000, actual : 5017
Timer : 1, programmed : 10000, actual : 10017
Timer : 2, programmed : 15000, actual : 0
Timer : 3, programmed : 20000, actual : 0
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 20072, Dms : 10059
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15017
Timer : 3, programmed : 20000, actual : 20017
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
...
SimpleTimer : 2, ms : 70376, Dms : 10062
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 24996
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40013
Timer : 8, programmed : 45000, actual : 45013
Timer : 9, programmed : 50000, actual : 50013
Timer : 10, programmed : 55000, actual : 55013
Timer : 11, programmed : 60000, actual : 60013
Timer : 12, programmed : 65000, actual : 65013
Timer : 13, programmed : 70000, actual : 70013
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 80439, Dms : 10063
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15000
Timer : 3, programmed : 20000, actual : 20000
Timer : 4, programmed : 25000, actual : 25000
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35000
Timer : 7, programmed : 40000, actual : 40000
Timer : 8, programmed : 45000, actual : 45013
Timer : 9, programmed : 50000, actual : 50013
Timer : 10, programmed : 55000, actual : 55013
Timer : 11, programmed : 60000, actual : 60013
Timer : 12, programmed : 65000, actual : 65013
Timer : 13, programmed : 70000, actual : 70013
Timer : 14, programmed : 75000, actual : 75013
Timer : 15, programmed : 80000, actual : 80013
Starting ISR_16_Timers_Array_Complex on AVR128DA
Dx_TimerInterrupt v1.1.3
CPU Frequency = 24 MHz
TCB Clock Frequency = Half clock (12/8MHz, etc.) for high accuracy
Starting ITimer OK, millis() = 12
SimpleTimer : 2, ms : 10013, Dms : 10013
Timer : 0, programmed : 5000, actual : 5015
Timer : 1, programmed : 10000, actual : 10015
Timer : 2, programmed : 15000, actual : 0
Timer : 3, programmed : 20000, actual : 0
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
SimpleTimer : 2, ms : 20072, Dms : 10059
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15015
Timer : 3, programmed : 20000, actual : 20015
Timer : 4, programmed : 25000, actual : 0
Timer : 5, programmed : 30000, actual : 0
Timer : 6, programmed : 35000, actual : 0
Timer : 7, programmed : 40000, actual : 0
Timer : 8, programmed : 45000, actual : 0
Timer : 9, programmed : 50000, actual : 0
Timer : 10, programmed : 55000, actual : 0
Timer : 11, programmed : 60000, actual : 0
Timer : 12, programmed : 65000, actual : 0
Timer : 13, programmed : 70000, actual : 0
Timer : 14, programmed : 75000, actual : 0
Timer : 15, programmed : 80000, actual : 0
...
SimpleTimer : 2, ms : 80439, Dms : 10063
Timer : 0, programmed : 5000, actual : 5000
Timer : 1, programmed : 10000, actual : 10000
Timer : 2, programmed : 15000, actual : 15001
Timer : 3, programmed : 20000, actual : 20001
Timer : 4, programmed : 25000, actual : 25001
Timer : 5, programmed : 30000, actual : 30000
Timer : 6, programmed : 35000, actual : 35001
Timer : 7, programmed : 40000, actual : 40001
Timer : 8, programmed : 45000, actual : 45015
Timer : 9, programmed : 50000, actual : 50015
Timer : 10, programmed : 55000, actual : 55015
Timer : 11, programmed : 60000, actual : 60015
Timer : 12, programmed : 65000, actual : 65016
Timer : 13, programmed : 70000, actual : 70016
Timer : 14, programmed : 75000, actual : 75016
Timer : 15, programmed : 80000, actual : 80016
The following is the sample terminal output when running example Change_Interval_HF on Arduino AVR128DA to demonstrate how to change High Frequency Timer Interval on-the-fly
Starting Change_Interval_HF on AVR128DA
Dx_TimerInterrupt v1.1.3
CPU Frequency = 24 MHz
TCB Clock Frequency = Full clock (24/16MHz, etc) for highest accuracy
Starting ITimer OK, millis() = 12
Frequency, Timer = 50
Time = 1001, Timer1Count = 49
Time = 2002, Timer1Count = 99
Time = 3003, Timer1Count = 149
Time = 4004, Timer1Count = 199
Time = 5005, Timer1Count = 249
Changing Frequency, Timer = 25
Time = 6006, Timer1Count = 274
Time = 7007, Timer1Count = 299
Time = 8008, Timer1Count = 324
Time = 9009, Timer1Count = 349
Time = 10010, Timer1Count = 374
Changing Frequency, Timer = 16
Time = 11011, Timer1Count = 390
Time = 12012, Timer1Count = 406
Time = 13013, Timer1Count = 422
Time = 14014, Timer1Count = 438
Time = 15015, Timer1Count = 454
Changing Frequency, Timer = 12
Time = 16016, Timer1Count = 466
Time = 17017, Timer1Count = 478
Time = 18018, Timer1Count = 490
Time = 19019, Timer1Count = 502
Time = 20020, Timer1Count = 514
Changing Frequency, Timer = 10
Time = 21021, Timer1Count = 524
Time = 22022, Timer1Count = 534
Time = 23023, Timer1Count = 544
Time = 24024, Timer1Count = 554
Time = 25025, Timer1Count = 564
Changing Frequency, Timer = 8
Time = 26026, Timer1Count = 572
Time = 27027, Timer1Count = 580
Time = 28028, Timer1Count = 588
Time = 29029, Timer1Count = 596
Time = 30030, Timer1Count = 604
Changing Frequency, Timer = 7
Time = 31031, Timer1Count = 611
Time = 32032, Timer1Count = 618
Time = 33033, Timer1Count = 625
Time = 34034, Timer1Count = 632
Time = 35035, Timer1Count = 639
Changing Frequency, Timer = 6
Time = 36036, Timer1Count = 645
Time = 37037, Timer1Count = 651
Time = 38038, Timer1Count = 657
Time = 39039, Timer1Count = 663
Time = 40040, Timer1Count = 669
Debug is enabled by default on Serial1
for Curiosity Nano AVRDA
and Serial3
for Curiosity Nano AVRDB
.
You can also change the debugging level from 0 to 4
// These define's must be placed at the beginning before #include "Dx_TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 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: Dx_TimerInterrupt issues
- Search for bug and improvement
- Add support to AVRDD-based boards (AVR64DD) using DxCore
- Add support to tinyAVRDD-based boards using megaTinyCore
- Add support to 250KHz Timer Frequency
- Longer Interval for timers
- Reduce code size if use less timers. Eliminate compiler warnings
- Now supporting complex object pointer-type argument
- 16 hardware-initiated software-enabled timers while using only 1 hardware timer
- Add support to AVRDx-based boards (AVR128Dx, AVR64Dx, AVR32Dx, etc.) using DxCore
- Selectable TCB Clock FULL, HALF depending on necessary accuracy
- Fix
multiple-definitions
linker error - Optimize library code by using
reference-passing
instead ofvalue-passing
- Improve and customize examples for
Curiosity Nano AVRDA/AVRDB
boards to use on-board LED and SW - Add notes
howto upload by drag-and-drop
toCURIOSITY
virtual drive - Using Serial3 for debugging with Curiosity Nano AVRDB, and Serial1 for debugging with Curiosity Nano AVRDA
- Fix bug giving error when using TCB0 (
USE_TIMER_0 == true
) - Fix bug causing system crash when using
_TIMERINTERRUPT_LOGLEVEL_ > 0
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library. Especially to these people who have directly or indirectly contributed to this Dx_TimerInterrupt library
- Thanks to good work of Spence Konde (aka Dr. Azzy) for the DxCore and megaTinyCore
- Thanks to LaurentR59 to request the enhancement Support for DX CORE CPU and MightyCORE CPU possible? #8 leading to this new library
⭐️⭐️ Spence Konde |
LaurentR59 |
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 2022- Khoi Hoang