QDK PIC24/dsPIC-XC16 - Quantum Leaps
QDK PIC24/dsPIC-XC16 - Quantum Leaps
QDK PIC24/dsPIC-XC16 - Quantum Leaps
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>QDK</strong><br />
<strong>PIC24</strong>/<strong>dsPIC</strong>-<strong>XC16</strong><br />
www.state-machine.com/pic<br />
NOTE: The QS_onGetTime() callback is always called with interrupts locked.<br />
Figure 12 shows how the Timer2 is extended to 32 bits. The TMR2 register counts up from zero to the<br />
reload value stored in the PR2 register. When TMR2 reaches PR2, the hardware automatically clears TMR2<br />
on the subsequent clock tick. Simultaneously, the hardware sets the _T2IF flag, which triggers the<br />
system clock tick interrupt.<br />
The system clock tick ISR _T2Interrupt() keeps updating the “tick count” variable l_tickTime (see<br />
Listing 4(5) and Listing 8(2)) by incrementing it each time by QS_tickPeriod_. The clock-tick ISR also<br />
clears the _T2IF flag.<br />
Figure 12: Using 16-bit Timer2 to provide 32-bit QS time stamp.<br />
count<br />
32-bit time stamp<br />
returned from<br />
QS_onGetTime()<br />
count in<br />
_T2Interrupt()<br />
System<br />
clock tick<br />
period<br />
0x00FFFFFF<br />
Count in TMR2<br />
Current Register<br />
Timer2<br />
Reload<br />
Value<br />
Register<br />
System<br />
clock-tick<br />
time<br />
Listing 12(11-15) shows the implementation of the function QS_onGetTime(), which combines all this<br />
information to produce a monotonic time stamp.<br />
(12) The QS_onGetTime() function tests the _T2IF flag to determine whether This flag being set means<br />
that the TMR2 counter has rolled over to zero, but the _T2Interrupt() ISR has not run yet<br />
(because interrupts are still locked).<br />
(13) Most of the time the _T2IF flag is not set, and the time stamp is simply the sum of l_tickTime +<br />
(uint32_t)TMR2).<br />
(13) If the _T2IF flag is set, the TMR2 counter misses one update period and must be additionally<br />
incremented by BSP_TMR2_PERIOD_, which is a compile-time constant.<br />
Copyright © <strong>Quantum</strong> <strong>Leaps</strong>, LLC. All Rights Reserved.<br />
32 of 35