This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Efficient RCap calculations

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 )
                    );
I also know for sure that the timer can overflow at most once between 2 interrupts, because the max. time between two interrupts is about 4 ms (that's a fact). On a 12 Mhz clock conventional 8032 processor, timer T2 can have as much as 65,565 ms (in theory...) before it generates an overflow. So, more than 1 overflow between 2 interrupts is simply not possible in my case.

However, I have the feeling I have to much overhead doing the calculation given above. To my opinion, it could be done much more efficient.

Does someone know a trick to optimize the formula given? Eventually by writing it in assembly (however, I'm not used to write in assembler...)

Rgds,

Geert