Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE, C: Counting milliseconds — Parallax Forums

SimpleIDE, C: Counting milliseconds

ptiagoptiago Posts: 37
edited 2015-06-19 12:56 in General Discussion
Hi,

Probably the question is not new...

I need to count the milliseconds, and i would like to be efficient and reliable.

I made a simple C program to test (attached).

To make it short: this is the method executed in a cog:

volatile static unsigned long millis=0;
void countMillis(void *par)
{
while(1)
{
millis++;
waitcnt(CLKFREQ_1000 + CNT);
}
}

this is the code to measure the 1sec waiting

ts1 = millis;
waitcnt(CLKFREQ + CNT);
ts2 = millis;

the delta (ts2-ts1) is 992/993 ms, there is a gap of 7 to 8 ms, countMillis, performs two sum operations and the measure code one sum operation plus the assigning variables etc.

what i can do minimize the gap, or to obtain the most realist milliseconds counter ?

Thanks,


Tiago
c
c
1K

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2015-06-19 12:39
    To eliminate the loop overhead and the time used by millis++ you can do the following
    volatile static unsigned long millis=0;
    void countMillis(void *par) 
    {
      int count = CNT;
      while(1)
      {
        millis++;
        waitcnt(count += CLKFREQ_1000);
      }    
    }
    
  • ptiagoptiago Posts: 37
    edited 2015-06-19 12:56
    Dave,

    Thanks, it works !!!!

    1000 ms, in all the cogs at same time.

    Tiago
Sign In or Register to comment.