Hello, I'm using timer 2 in capture mode to measure the time between to interrupts (T2CON = 0x09). The interrupts are triggered by a 1->0 transient on pin P1_1 (so I'm using T2EX). I need to calculate the time between the previous value of RCap and the current one in the interrupt routine, taking into account the fact if timer 2 has overflown or not. Since I'm working in the interrupt routine, I want to use as less code as possible. For the moment, I'm using a static global variable (RCapTime, see below) where I store the value of RCap when the interrupt occurs. The next time the interrupt occurs, I'm calculating the difference between the stored value of RCap and the new one and store it in a local --function scope-- variable, according the following formula:
DeltaTime = ( TF2 ? ( NewRCapTime - RCapTime ) : ( RCapTime - NewRCapTime ) );
The PCA cookbook found at http://www.intel.com/design/MCS51/applnots/27085101.PDF has many ways to do what you want. Although it refers to the PCA, timer2 in most respects is a PCA with just one compare register instead of 5. If that should inspire you to use a derivative with a PCA such as the P89C51Rx2 and P89C66x for an even better solution, that will be up to you. Erik