Pages: 1 [2]   Go Down
Author Topic: Bug: millis() delay off by +1 ms  (Read 3404 times)
0 Members and 1 Guest are viewing this topic.
SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6647
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I  like BenF's "compromise."   Depending on how much we trust micros()
Logged

Ross-on-Wye (UK)
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, err... since this is in the hardware part of this forum there is another potential solution...

Instead of trying to make time sync with the system clock, make the system clock sync with time. Timing crystals with a frequency of 16.384 Mhz are commonly available. The timers would overflow on exact 1 ms boundaries. PWM frequency would be exactly 500 Khz. The USART baud rate prescalars (UBRRn's) would need re-calibrating. Nice clean interrupt code.

Something to think about.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have done some testing of BenF's code solution and I think it is the best.  The use of uint16 types saves memory.  Overall the code is 20 bytes smaller than the existing delay() function.  In addition, the accuracy is impressive.  It displays an absolute accuracy of 6 microseconds on average.  The fractional error at the smallest reasonable delay, 1 millisecond, is just 0.6%.  I also confirmed that it handles rollovers in the micros() counter correctly.  There are no glitches in delay() every 70 minutes.  Adopting this code also means not having to update the delay() documentation.

In short, it's smaller, better, and requires less work from developers.  Is there someone with authority to make this change monitoring this bug forum topic?  Could you check in his proposed code?

The only change I would make is in the calling convention for delay where BenF has used 'uint32_t millis_delay' and I think it would be preferable to keep the existing 'unsigned long ms' since this matches the prototype of the function in wiring.h
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6647
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I created http://code.google.com/p/arduino/issues/detail?id=237
and attached BenF's code...

We'll see...
Logged

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 63
Posts: 2643
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why don't you talk to Hans-Juergen Heinrichs to use these and be done with it:
http://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_id=665&item_type=project

Unlike the standard AVR libC <util/delay.h> routines, these give you very accurate timings with good resolution even in the sub microsecond range.


--- bill
Logged

Pages: 1 [2]   Go Up
Jump to: