boylesg
September 4, 2015, 3:47pm
1
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.
Robin2
September 4, 2015, 6:09pm
2
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.
boylesg
September 5, 2015, 3:28am
4
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
boylesg
September 5, 2015, 3:28am
5
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;
}
}