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:
Permalink Reply by Gareth Farrington on February 25, 2008 at 11:29am 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)
Permalink Reply by Gareth Farrington on February 25, 2008 at 5:44pm 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) ;
}
Permalink Reply by Gareth Farrington on March 3, 2008 at 9:24am
Permalink Reply by bGatti on April 16, 2008 at 7:13am
Permalink Reply by fornzix on June 5, 2008 at 7:52am
Permalink Reply by Gareth Farrington on June 3, 2008 at 10:54pm
Permalink Reply by Venom57 on December 27, 2008 at 6:17am
Season Two of the Trust Time Trial (T3) Contest has now begun. The third round was a reliablilty/aerial photography round for both planes and copters, which is now closed. Stay tuned for the next round, beginning soon.51 members
58 members
67 members
41 members
26 members
© 2012 Created by Chris Anderson.
