301/317
10 - Second Application: a Sailing Computer
ld a, TBIC1HR
/* Get new time */
ld Capture1.W.Low, a
ld a, TBIC1LR
ld Capture1.W.Low:1, a
}
BoatPeriod = Capture1.Long - LastCapture1 ;
/* calculate time
difference */
LastCapture1 = Capture1.Long ;
/* Remember this time for next
capture */
}
if ( TBSR & ( 1 << ICF2 ) )
/* Is this is a capture 2 interrupt ? */
{
/* yes */
asm
{
ld a, TBIC2HR
/* Get new time */
ld Capture2.W.Low, a
ld a, TBIC2LR
ld Capture2.W.Low:1, a
}
WindPeriod = Capture2.Long - LastCapture2 ;
/* calculate time
difference */
LastCapture2 = Capture2.Long ;
/* Remember this time for next
capture */
}
if ( TBSR & ( 1 << TOF ) )
/* Is this is an overflow interrupt ? */
{
/* yes */
asm
{
ld a, TBCLR
/* clear interrupt request */
}
Capture1.W.High++ ;
/* Increment high order word */
Capture2.W.High++ ;
/* on both channels */
}
}
All timer events trigger the same interrupt request. At the beginning of the function, the
TBSR
status register is tested for one of the following three events:
Capture 1 event
Capture 2 event
Timer overflow event
Once the origin of the interrupt has been determined, the flag must be cleared. This is done by
reading the capture low register of the corresponding channel, or by reading the free-running
counter low register, for an overflow event.
As said above, the captured value is written to the low-order word of the capture variable
which is of type unsigned long. The overflow of the timer produces the incrementation of the