You can use the TeensyTimerTool for this. It provides a generic interface to the hardware timers (TMR, GPT, PIT) and 20 software timers. Since the coupling of the timer peripherals to the ARM core is rather inefficient, the software timers might be a better fit for sub micro second interrupts. If you prefer the hardware timers I suggest to use a GPT timer (also supported by the lib). You can configure it to use a 150MHz clock instead of the standard 24MHz clock. So something like 2MHz interrupt rate should be feasible.

Documentation can be found here: https://github.com/luni64/TeensyTimerTool/wiki

Answer from luni64 on Stack Exchange
🌐
PJRC
pjrc.com › teensy › td_libs_TimerOne.html
TimerOne & TimerThree Arduino Libraries
TimerOne and TimerThree let you use the built-in 16 bit timers.
Top answer
1 of 2
2

You can use the TeensyTimerTool for this. It provides a generic interface to the hardware timers (TMR, GPT, PIT) and 20 software timers. Since the coupling of the timer peripherals to the ARM core is rather inefficient, the software timers might be a better fit for sub micro second interrupts. If you prefer the hardware timers I suggest to use a GPT timer (also supported by the lib). You can configure it to use a 150MHz clock instead of the standard 24MHz clock. So something like 2MHz interrupt rate should be feasible.

Documentation can be found here: https://github.com/luni64/TeensyTimerTool/wiki

2 of 2
0

I had a similar problem with the Teensy 3.5. I found that using the "Periodic Interrupt Timer (PIT)" let me generate interrupt every 300 ns. I imagine that with a Teensy 4.0, it must be possible to generate an interrupt every 100 ns. Here is the code which works for me on my Teensy 3.5, with comments in french and some references in the comments. I test the signal on an oscilloscope.

// ex00305_Timer_pit0_isr_rapide.ino
/*

FONCTIONNE TRES BIEN

Le signal n'est pas très stable, mais acceptable.
J'imagine que d'autres routines d'interruption perturbe la précision, comme la routine millis()

L'instruction suivante améliore la stabilité. visiblement.
NVIC_SET_PRIORITY(IRQ_PIT_CH0, 0);

Le but est de tester l'utilisation du Timer1 .c.f
/home/bg/Arduino/Teensy/Teensy_3_5_manual_K64P144M120SF5RM.pdf
Page 1083, Chapter 41 : "Periodic Interrupt Timer (PIT)"

Pour la gestion des timers PIT, c.f.
====================================
https://github.com/loglow/PITimer/blob/master/PITimer.cpp

********************************************************************************** */

#define PINOUT1 34
#define LEDRED  24
#define LEDYELLOW  25
#define LEDGREEN  26

volatile byte bEtat = 0;

void setup() {
    //============
    pinMode(PINOUT1, OUTPUT);
    digitalWriteFast(PINOUT1, HIGH);

    pinMode(33, OUTPUT);
    pinMode(34, OUTPUT);
    pinMode(LEDRED, OUTPUT);  digitalWriteFast(LEDRED, LOW);
    pinMode(LEDYELLOW, OUTPUT);  digitalWriteFast(LEDYELLOW, LOW);
    pinMode(LEDGREEN, OUTPUT);  digitalWriteFast(LEDGREEN, LOW);

    //if (SIM_SCGC6_PIT == 0b100000000000000000000000) digitalWriteFast(LEDYELLOW, HIGH);
    SIM_SCGC6 |= SIM_SCGC6_PIT; // C.f. page 317.  PIT Clock Gate Control   SIM_SCGC6_PIT == 0b100000000000000000000000

    // C.f. : /home/bg/.arduino15/packages/teensy/hardware/avr/1.58.1/cores/teensy3/kinetis.h
    // #define NVIC_ENABLE_IRQ(n) (*((volatile uint32_t *)0xE000E100 + ((n) >> 5)) = (1 << ((n) & 31)))
    // enable IRQ Interrupt
    NVIC_SET_PRIORITY(IRQ_PIT_CH0, 0); // Haute priorité, pour ne pas être perturbé par d'autres interrupt. Amélioration visible !
    NVIC_ENABLE_IRQ(IRQ_PIT_CH0);
    PIT_TCTRL0 = 3; // 0b011;  // enable Timer 0 interrupts ; start Timer 0   IRQ_PIT_CH0 == 48  Dans #elif defined(__MK64FX512__)
    //if (IRQ_PIT_CH0 == 48) digitalWriteFast(LEDGREEN,  HIGH);

    // Efface le flag du timer, pour permettre d'autres interruptions, nécessaire au départ
    PIT_TFLG0 = 1;

    // L'ordre des instruction a de l'importance !
    //""""""""""""""""""""""""""""""""""""""""""""
    PIT_MCR = 0;  // turn on PIT
    //PIT_LDVAL0 = 600-1;  // == 10 micro secondes setup timer 0 for xxx cycles Fréqu = 60 MHz  dt = 16.666 ns
    //PIT_LDVAL0 = 120-1;  // == 2 micros second
    //PIT_LDVAL0 = 60-1;   // == 1 micros second
    //PIT_LDVAL0 = 30-1;   // == 500 ns
    //PIT_LDVAL0 = 24-1;   // == 400 ns
    //PIT_LDVAL0 = 21-1;   // == 350 ns
    //PIT_LDVAL0 = 20-1;   // == 333 ns
    //PIT_LDVAL0 = 18-1;   // == 300 ns
    PIT_LDVAL0 = 120-1;
} // setup

void loop() {
    //===========
} // loop

void pit0_isr() {  // C.f. mk20dx128.c
    //===============
    // J'espère pouvoir gérer un temps

    // Efface le flag du timer, pour permettre d'autres interruptions
    // Doit être mis en début de routine !!!
    PIT_TFLG0 = 1;

    if (bEtat) {
        digitalWriteFast(PINOUT1, HIGH);
        //digitalWriteFast(LEDRED,  HIGH);
        bEtat = 0;
    }
    else {
        bEtat = 1;
        digitalWriteFast(PINOUT1, LOW);
        //digitalWriteFast(LEDRED,  LOW);
    }

} // pit0_isr
Discussions

Teensy 4.0 timer1, one second loop question
I had a look at the TimerOne lib. As discussed by Kurt and Defragster, it does not implement the required dsb indeed. That should definitely be added in the lib after invoking the callback. I can do some tests and a PR later this week. ... #elif defined(__arm__) && defined(TEENSYDUINO) && ... More on forum.pjrc.com
🌐 forum.pjrc.com
13
August 18, 2020
Timer

include <TimerOne.h>

boolean toggle13 = 0;

void setup(void) { pinMode(13, OUTPUT); Timer1.initialize(1000000); Timer1.attachInterrupt(Blink); }

void Blink() { digitalWrite(13, toggle13); toggle13 = !toggle13; }

More on reddit.com
🌐 r/Teensy
1
1
February 15, 2021
Teensy_TimerInterrupt Library
Teensy_TimerInterrupt Library How To Install Using Arduino Library Manager This library enables you to use Interrupt from Hardware Timers on an Teensy-based board, such as Teensy 4.x, 3.x, LC, Teensy++ 2.0 or Teensy 2.0. As Hardware Timers are rare, and very precious assets of any board, this ... More on forum.arduino.cc
🌐 forum.arduino.cc
1
0
November 6, 2020
TimerOne Library timer pins on Teensy-LC
Hello everybody, I need an help with using TimerOne library and Teensy-LC board. What are the TimerOne PWM pins on the Teensy-LC board ? I have found a file on github... More on forum.pjrc.com
🌐 forum.pjrc.com
1
May 31, 2016
🌐
PJRC
pjrc.com › teensy › td_timing_IntervalTimer.html
Delay and Timing Functions
For Teensy 2.0 and Teensy++ 2.0, the TimerOne & TimerThree and FlexiTimer2 libraries provide similar capability.
🌐
Tavisharmstrong
tavisharmstrong.com › timer1
A Simple and Interactive Explanation of the Teensy's 16-bit timer (Timer1)
In the Teensy++, there are three timers. The second of these, Timer 1, is a 16-bit timer. It can be used for periodic interrupts, which lets you run a piece of code every so often. It can also be used for pulse-width modulation (PWM), which is a way to simulate analogue signals using digital ...
🌐
Teensy Forum
forum.pjrc.com › home › forums › main category › technical support & questions
Teensy 4.0 timer1, one second loop question | Teensy Forum
August 18, 2020 - I can do some tests and a PR later this week. ... #elif defined(__arm__) && defined(TEENSYDUINO) && defined(__IMXRT1062__) void TimerOne::isr(void) { FLEXPWM1_SM3STS = FLEXPWM_SMSTS_RF; Timer1.isrCallback(); } Anyway, the TimerOne lib is just a compatibility layer for the AVR processors.
🌐
GitHub
github.com › PaulStoffregen › TimerOne
GitHub - PaulStoffregen/TimerOne: TimerOne Library with optimization and expanded hardware support · GitHub
Paul Stoffregen's modified TimerOne. This version provides 2 main benefits: 1: Optimized inline functions - much faster for the most common usage 2: Support for more boards (including ATTiny85 except for the PWM functionality) http://www.pjrc.com/teensy/td_libs_TimerOne.html ·
Starred by 499 users
Forked by 213 users
Languages   C++ 83.4% | C 16.6%
🌐
Arduino
arduino.cc › reference › en › libraries › teensytimertool
TeensyTimerTool - Arduino Reference
November 25, 2020 - Generic Interface to Teensy Timers TeensyTimerTool is a library that provides a generic, easy to use interface to the hardware timers (FTM, GPT, QUAD, PIT) of the PJRC Teensy boards. In addition, it provides up to 20 highly efficient software timers based on the cycle counter or the RTC (32 ...
Find elsewhere
🌐
Reddit
reddit.com › r › Teensy › comments › lkaa8p › timer
r/Teensy - Timer
February 15, 2021 -

Hi i am setting a timer interrupt to blink an led every 1 seconds on my teensy 4.0, my led is only blinking every 0.1 seconds (estimated with the speed it is blinking) , is there something i am not doing right? I initialize 1000000 on my timer

🌐
GitHub
github.com › PaulStoffregen › TimerOne › blob › master › TimerOne.h
TimerOne/TimerOne.h at master · PaulStoffregen/TimerOne
* Modified again, June 2014 by Paul Stoffregen - support Teensy 3.x & even more AVR chips
Author   PaulStoffregen
🌐
Arduino Forum
forum.arduino.cc › projects › libraries
Teensy_TimerInterrupt Library - Libraries - Arduino Forum
November 6, 2020 - Teensy_TimerInterrupt Library How To Install Using Arduino Library Manager This library enables you to use Interrupt from Hardware Timers on an Teensy-based board, such as Teensy 4.x, 3.x, LC, Teensy++ 2.0 or Teensy 2.0. As Hardware Timers are ...
🌐
Arduino Libraries
arduinolibraries.info › libraries › teensy-timer-tool
TeensyTimerTool - Arduino Libraries
August 8, 2023 - TeensyTimerTool is a library that provides a generic, easy to use interface to the hardware timers (FTM, GPT, QUAD, PIT) of the PJRC Teensy boards. In addition, it provides up to 20 highly efficient software timers based on the cycle counter or the RTC (32 and 64bit) that use the same interface.
🌐
GitHub
github.com › loglow › IntervalTimer
GitHub - loglow/IntervalTimer: Timer library for Teensy 3.0
Timer library for Teensy 3.0. Contribute to loglow/IntervalTimer development by creating an account on GitHub.
Starred by 21 users
Forked by 8 users
Languages   C++ 90.1% | C++ 90.1%
🌐
Teensy Forum
forum.pjrc.com › home › forums › main category › technical support & questions
Teensy3.0 and compile time errors in TimerOne.h | Teensy Forum
March 21, 2013 - TimerOne and TimerThree do not work on Teensy 3.0. For periodic interrupts, use IntervalTimer.
🌐
GitHub
github.com › PaulStoffregen › TimerOne › issues › 17
Wrong Output Freq on Teensy 3.2 · Issue #17 · PaulStoffregen/TimerOne
August 25, 2016 - I believe there is a bug in this library, related to the binary search tree logic in setPeriod(). The logic never sets clockSelectBits to 2, where the previous implementation (commented code) can. Specific case which can cause incorrect ...
Author   PaulStoffregen
🌐
PJRC
pjrc.com › teensy › td_pulse.html
Pulse Width and Tone on Teensy with Arduino
For Teensy 2.0 and Teensy++ 2.0, the TimerOne & TimerThree libraries can be used to control the PWM frequency.
🌐
Adafruit
learn.adafruit.com › multi-tasking-the-arduino-part-2 › pin-change-interrupts
Libraries and Links | Multi-tasking the Arduino - Part 2 | Adafruit Learning System
December 1, 2014 - Paul Stoffregan's excellent TimerOne and TimerThree libraries take care of many of the low-level details of timer interrupt configuration. (Note that TimerThree is not applicable to the UNO. It can be used with the Leonardo, Mega and some of the Teensy boards)
🌐
Teensy Forum
forum.pjrc.com › home › forums › main category › project guidance
PWM Frequency (was: TimerOne on Teensy 3) | Teensy Forum
November 16, 2012 - This Teensy is great!! Thanks for all the hard work Paul! Is there currently a way to alter the PWM frequency without setting up a timer interrupt? I used TimerOne on my Uno, though I can't get it to work on the Teensy 3. I'm using PWM to drive an LED in a guitar pedal so I have to get it over...