PDA

View Full Version : VERY accurate timing



Gherkin
09-28-2001, 08:07 AM
Hello,

I am using Borland C++ 3 (yeayea oldie, need it for school) in MSDOS and trying to do VERY accurate timing.

I need to get the time in milliseconds or more accurate. The clock() function divided by CLK_TCK divided by 1000 does NOT count milliseconds, ts not accurate enough, I tested that.

Does anyone know a way to get REAL milliseconds or better?Maybe some assembly routine?

Thanks!

Salem
09-28-2001, 04:05 PM
You'll probably need to do some reading up on the programmable interval timer (PIT) which is used on PCs to control time related activities.

This code reads the PIT...


#include <stdio.h>
// for inp() - might also be called inportb or inb or something like that
// could also be in a different .h file
#include <pc.h>

unsigned int read_pit ( void ) {
// a 16 bit value, obtained in lsb/msb order
unsigned char lsb = inp( 0x40 );
unsigned char msb = inp( 0x40 );
return lsb + ( msb << 8 );
}
int main ( ) {
unsigned int a, b;
int i;
double elapsed;

a = read_pit();
for ( i = 0 ; i < 1000000 ; i++ ) {
// do stuff to be timed
// this has at most one rollover on my machine...
}
b = read_pit();
printf( "%u %u\n", a, b );
if ( a < b ) {
// rollover adjustment
// this assumes at most once - see below...
a += 0x10000;
}
elapsed = (a-b) / 1193181.0; // in fractions of a second
printf( "%f seconds\n", elapsed );
return 0;
}


The PIT counts down at a rate of 1193181 per second, which when divided by 65536 (max short int), gives the 18.2 ticks per second you may have seen around.

But you'll also need to trap the timer interrupt to record how many times the timer rolls over from 0x0000 to 0xFFFF.

This may help...
http://www.ctyme.com/intr/rb-0043.htm

Gherkin
09-28-2001, 04:30 PM
mmm that rollover is nasty, never created such handlers, but ill try something and crash my system a couple times again, hehe :D