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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With