 # micros() function questions

``````unsigned long micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;

cli();
m = timer0_overflow_count;
t = TCNT0;

if ((TIFR0 & _BV(TOV0)) && (t < 255))
m++;

SREG = oldSREG;

return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
``````

The timer0_overflow_count is the number of times of the overflow of Timer/Counter0. The value of timer0_overflow_count is set by an interrupt handler.

TCNT0 is a register that is incremented at regular intervals. The division ratio of Timer/Counter0 is set to 64 in Arduino Uno, the TCNT0 is incremented at every 64 clocks.

# If the TOV0 bit of TIFR0 is set and TCNT0 is smaller than 255, it increments m.

1)my question is why need to keep track the number of overflow of timer0, TCNT0?
2)if t > 255, would that trigger an overflow?
3)once a TOV0 bit is set which indicates an overflow, what happen to t? would it rollover to 0 ?
and when will TOV0 be reset?
4)what does m << 8 mean? I know m holds the number of overflows.

1)my question is why need to keep track the number of overflow of timer0, TCNT0?

So we can count up to 71.58 minutes. Without counting overflows you could only count up to 4 µs.

2)if t > 255, would that trigger an overflow?

t is uint8_t, so it will never be > 255.

3)once a TOV0 bit is set which indicates an overflow, what happen to t? would it rollover to 0 ?

Yes, it rolls over.

and when will TOV0 be reset?

When the overflow ISR is entered.

4)what does m << 8 mean? I know m holds the number of overflows.

It multiplies m by 256. Since it overflows ever 256 counts, together that gives you the number of counts, including overflows.