MS_TIMER & TICK_TIMER Too slow by half

I have the following code in my program and I’m using TICK_TIMER. I expected the code inside the if to execute every 34 ticks, which should be 33.2mSec. Instead it’s 66.4mSec. I have run lopower.c and it reports that the starting frequency for my clock is 58.9MHz. Just what I expected. So what’s the deal? Code snippet follows:


main(void)
{
long t1=0, t2;
...
...
...
   for(;;)  // endless loop
   {
      t1 = TICK_TIMER;
      if (t1 - t0 >= 34)
      {
         digOut(0,1);
         t0 = t1 + 34;
         digOut(0,0);
      }
   }
}

The digOut’s are so I can put my scope on the output and measure the time.

Tom

You make two time the +34 operation

You can replace the if condition by :
if (t1-t0 >=0)

Thanks Nicolas,

Are you saying that 34 is added to t0 in the if statement? I can’t check until I get home tonight but I will definitely try your suggestion.

Tom

Now I see the problem. For example if t1 = 50 and t0 = 16 the if comparison is true and t0 = 50 + 34 = 84.

So the next time through the for loop the comparison will be:
if ( 51 - 84 >= 34 )

This will take twice as long to get to t1 = 118.

Thanks again
Tom