ArduinoShrink
ArduinoShrink copied to clipboard
micros() doesn't include fractions of milliseconds.
The timer0 interrupt code maintains t0_fract, but the micros() function only counts milliseconds plus timer count values, leaving out that fractional milliseconds that have elapsed, and thus producing incorrect results.
(This seems to be the only reason that the original Arduino code maintains both a millisecond count AND an overflow count :-( )
I may have to think about this a bit. One of the optimizations I did with ArdunoShrink is make millis() and micros() work without disabling interrupts. The fix means adding (t0_fract * 4) to the result, and I'd like to do that with a minimal amount of code and still without disabling interrupts. So far the optimal implementation has not come to me yet.
t0_fract is a negative value, so I think it should be (255 - t0_fract) * 4. https://github.com/nerdralph/ArduinoShrink/blob/master/src/t0_ovfl_isr.S#L38
Fix is not fully tested. It builds OK and the complied asm looks OK. Still needs testing on a live target.