Skip to content

Commit

Permalink
Improve micros() accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUdude committed Apr 17, 2020
1 parent 3553ec1 commit 7714ac9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion megaavr/cores/coreX-corefiles/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,28 @@ unsigned long micros()
// Restore SREG
SREG = status;

return (m * 1000L) + (t / (TIME_TRACKING_TIMER_COUNT / 1000));
#if (F_CPU == 20000000L)
t = t >> 4;
return m * 1000 + (t - (t >> 2) + (t >> 4) - (t >> 6));
#elif (F_CPU == 16000000L)
return m * 1000 + (t >> 4);
#elif (F_CPU == 10000000L)
t = t >> 3;
return m * 1000 + (t - (t >> 2) + (t >> 4) - (t >> 6));
#elif (F_CPU == 8000000L)
return m * 1000 + (t >> 3);
#elif (F_CPU == 5000000L)
t = t >> 2;
return m * 1000 + (t - (t >> 2) + (t >> 4) - (t >> 6));
#elif (F_CPU == 4000000L)
return m * 1000 + (t >> 2);
#elif (F_CPU == 2000000L)
return m * 1000 + (t >> 1);
#elif (F_CPU == 1000000L)
return m * 1000 + t;
#else
return 0;
#endif
}

void delay(unsigned long ms)
Expand Down

0 comments on commit 7714ac9

Please sign in to comment.