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 | Loses 1.6ms each second (0.16% error) |
No error | Loses 1.6ms each second (0.16% error) |
No error |
16MHz | No error | No error | No error | No error |
10MHz | Loses 1.6ms each second (0.16% error) |
No error | Loses 1.6ms each second (0.16% error) |
No error |
8MHz | No error | No error | No error | Updates every 2ms |
4MHz | Loses 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 | Loses 8ms each second (0.8% error) |
No error | Loses 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 |
Comments
Skip to comment form