Author Topic: Do GetTickDiff Functions return Absolute Values?  (Read 2071 times)


  • Sr. Member
  • ****
  • Posts: 31
    • View Profile
Do GetTickDiff Functions return Absolute Values?
« on: January 07, 2008, 12:56:24 PM »
It looks to me like the GetTickDiff functions within the Tick.h module of the V3.05 stack could provide unintended responses, but I'm unsure do to my ignorance of the subtraction methodology used with the PIC processor in the case of an overflow condition (ie substrating a larger number from a smaller one).

It looks like these functions simply subtract the stored Tick value (the test value) from the current tick value.

In most cases this differential would be correct.  However in the case where the tested value is closer to the rollover value than the test interval, then the differential value returned would not be as intended by the name of the function.

Here's an example use GetTickDiffSec(t) which has a range of 18 hours.

I store a time:  testTime = TickGetSec()
I want to execute a command every 4 hours, so my test interval is 14400 (4 x 60 x 60)
The TickGetSec() tick counter rolls over to zero at around 64800 (18 hours, or around there)

Lets say everything starts at zero.  For the first four iterations the function TickGetDiffSec(testTime) returns the correct differential between the tick clock and my stored time.  Every time the TickGetDiffSec(testTime) function is greater than 14400, my routine executes, and then I update the current time testTime=TickGetSec().  The 4 hour count process begins again. 

There will come a time when I store a value like testTime=TickGetSec() will equal say 57600 (4 x 14400).  The TickGetSec counter however rolls over at 64800 (or close to that).   When the TickGetSec() counter rolls over, it becomes 0, and at that point, the function TickGetDiffSec(testTime) returns ( 0 - 57600) which I presume to be some large number because of binary subtraction overflow.  The result is that the event occurs prior to 4 hours, in this case, more like two hours.

Caveat:  I'm not sure exactly how subtraction of a larger number from zero works within the PIC instructions.  I'm vaguely familiar with 1's complement arithmetic, so it could be that the situation takes care of itself.

Can anyone confirm whether the TickGetDiff functions (any tick function that returns a differential between current time and stored time) return the absolute value of the difference?