delay implementation


I'm looking for the delay() implementation in Arduino.
Is this used ..\hardware\arduino\avr\cores\arduino\wiring.c? (from the source,github)

Is this used

On some Arduinos, yes.

On Atmega2560?

On Atmega2560?

Yes, the Uno, Leonardo, Mega, Micro, Mini are all AVR chips.


Trying to understand the delay routine in wiring.c...

The yield() is simply a ret instruction and the interrupts are disabled in micros(). When will timer0_overflow_count variable incremented?

I believe that happens in the timer0 overflow ISR, which is the other part of micros and millis and delay.

But when will the ISR executed? The ISR is disabled with cli() in micros()..
And the yield() returns immediatelly.

From micros() in wiring.c:

uint8_t oldSREG = SREG, t;




The SREG register is restored near the end of the function. SREG contains the interrupt enable flag, so restoring it restores the interrupt enable flag to what it was before micros() was called.

The ISR runs in all 1024us: prescaler is 64, f=16Mhz (I assumed Atmega2560 at 16Mhz)
Assume the timer setup end at t=100u, so the ISR will run in 1124u, 2148u, ...

The delay code runs contininously. Assume 'delay(100)' for example. Because in micros() the ISR is disabled(and restored at the end) the ISR can run only in yield() but this is very short, I checked the disassembled statements this is a simple 'ret' instruction, so when the yield() executing in 1124u, 2148u, the ISR will called. What I made wrong..?

void delay(unsigned long ms)
	uint16_t start = (uint16_t)micros();

	while (ms > 0) {
		if (((uint16_t)micros() - start) >= 1000) {
			start += 1000;

Clearing the TOV0 flag is not missed from micros() ? (wiring.c)
It is possible to loose the interrupt because in micros() the interrupts are disabled.

#ifdef TIFR0
	if ((TIFR0 & _BV(TOV0)) && (t < 255)) {
          TIFR0 |= _BV(TOV0);
	if ((TIFR & _BV(TOV0)) && (t < 255)){
          TIFR0 |= _BV(TOV0);