Dealing with micros rollover in an ISR

Have I implemented this algorithm correctly:

void doStartDisplay()
{
uint32_t lunTimeElapsed = micros(), lunPeriod = 0, lunMask = 0;

lunPeriod = lunTimeElapsed - lintLastTimeElasped;
lunMask = lunPeriod >> 31;
lunPeriod = (lunMask ^ lunPeriod) - lunMask;

lintLastTimeElasped = lunTimeElapsed;
lunTimeSlice = lunPeriod / (60 * 6);
lunCount = 0;
// Timer1.attachInterrupt(doDisplay, lunTimeSlice);
// doDisplay();
}

I am trying to get rid of the if statement I previously had which would probably consume more CPU cycles.

Why not reduce your ISR to

void  doStartDisplay() {
    lunTimeElapsed = micros();
}

and do the rest of the calculation outside the ISR

Your names are not very well chosen for that they relate to.

...R

You know, after 200+ posts you should know how to use code tags when posting source code here. If not, read the two posts by Nick Gammon at the top of this Forum.

Robin2:
Why not reduce your ISR to

void  doStartDisplay() {

lunTimeElapsed = micros();
}




and do the rest of the calculation outside the ISR

Your names are not very well chosen for that they relate to.

...R

Didn't occur to me to do it that - thanks.

lunMask = lunPeriod >> 31;

This is likely to be unnecessarily slow.

If you are measuring intervals less then 71 minutes you can use the same math as for millis():

unsigned long EventInterval = 3276700UL; // Microseconds between events

void  doStartDisplay() {
    static unsigned long lastEventTime = 0;

   if (micros() - lastEventTime >= EventInterval) {
       // do the thing
       lastEventTime += EventInterval;
  }
}