Built-in timer

Im is currently working on an embedded project where most of the subsystems are based on synchronization.

I have been looking for a lot of solutions to avoid the problems with transitioning timers, but one thing still puzzles me.

I am currently using two unsigned long add-ons 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 pass the result of t2-t1 to the signed object before this works, but I cannot understand why. Any other ideas or suggestions?

+5
source share
3 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
}
+2
source

, t2 , t1, . UTYPE_MAX + 1, t2 - t1 ULONG_MAX + 1 unsigned long s. , . .

if ( t2 - t1 >= limit ){

, , ULONG_MAX, .

t2 t1, t2 - t1 , unsigned long, . , .

0

You do not need to point it to a signature. If "do something" is the alarm handling code, and the limit is the time interval.

Best practice at regular intervals:

 ulong next_alarm_ticks = 0;

 void poll_alarm()
 {
     if (get_ticks() - next_alarm_ticks < ULONG_MAX/2) { 
         // current time is "greater" than next_alarm_ticks
         handle_alarm();
         next_alarm_ticks += alarm_interval;
     }
 }
0
source

All Articles