«

»

Feb 10 2013

Millisecond tracking library for AVR

This is a lightweight library for keeping track of time to millisecond accuracy, the data type for storing milliseconds can be easily configured, it supports ‘unsigned long long’ which can keep track of time for 584.9 million years before overflowing! Any clock frequency up to 20MHz can be used, most of the commonly used frequencies have no time keeping error (see table below).
Even though Arduino has its own millis() time keeping, this library may be handy if running at clock frequencies at or below 8MHz or for running longer than 50 days.

Download from GitHub

Brief comparison against the Arduino millis()
Arduino millis()

  • Microsecond support
  • Doesn’t loose or gain time at any clock frequency
  • Loss of resolution at lower frequencies

This library

  • Faster execution
  • Less RAM used
  • Always updates millisecond count every millisecond
  • Support for ‘unsigned long long’ data type, 64 bit integer allows tracking time for up to 584.9 million years, Arduino millis() uses ‘unsigned long’ which goes up to 49.71 days
  • Uses CTC mode for the timer, which might make it a little bit more difficult to use the timer for multiple things

Accuracy at commonly used clock frequencies

Clock This library (TIMER0) This library (TIMER1) This library (TIMER2) Arduino millis()
20MHz Looses 1.6ms each second
(0.16% error)
No error Looses 1.6ms each second
(0.16% error)
No error
16MHz No error No error No error No error
10MHz Looses 1.6ms each second
(0.16% error)
No error Looses 1.6ms each second
(0.16% error)
No error
8MHz No error No error No error Updates every 2ms
4MHz Looses 8ms each second
(0.8% error)
No error No error Updates every 4ms
2MHz No error No error No error Updates every 8ms
1MHz No error No error No error Updates every 16ms
500KHz Looses 8ms each second
(0.8% error)
No error Looses 8ms each second
(0.8% error)
Updates every 8ms

When the table mentions ‘Updates every ##ms’ it means, for example, if it’s 16ms when getting the value of millis() then 10ms later getting the value again, they will both be the same value (unless the 16ms ended during the 10ms delay).
Even when Arduino millis() gets ‘No error’ there is still a small jitter in the length of each millisecond since the interrupt doesn’t fire at exactly every 1ms.

Actual accuracy will ultimately depend on the accuracy of the uC clock source (external crystal, internal RC etc).

API

Function Description Parameters Returns
millis_init() Initializes library, must be called first! None void
millis_get() Get current milliseconds None millis_t
millis() Alias of millis_get(), only for non-Arduino since millis() is already used None millis_t
millis_resume() Turn on timer and resume time keeping None void
millis_pause() Pause time keeping and turn off timer to save power None void
millis_reset() Reset milliseconds count to 0 None void
millis_add(
millis_t ms
)
Add time ms – Milliseconds to add void
millis_subtract(
millis_t ms
)
Subtract time ms – Milliseconds to subtract void

6 comments

1 ping

Skip to comment form

  1. Carl W

    Cool library, thanks.

    I guess your accuracy table above is for using TIMER0 or TIMER2? You might want to point out that using TIMER1 is vastly more accurate in the “inaccurate” cases from your table…

    1. Zak Kemble

      The table is for TIMER2 vs Arduino TIMER0. The library only supported TIMER2 when I first released it. I’ll be updating the table with the accuracy of the other timers soon.

  2. Sovichea

    Great library! It’s very convenient for me because 1) it’s fast and 2) the ability to manipulate the timer like resetting the millisecond counts. Thanks for contributing this library.

    1. Zak Kemble

      Glad it’s of use to you! 😀

  3. Renato

    Why you limit in your library the maximum CPU speed 32640000? What microcontroller uses this speed? I thought the limit was 20000000

    1. Zak Kemble

      It’s actually possible to overclock an ATmega328 up to 30MHz, but really I used 32640000 because that’s the max frequency that can be used with the timer clock dividers that I defined in the library. It wouldn’t have made any difference if I used 20000000 instead.

  1. Millisecond tracking library for AVR | A Maker's Dream Factory

    […] For more details please go into the source. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Are you human? *