A Virtual Peripheral Time Clock Introduction               This application note presents programming techniques for implementing a real time clock that keeps a
16-bit milliseconds count, and has the option for full time clock capabilities, including seconds, minutes, hours,
and days. The routine takes advantage of the SX's internal interrupt feature to allow background operation of the
clock as a virtual peripheral.
How the code works This firmware module requires no external circuitry (other than an oscillator crystal) and is quite straight forward. There are three options for code assembly, controlled by the clock_type parameter. If clock_type=0, the
clock only counts milliseconds. A value of clock_type=1 adds seconds, minutes and hours, and a value of
clock_type=2 allows the days to be counted as well. The timing constant for each millisecond ‘tick’ is determined as follows: msec tick timing  =  osc. freq. / (1000 msec/sec * prescaler * mode) where mode=1 (turbo)  or  =4  (normal) So, for a crystal frequency of 50 MHz, in turbo mode, with a prescaler of 1, the msec tick timing constant is: msec tick timing  =  50 x 106 / (1000 * 1 * 1)  =  50 x 103               By comparing the number of elapsed instructions with the millisecond tick timing, the code decides when
one millisecond has passed and increments the msc_lo and msec_hi counters accordingly. In the same way, if
selected, the code checks the corresponding count and ticks off each second, minute, hour, day, etc.
              The time clock’s accuracy is dependent upon the accuracy of the oscillator used, which for crystals is
usually extremely good. For oscillators, especially slower ones, that do not have a frequency in kHz that is
divisible by an integer, the accuracy starts being affected by the msec count timing algorithm, and it should be
adjusted or left out accordingly1.
Modifications and further options                 Ideally the circuit and program will provide a method for the user to enter the time and date, etc.,
otherwise it is a relative time count in reference to the last time the circuit was turned on or reset.
                If the need for processor power between timed events is minimal, the routine could be modified and set
up in conjunction with the watchdog timer instead of the internal RTCC interrupt where the SX is put in sleep
mode between watchdog time-outs. This allows for a tremendous savings in power consumption.
                With some additional programming, day-of-the-week, month, and even year counts could be added, the
month count being somewhat more involved, for obvious reasons.
                                                       1 With a 32768Hz watch crystal, for example, this is not the optimal algorithm. The msec count could be dropped and only the seconds (and larger) counts kept to resolve this issue.