ArduinoShrink icon indicating copy to clipboard operation
ArduinoShrink copied to clipboard

micros() doesn't include fractions of milliseconds.

Open WestfW opened this issue 7 months ago • 3 comments

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 :-( )

WestfW avatar Apr 06 '25 00:04 WestfW

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.

nerdralph avatar Apr 06 '25 02:04 nerdralph

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

nerdralph avatar Apr 08 '25 18:04 nerdralph

Fix is not fully tested. It builds OK and the complied asm looks OK. Still needs testing on a live target.

nerdralph avatar Apr 13 '25 18:04 nerdralph