unsigned long millis() {
// timer 0 increments every 64 cycles, and overflows when it reaches
// 256. we would calculate the total number of clock cycles, then
// divide by the number of clock cycles per millisecond, but this
// overflows too often.
//return timer0_overflow_count * 64UL * 256UL / (F_CPU / 1000UL);
// instead find 1/128th the number of clock cycles and divide by
// 1/128th the number of clock cycles per millisecond
return timer0_overflow_count * 64UL * 2UL / (F_CPU / 128000UL);
}
Tags:
return timer0_overflow_count * 64UL * 2UL / (F_CPU / 128000UL);
timer0_overflow_count * 256UL * 64UL
(F_CPU / 1000)
(timer0_overflow_count * 256UL * 64UL) / (F_CPU / 1000)
unsigned long hpticks (void) { return (timer0_overflow_count 8) + TCNT0;}Taking what we now know about the implementation of millis() we can improve this to get rid of the error. TCNT0 contains the number of ticks in Timer 0. Its some number between 0 and 255.
unsigned long micros(void) { return (((timer0_overflow_count * 256UL) + TCNT0) * 64UL ) / (F_CPU / 1000000UL) ; }
1470 members
162 members
631 members
259 members
281 members
© 2019 Created by Chris Anderson. Powered by