Does Counter for micros() Run During Interrupts?

Status
Not open for further replies.

gfvalvo

Well-known member
Hi all.

I’m using the IntervalTimer class to run periodic processing on a T3.2. I also have an external signal that generates an interrupt on its rising edge. I need to time the interval between these interrupts with micro-second precision. One idea I have is to use the micros() function for this as shown in the code below.

So, my question -- Is the counter that keeps track of time for micros() disabled during interrupts? Since the IntervalTimer runs my periodicTask() so frequently, I’m concerned that my timing of the external interrupts will be thrown off if the counter for micros() doesn’t keep running.

If the counter does keep running during ISRs, I think I’m OK.

Otherwise, I was thinking of using one of the other Periodic Interrupt Timers (counter enabled but interrupt disabled) to count down from 0xFFFFFFFF. I could then read it during my serviceTrigger() ISR to determine the interval since the previous trigger (and then reset it to 0xFFFFFFFF). Thoughts?

Thanks.

Code:
void periodicTask(void);

IntervalTimer myTimer;
volatile uint32_t previousMicros, deltaMicros;
volatile bool newDeltaAvailable = false;
const uint8_t triggerPin = 2;

void setup() {
  Serial.begin(115200);
  pinMode(triggerPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(triggerPin), serviceTrigger, RISING);
  previousMicros = micros();
  myTimer.begin(periodicTask, 200);
}

void loop() {
  uint32_t deltaT;
  
  if (newDeltaAvailable) {
    noInterrupts();
    deltaT = deltaMicros;
    newDeltaAvailable = false;
    interrupts();

    // Do stuff here with new deltaT value
    
  }
}

void periodicTask() {
  // Do periodic task here in ISR
}

void serviceTrigger() {
  uint32_t currentMicros = micros();
  deltaMicros = currentMicros - previousMicros;
  previousMicros = currentMicros ;
  newDeltaAvailable = true;
}
 
Last edited:
Did you determine if it caused problems?

My understanding is micros() continues to work during interrupts, but I am not generally measuring to the microsecond haha
 
I ended up going a different route than using the micros() function. I dug into the code for the PJRC IntervalTimer class and learned how to directly manipulate the T3.2’s Periodic Interrupt Timers. These count down at 48 MHz in hardware giving me all the timing precision I need.
 
Status
Not open for further replies.
Back
Top