Getting started¶
TeensyTimerTool provides a common interface to most timers of the Teensy boards: the hardware timers (GPT, TMR, PIT and RTC on Teensy 4, FTM on Teensy 3) and up to 20 software timers (TCK), which are available on every Teensy. Each timer can be used as periodic or as one-shot timer. A sketch can either request a specific timer or take the next free timer from the timer pool (1).
-
Timer pool
The library keeps a list of the timers it may use freely, the timer pool. A timer declared without naming a specific hardware timer takes the next free one from this list. The content of the pool can be changed in the configuration.
In addition to the IntervalTimer of the Teensy core, TeensyTimerTool offers
- a large number of timers (more than 40 on Teensy 4),
- one-shot timers, which invoke a function once after a delay,
- periods and delays as numbers (microseconds) or with units:
250ms,20us,1.5s, - callbacks with state: lambdas, member functions, functors.
The overhead is low: invoking a callback through TeensyTimerTool takes about as long as programming the timer registers directly (comparison).
Version 1.5
Sketches written for version 1.4 compile without changes in most cases; the exceptions are listed in Upgrading from 1.4. All changes of this and earlier versions are listed in the Changelog.
Installation¶
Open the Library Manager (books icon in the left side bar, or Tools → Manage Libraries…), search for TeensyTimerTool and click Install.
Download the latest release as zip file and install it with Sketch → Include Library → Add .ZIP Library….
A first periodic timer¶
The following sketch takes a timer from the pool and toggles the built-in LED every 250 ms:
/********************************************************
* Basic usage of the timer
*
* Generates a timer from the timer pool and
* starts it with a period of 250ms.
* The timer callback simply toggles the built in LED
*
********************************************************/
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
void callback()
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
}
PeriodicTimer t1; // takes the next free timer from the timer pool (see defaultConfig.h)
void setup()
{
pinMode(LED_BUILTIN,OUTPUT);
t1.begin(callback, 250'000); // 250ms
}
void loop()
{
}
The sketch declares a PeriodicTimer and calls begin() in setup() with the function to be invoked
(the callback) and the period in microseconds. The timer then runs in the background; loop() remains
available for the application. The line using namespace TeensyTimerTool; (1) makes the classes of the
library available under their short names.
-
Namespace
All classes of the library are declared in the namespace
TeensyTimerTool, their full names areTeensyTimerTool::PeriodicTimerand so on. This avoids name clashes with other libraries. The lineusing namespace TeensyTimerTool;makes the classes available under their short names. More about namespaces
Tip
The period can also be written as 250ms. Units, starting and stopping, changing the period and
further functions are described in Usage.
A first one-shot timer¶
A OneShotTimer invokes its callback once, after a delay. The delay is passed to trigger(), which
starts the timer and returns immediately. Unlike delay(), the sketch is not paused: the program
continues, and the callback is invoked in the background when the delay has expired.
In the following sketch, setup() switches the LED on and triggers the timer, which switches the LED
off again after 3 s. In the meantime, loop() prints the time and the LED state to the serial monitor:
/********************************************************
* Basic usage of a one-shot timer
*
* setup() switches the LED on and triggers a one-shot timer
* which switches it off again after 3 s. trigger() returns
* immediately: loop() keeps running and prints the time and
* the LED state while the timer runs in the background.
*
* The sketch uses a TCK (software) timer, which supports long
* delays on every Teensy.
********************************************************/
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
OneShotTimer ledOff(TCK);
void switchOff()
{
digitalWriteFast(LED_BUILTIN, LOW);
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
while (!Serial && millis() < 3000) {} // wait (max. 3 s) for the serial monitor
ledOff.begin(switchOff);
digitalWriteFast(LED_BUILTIN, HIGH);
ledOff.trigger(3'000'000); // switch the LED off in 3 s (3'000'000 µs)
}
void loop()
{
Serial.printf("%5lu ms: LED %s\n", millis(), digitalReadFast(LED_BUILTIN) ? "on" : "off");
delay(250);
}
Selecting a timer¶
PeriodicTimer t1; takes the next free timer from the timer pool. On Teensy 4, the pool contains the
hardware timers GPT1, GPT2 and TMR1 … TMR4, followed by the software timers (TCK); on Teensy 3, it
starts with the FTM timers. This is sufficient for most applications. The pool can be changed in the
configuration.
A specific timer, e.g. for very long periods or a particularly fine resolution, is selected by passing it to the constructor:
PeriodicTimer t1(TCK); // software timer, available on every Teensy
PeriodicTimer t2(PIT); // PIT channel (Teensy 4)
OneShotTimer t3(GPT1); // GPT1 timer (Teensy 4)
The chapter Choosing a timer compares the properties of all timers.
Further reading¶
- Usage: starting, stopping and changing timers.
- Callbacks: lambdas, member functions, timers in classes.
- Error handling: evaluating the codes returned by
begin()and other functions. - Examples: the example sketches of the library.
- Changelog: the changes of all versions.
Questions, bug reports and feature requests are welcome in the PJRC forum or in the issue tracker.