Does a properly configured interrupt timer start instantly?

I'm using this Due specific timer library

and for reasons I don't understand it takes 80 microseconds for a timer to get going after being started.
For example, at the end of the setup function I have
Timer5.start(50);
I don't see any activity until 80 microseconds has elapsed then it does its expected 50 microsecond repeating cycle.

How are you measuring this 80us

That would be about correct, you are using a high level language that has been converted to assembler for the Arduino by the compiler. You are passing an argument which has to be set before starting the timer. There is also some other configuration going on in the background. Each of these instructions takes time.

Here's a minimal test program and corresponding scope trace.
The overhead of Timer5.start() appears to be a constant 80 microseconds regardless of the set interval.
My original goal was to use an external interrupt to generate a set of pulses that would be in phase with the trigger. Effectively a phase locked loop that runs n times before being retriggered by the next external interrupt.

#include <Arduino.h>
#include <DueTimer.h>

const uint8_t digPin = 35;

void myHandler()
{
  static uint8_t counter = 0;

  digitalWrite(digPin, HIGH);
  digitalWrite(digPin, LOW);

  counter++;
  if(counter == 8){
    Timer5.stop();
  }
}

void setup()
{
  pinMode(digPin, OUTPUT);
  
  Timer5.attachInterrupt(myHandler);

  digitalWrite(digPin, HIGH);
  Timer5.start(50); // 20k
  digitalWrite(digPin, LOW);
}

void loop()
{
}

a solution may be if you use start() only once and disable and enable the timer interrupt by directly setting the appropriate register. TC_IER. you could reset the timer counter SWTRG and enable the timer interrupt from the external interrupt handler for the proper timing.

I recommend that you review the timer documentation, especially section 36.6.12 External Event/Trigger Conditions

1 Like

Yes, it looks like the start() method actually blocks everything for 80uS. If there is also an overhead with setPeriod() or setFrequency(), which presumably you would have to use to maintain synchronisation with whatever clock you are using, then you'll have difficulties using this library for your application.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.