Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Up to 8 channel delay timer

  1. #1
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    141

    Up to 8 channel delay timer

    In case somebody needs an interrupt driven delay library (e.g. for generating stepper pulses with long pulse width):

    https://github.com/luni64/TeensyDelay

    The library uses one of the FTM or TPM timers (selectable) and can provide up to 8 independent delay channels. Not a big thing, but might come in handy for some project.
    Basic usage:

    Code:
    #include <TeensyDelay.h>
    
    void callback()
    {
      digitalWriteFast(LED_BUILTIN,LOW);
    }
    
    void setup() 
    {
      pinMode(LED_BUILTIN,OUTPUT);
    
      TeensyDelay::begin();
      TeensyDelay::addDelayChannel(callback); // setup a delay channel and attach the callback function to it
    }
    
    void loop() 
    {
        digitalWriteFast(LED_BUILTIN,HIGH);  // switch on LED
        TeensyDelay::trigger(15);            // the callback function will switch it off 15Ás later
    
        delay(500);                          // repeat every 500ms
    }
    The library is compatible to T3.0, T3.1/2, T3.5, T3.6. Im working on compatibiltiy to T-LC

    Have fun.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    15,720
    I've added a link to this thread from the IntervalTimer page. Hopefully it'll help people find this library if they need more interrupt-based timers.

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    141
    Great, thanks a lot for linking the library

  4. #4
    Senior Member
    Join Date
    Apr 2015
    Posts
    120
    Life==SAVED! Thank you! I think you might need to change your addChannel to addDelayChannel in your examples Readme.md file, btw.

    Could someone tell me what the functional difference is between Paul's timers and these? Is there a difference? I noticed that the teensy 3.2 has only 2 FTM timers, so could you use the 4 Interval timers and the 2 FTM for a total of 6?

  5. #5
    Looking at the linked code will show - but perhaps a single fast timer is used, and then each added channel is tracked for expiration in some fashion on each call to that timer's interrupt code.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    141
    Pauls timers (if you refer to the IntervalTimers) use the PIT module of the chip. They are 32bit counters and basically generate an interrupt whenever the counter matches a given compare value.

    TeensyDelay uses a FTM timer. The FTM timers are flexible (hence the name) 16bit timers. They can be used in various modes; I'm using them in the simple TPM mode. In TPM mode each of the FTM timers consists of one counter and up to 8 channels (i.e. compare values). Each of the compare values can be used to generate an interrupt whenever its value matches the counter value. If you set the compare values carefully you get up to 8 'independent' timers per FTM module

    There is a table in config.h showing which FTM modules are available for which board, how many channels the module provides and which module is used by default.

    T3.2 by default uses FTM0 which provides 8 channels. So, together with the 4 IntervalTimers you have up to 12 usable timers. Please note that the FTM timers are only 16bit. TeensyDelay chooses the prescaler such that 1 timer tick is roundabout 1Ás. Therefore the maximum possible delay is about 65ms. If you need periodic interrupts you can reload the delayChannel in its callback function. (See the chapter "A Poor Man's Periodic Timer" in the documentation)

    Hope that helps

    I think you might need to change your addChannel to addDelayChannel in your examples Readme.md file
    Thanks, fixed it.
    Last edited by luni; 04-26-2017 at 04:17 PM.

  7. #7
    Senior Member
    Join Date
    Apr 2015
    Posts
    120
    Theoretically, couldn't you also enable the FTM1 and FTM2 after setting a different default timer and renaming the TeensyDelay to another name twice, to have a total of 16 timers for teensy 3.2? It might be nice to have an instantiation init call on the class that allows one to choose the timer, if possible.

    Either way, thanks so much for writing this! I am using this to create many independent changing (different) PWM audible tones. I haven't tested the TeensyDelay library for this yet, but it seems like it would work great!

  8. #8
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    141
    Your idea about duplicating/renaming the TeensyDelay functions to allocate another FTM might end up in a mess. If you need loads of timers for a specific project on a specific board it would probably be quicker to implement the functionality from scratch. Although the documentation for the FTM counters looks quite deterrent, they default to TPM mode which is easy to use (have a look at the TPM section of the manual). You find all the code required to setup and trigger the timers in TeensyDelay.h and TeensyDelay.cpp.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •