Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

embedded timer wrap

Tags:

c

embedded

timer

Currently, im working on an embedded project, where most of the subsystems are based on timing.

i have searched for many solutions to avoid issues with timers rolling over, but one thing still puzzles me.

At present time, i am using the twos complement of unsigned long's like this.

ulong t1 = tick of last event;
ulong t2 = current tick;
if ( t2 - t1 >= limit ){
 do something
}

Others have suggested, that it is necessary to cast the result of t2-t1 to a signed entity, before this will work, but that i can't understand why. Any other insights or suggestions?

like image 259
olekaiwalker Avatar asked Nov 03 '22 19:11

olekaiwalker


1 Answers

Sometimes I do it like this:

ulong t1 = tick of last event;
ulong t2 = current tick;

if ( t1 > t2 ){
    if ((ULONG_MAX-t1+t2+1)>=limit){
       do something
    }
} else {
if ( t2 - t1 >= limit ){
    do something
}
like image 152
Jeff Avatar answered Nov 15 '22 06:11

Jeff