# Timer Calls - overruns?

Guys, I’m using a timer on my project and I want to know what happens if my timer call takes too long. Does the next timer call stops the previous one, or a total mess happens? What will exactly happen? I tried to make it take forever and added a boolean variable check but couldn’t make it overrun, not sure what I did wrong. I need to figure out a way to check when overruns happens on my timer call, any ideas?

``````ISR(TIMER2_COMPA_vect)
{
// CODE HERE
}

void timerStart()
{
float frequency = ((float(midiClockBPM)*float(PPQ))/60.0f);

TCCR2A = 0;
TCCR2B = 0;
bitWrite(TCCR2A, WGM21, 1);
bitWrite(TCCR2B, CS20, 1);

uint32_t ocr = F_CPU / frequency - 1;
uint8_t prescalarbits = 0b001;

if (ocr > 255)
{
ocr = F_CPU / frequency / 8 - 1;
prescalarbits = 0b010;

if (ocr > 255)
{
ocr = F_CPU / frequency / 32 - 1;
prescalarbits = 0b011;
}

if (ocr > 255)
{
ocr = F_CPU / frequency / 64 - 1;
prescalarbits = 0b100;

if (ocr > 255)
{
ocr = F_CPU / frequency / 128 - 1;
prescalarbits = 0b101;
}

if (ocr > 255)
{
ocr = F_CPU / frequency / 256 - 1;
prescalarbits = 0b110;

if (ocr > 255)
{
ocr = F_CPU / frequency / 1024 - 1;
prescalarbits = 0b111;

if (ocr > 255)
{
timerStop();
return;
}
}
}
}
}

TCCR2B = prescalarbits;
OCR2A = ocr;
bitWrite(TIMSK2, OCIE2A, 1);
}

void timerStop(void)
{
bitWrite(TIMSK2, OCIE2A, 0);
TCCR2A = (1 << WGM20);
TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
OCR2A = 0;
}
``````

Best Regards, WilliamK

Does the next timer call stops the previous one

No. Unless you re-enable interrupts (which is a very bad idea), your interrupt service routine runs to completion.

What will exactly happen?

Your interrupt service routine completes. A single machine instruction is executed on the loop side. Your interrupt service routine is called again. Essentially, loop gets starved.

I need to figure out a way to check when overruns happens on my timer call, any ideas?

Time loop...

``````unsigned long PreviousLoop;

void loop( void )
{
unsigned long CurrentLoop;
unsigned long DeltaLoop;

CurrentLoop = micros();  // or millis()
DeltaLoop = CurrentLoop - PreviousLoop;
PreviousLoop = CurrentLoop;

// DeltaLoop continues to increase as loop becomes starved.  At a certain point, for all practical purposes, loop stops running.
}
``````