Working with millis()

I'm very new to all this....

  • Reading the Arduino doc I see that millis() overflows every 9 hours
  • Reading this forum, I gather that millis() runs on Timer0
  • Reading the specs for the ATMega168 I see that Timer0 is 8 bit

Since Timer0 is only 8 bits I gather that the Arduino "system" must set the "prescalar" to update Timer0 at FAR LESS than the system clock frequency (otherwise we would never get to 9 hours).

I need something closer to "micros"()... well maybe not microseconds, but at least TENTHS of milliseconds.

Am I correct about the prescaler? Can I get in and adjust the prescaler to get finer timing out of millis() without breaking the Arduino?
dda

By the way....
If 8 bits gives 9 hours at milliseconds, I guess I would only get a bit over 30 seconds at microseconds!!!!!
As it happens, for my current needs, 30 seconds would be more than enough between Timer0 rollovers.

dda

Here is basically what I'm doing for better time resolution within arduino, but it will probably stop working with 0012:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210889307/29#29

You will need to add the following line before that code to get it to work:
extern volatile unsigned long timer0_overflow_count;

Thanks...
Your code snippet has helped me greatly in understanding what is going on.... especially after I compared it with what I found in:
D:\Arduino\hardware\cores\arduino\wiring.c (in my installation).
...
I will insert it in my project as is, then come back and tune it (if necessary) when all my code is in place.

BTW... why will it break in 0012?

dda

mellis is changing millis in 0012 so it can go much longer without overflowing:

It should only overflow after 1200 hours instead of 9 with that fix. :slight_smile: But timer0_overflow_count is going away and I haven't thought about how to adapt yet.