General-Purpose Timers
Élan™SC520 Microcontroller User’s Manual 17-7
To test for rollover, software must read both timers two times in succession, reading the
least significant timer value (i.e., GP Timer 2), followed by the most significant timer value.
A very important assumption must be made that software is able to perform these four 16-
bit reads in less one tick of the “most significant” timer. Software may have to disable
interrupts in order to meet this qualification.
For example, suppose that on the first read, the value L1 is read from the least significant
timer, and the value M1 is read from the most significant timer. Then, values of L2 and M2
are read from the least significant and most significant timers, respectively. There are three
possibilities.
17.5.7.2.1 Case 1
(M1 = M2 = 0) and (L1 > L2)
This condition indicates that the most significant timer rolled over between reading L1
and M1. In this case, the correct value to be interpreted from the most significant timer
value should be one less than the value programmed into the GP Timer x Maxcount
Compare A (GPTMRxMAXCMPA) register (when the ALT_CMP bit is 0), or the maximum
of GP Timer x Maxcount Compare A (GPTMRxMAXCMPA) register and GP Timer x
Maxcount Compare B (GPTMRxMAXCMPB) register (when the ALT_CMP bit is 1).
17.5.7.2.2 Case 2
(M2 = M1 <> 0) and (L1 > L2)
This condition indicates that the least significant timer (but not the most significant timer)
rolled over between reading M1 and L1. In this case, the correct value to be interpreted
for the most significant timer is M1–1, which was the value of the most significant timer
at the time that L1 was read.
17.5.7.2.3 Case 3
■ In all other instances, if rollover occurred, then it occurred after L1 and M1 were read,
and L1 and M1 can be used for the correct values.
17.5.7.2.4 Example 1
For example, suppose GP Timer 0 is programmed in continuous mode, clocked by the
output of GP Timer 2, with the ALT_CMP bit cleared to 0 and a value of 2000h programmed
for the GP Timer 0 Maxcount Compare A (GPTMR0MAXCMPA) register.
GP Timer 2 is programmed in continuous mode, clocked by the internal 33-MHz clock with
the GP Timer 2 Maxcount Compare A (GPTMR2MAXCMPA) register set to 8000h.
The period of GP Timer 2 is:
8000h / 33 MHz * 4 = 4 ms
The cycle time of GP Timer 0 is:
2000h * 4 ms = 32.77 s
In the example, software reads the timers in the following order:
1. GP Timer 2 = 7997h
2. GP Timer 0 = 0h
3. GP Timer 2 = 14h
4. GP Timer 0 = 0h