This post is a description of a possible addition to the Tiny Core for managing time: the ticks function.

There are currently four time-related functions: millis, micros, delay, and delayMicroseconds.  Because timer 0 is typically used for the time functions, for this and future posts, “timer 0″ is always used to reference the timer used for the time functions.

Like millis and micros, the ticks function returns an unsigned long.  Unlike the existing functions, the value returned by ticks is unit-less.  Utility functions can be used to convert a ticks-value to various units: ticks2us (convert ticks to microseconds), ticks2ms (to milliseconds), and ticks2s (to seconds).  Other utility functions can be used to convert from various units to a ticks-value: us2ticks, ms2ticks, s2ticks.

The current time-related functions are carefully crafted to eliminate all mathematical error.  Unfortunately, that is not possible with ticks.  At some processor speeds converting a ticks-value introduces an error.  Except in extreme circumstances (like trying to convert a very small ticks-value to a small number of microseconds) the error is much smaller than the error from the internal oscillator or an external resonator.  Typically, only if the application needs the accuracy of a crystal will the ticks-value conversion be a concern.

The ticks function is smaller and uses less SRAM than the current time functions.  Theoretically, the interrupt service routine will be fast enough that timer 0 can be configured for very high frequency PWM.  If only tick-values are used at run-time, the overall program size will be a bit smaller and a bit faster.  For the kinds of ATtiny applications that are typically develop, ticks is a very welcome addition.