how can i get the accurate time

hi,

I used a multifunction Counter/Timer and digital I/O card (advantech
PCL-836) under QNX 4.25.

The program is such as follow:

#include"i86.h"
#include “stdlib.h”
#include “conio.h”
#include “string.h”
#include “math.h”
#include “stdio.h”
#include <sys/sched.h>
#define baseadd 0x200

void main()
{

unsigned int time,lowbyte,hibyte;
int i,j;

setprio(0,25);

time=0;

// initiate the timer
outp(baseadd+0x03,0x34); // 10MHZ freqency divided by 10
outp(baseadd+0x00,0x0a);
outp(baseadd+0x00,0x00);

outp(baseadd+0x12,0x02); // set inter clock
outp(baseadd+0x0b,0x30); // set operation mode 0

// set the timer
outp(baseadd+0x08,0x00);
outp(baseadd+0x08,0x00);

for(i=0;i<65500;i++)
for(j=0;j<5;j++)
{
;
}

// delay(1);

// read the timer
outp(baseadd+0x0b,0xd2);
lowbyte=inp(baseadd+0x08);
hibyte=inp(baseadd+0x08);

// stop the timer
outp(baseadd+0x0b,0x30);

hibyte=hibyte<<8;
time=hibyte|lowbyte;

time=(~time)+1;

printf("\n %d us,lowbyte:%x,hibyte:%x",(unsigned int)time,lowbyte,hibyte);

}

1.Every time the value i got is not so accurate ,that is the value sometimes
ranges greatly ,which is about tens of us.

But i test this program at Turbo C 3.0 under DOS,this is not case,the timer
works properly,and the rang is about 1 or 2 us.Even i use the function
delay(1) ,it got the satisifing result ,the value is 2001 or 2002 us.

Then i _disable() the interrupts under QNX. The range of the value dereases
,which is no more than 2 us (only the test results for the excution time of
the ‘for’ loops ) .But the another strange things occurs.


When i add another setprio(0,25) followed ,the value of the timer ranges
from many hundred of millionseconds in contrast to the only setprio() one
time ,why?

If there is one setprio() in the programe ,whatever the priprity you set,
the value of the timer got is almost the same. So you can conclude that
the value of the timer depends on
the amount of the setprio() function you have used.


I can’t tell which is the true time of the excution time of the loops of
the 'for’part.


Any help will be appreciated!

ycao wrote:


1.Every time the value i got is not so accurate ,that is the value sometimes
ranges greatly ,which is about tens of us.

Not surprising, since all interrupt handlers (and everything above prio
25 will pre-empt your code).


But i test this program at Turbo C 3.0 under DOS,this is not case,the timer
works properly,and the rang is about 1 or 2 us.Even i use the function
delay(1) ,it got the satisifing result ,the value is 2001 or 2002 us.

DOS does not have a pre-emptive scheduler, so this is expected.


Then i _disable() the interrupts under QNX. The range of the value dereases
,which is no more than 2 us (only the test results for the excution time of
the ‘for’ loops ) .But the another strange things occurs.


When i add another setprio(0,25) followed ,the value of the timer ranges
from many hundred of millionseconds in contrast to the only setprio() one
time ,why?

Not sure, where exactly did you put the setprio() ?


If there is one setprio() in the programe ,whatever the priprity you set,
the value of the timer got is almost the same. So you can conclude that
the value of the timer depends on
the amount of the setprio() function you have used.

This makes no sense, again, can you elaborate ?


ps: this belongs in the qnx4 group.