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

Thread: Question about Timers and Possible Teensy 4.0 Capabilities

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    17

    Question about Timers and Possible Teensy 4.0 Capabilities

    I'm working on a project that I'm adapting to run on a Teensy 3.5 and someone just released an entirely different version of code (based on something much better) but they did it based upon using an Arduino Due. I am contemplating sticking with a Teensy and porting their code over since my additions will be doing a great deal of floating point calculations. However, they used DueTimer.cpp and are using Timers0 through Timer6. Is there a way to get these six timers on a Teensy 3.5 and if not, can it be done on a Teensy 4.0?

    Thanks

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,914
    Maybe you could use IntervalTimer?

    https://www.pjrc.com/teensy/td_timin...rvalTimer.html

    I'm not familiar with DueTimer, but a quick look seems all it does it call interrupt functions at regular intervals.

    If you need more, maybe look at the TimerOne and TimerThree libs, which are ported to Teensy. Those use different timers, so you could get 2 more regular timer calls this way. They are based on the FTM timers, so if you dig into the code you could probably make even 2 more for the other FTM timers (Teensy 3.5 has 4 of those timers).

    You might also consider whether using interrupt so much is really the best way to go about things. Many people have built projects this way, believing they've done everything needed with volatile variables and interrupt disable, only to find rare cases that crash are incredibly hard to debug. Interrupts are powerful, but they've a very sharp double-edge sword.

    Often less timing critical stuff can be done quite well with elapsedMillis or elapsedMicros.

    https://www.pjrc.com/teensy/td_timin...pedMillis.html

    Highly recommend you use those instead of interrupts, if you can.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    17
    Thanks for the quick response. I'm really new to all of this and winging most of it. The timers are used for many functions (this is for a CNC machine firmware based on GRBL).. from the guy that did the firmware update when I asked which were being used:

    "Timer1 for Spindle, Timer5 for PID, Timers 3 & 4 are used by GRBL stepper.c, and Arduino uses Timers 0, 2, 6 for PWMs, delay functions, etc."

    Timing is critical on many of those functions, so I don't think elapsedmillis/micros will work. I saw TimerOne & TimerThree (and read where you indicated .. I think.. one could create TimerTwo and TimerFour) but I think I need TimerFive and TimerSix as well? There probably is a way to work around it, but I'm not knowledgeable enough to do so.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,914
    Quote Originally Posted by madgrizzle View Post
    Timing is critical on many of those functions, so I don't think elapsedmillis/micros will work.
    Perhaps not for the steppers, but you really should use these for most of that other stuff. On top of the problems you'll avoid from interrupt context, using fewer interrupt for other stuff means the really critical things (timing of pulses to the steppers) you do with interrupts will be more efficient.

    Teensy 3.5 is significantly faster than Arduino Due, and massively faster if float variables are used, which gives you quite a lot of breathing room to do things in simpler ways.

    Interrupts do have substantial overhead, because the processor has to save and restore context, so they're not automatically better and in some cases much worse if used poorly. Do yourself a huge favor and try to use elapsedMicros for the non-stepper timing stuff.

Posting Permissions

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