millis() and micros() and delayMicroseconds inside ISR. (?)

BulldogLowell:

jboyton:
micros() will return the right value unless timer0 rolls over while you're in your ISR.

???

what happens then? does micros() return the wrong value?

micros() returns the hardware timer contents (which updates continuously), plus a count of rollovers (ie. one rollover ever 1.024 mS). It can handle one rollover (the hardware remembers that) so it doesn't matter if you cross a rollover point, however after 1.024 mS it will not know about the second rollover and then will be 1.024 mS out.

Slightly simplified code (removed stuff not for the Atmega328) and with comments:

unsigned long micros() {
	unsigned long m;
	uint8_t oldSREG = SREG, t;   // remember if interrupts were on
	
	cli();   // turn interrupts off
	m = timer0_overflow_count;   // get overflow count from previous interrupts
	t = TCNT0;     // get hardware counter
  
	if ((TIFR0 & _BV(TOV0)) && (t < 255))
		m++;  // increment overflow count if hardware has rolled over

	SREG = oldSREG;  // turn interrupts back on if needed
	
	return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());  // return result
}

It multiplies by 64 rather than 256 because of the way the prescaler is set up (one "tick" every 4 µS).