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

Thread: TeensyTimerTool setPeriod(), setNextPeriod()?

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    4

    TeensyTimerTool setPeriod(), setNextPeriod()?

    Has anyone gotten .setNextPeriod() or .setPeriod() working for the TeensyTimerTool?

    I'm trying to change the period of a TCK64 (software) timer from within the timer callback function. My code compiles just fine when I use either of these functions but they don't do anything.

    I can get the code to work if I use .begin(), but I am assuming that it is not a good idea to use .begin() within a callback function.

    Here is my working code example with .begin() within a callback function.

    Code:
    #include "TeensyTimerTool.h"
    using namespace TeensyTimerTool;
    
    PeriodicTimer tmr(TCK64);
    // ==============================================
    void setup()
    {
      // put your setup code here, to run once:
      pinMode(LED_BUILTIN, OUTPUT);
      tmr.begin(callback, 500);  // 50us timer (period is unimportant here, just starting it up)
    }
    // ==============================================
    void callback()   // two 1/4 second LED blinks alternating with two 1/2 second blinks
    {
      static uint32_t period[] = {250000, 250000, 250000, 250000, 500000, 500000, 500000, 500000};
      static int x = 0;
    
      digitalToggleFast(LED_BUILTIN);  
      tmr.begin(callback,period[x]);
      ++x;
      if (x > 7)
        x = 0;
    }
    // ==============================================
    void loop() 
    {
    }
    // end of code
    Is the above code 'safe' to run since it seems to work?
    The above code is just a simple example to demonstrate the issue I am having. The program I am using it in will be making use of the general technique to perform pre-calculated acceleration profiles for a motor. I don't want to use a motor library if I can help it because I am trying to 'roll my own' and hopefully learn something along the way.

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,704
    Those functions are (currently) not implemented for all timers (the PITs should work). Anyway, you should get an corresponding error message. To activate Error handling just add
    Code:
    void setup()
    {
      while(!Serial){}
      TeensyTimerTool::attachErrFunc(ErrorHandler(Serial));
      // ....
    }
    See also: https://github.com/luni64/TeensyTime...Error-Handling

    The code above should be save but it will reconfigure the the timer in its callback which is not very efficient. I can add the missing functions to the TCK timers later this week.

  3. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Quote Originally Posted by luni View Post
    Those functions are (currently) not implemented for all timers (the PITs should work). Anyway, you should get an corresponding error message. To activate Error handling just add
    Code:
    void setup()
    {
      while(!Serial){}
      TeensyTimerTool::attachErrFunc(ErrorHandler(Serial));
      // ....
    }
    See also: https://github.com/luni64/TeensyTime...Error-Handling

    The code above should be save but it will reconfigure the the timer in its callback which is not very efficient. I can add the missing functions to the TCK timers later this week.
    Thanks luni! Good to know about the error handler, I will include that tonight. The fact that it compiled fine really had me stumped!

    I'm looking forward to trying out the other TCK functions if/when you have a chance to add them. Awesome library, I really like the versatility of the TCK64 timer(s) especially - Thank you!

Posting Permissions

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