[comp.sys.ibm.pc] Counting time intervals in QuickC

fang@dukempd.phy.duke.edu (Fang Zhong) (09/10/89)

	I want to be able to count time intervals with the resolution of
1/100 second.
	I tried both "ftime()" and "clock()" functions in MS-C.  I found
that both the functions gave the error results when it jumps from 1.90 to
2.05 or whenever it crosses the second.
	Anyone who has QuickC 2.0  can try the sample program "Sieve.c".
Add a loop to the main program to repeat.  Change 10000 in the original
loops to 1000 to reduce the waiting time.  It is found that the results
are not same.
	I have tried this modified program on different machines.  It
gave me the same error.
	I do not know if this is the problem of MS-C or just PC.
	Any idea is approciated.  Please answear directly.

-- 
	My Name					1-919-684-XXXX
	Duke University Dept. of Physics	change_this@phy.duke.edu
	Durham, N.C.      27706			

negris@southbay.sybase.com (Tim Negris) (09/10/89)

The Intel hardware timer chip in the PC by default delivers a timer tick that is less than the 100 ticks per sec that you are trying to get.  
I think it is 52.
The way this works is every 18/100's of a second or so the system generates a *hardware* interrupt through the timer chip, (actually chip set on ATs, I think).
This tick, or interrupt is used by the system for two primary purposes - keeping the system clock up to date and knowing how to time out an open floppy drive after two seconds.  
There may be other, internal applications, but these are the two you see mentioned most commonly.

You can get finer resolutions, I have experimented with speeds well in excess of 100 ticks per sec, but to do so you must reprogram the the timer hardware.
I'm not quite sure how, or if, you could do this entirely in C.
I had to resort to MASM to do some of it.

You have to replace the timer interrupt with your own, reset the timer chip so that it is running faster, and every 18/100th of a second temporarily return control to the normal interrupt.

Replacing interrupts and the related mechanics of interrupt service routines is a process that is documented in just about every book I have seen on Assembly language for the PC.
Maybe you can do this in C, it seems that in the most recent MSC 5.1 I saw some functions related to the process.
The thing that is a little different here is that this is involves one of the few accessible hardware interrupts available on the PC.

Resetting the timer chip is relatively simple, you put certain values in hardware ports etc.
I think the C function for this is INP, or INPUT, or is it OUTPUT, I never could keep that straight.
The particulars of this chip and how to twiddle it are in many of the more hardware oriented PC books or Intel documentation.

The book titled something like "Supercharging C with Assembly Language" provided me with a program example that I was able to borrow from to build a program that could do this.
An incidental side benefit of changing the timer speed is that, because the timer is also used by the sound generator on the PC, you can create much more elaborate sounds by altering the speed of the timer - sine waves and such.

I hope this helps. 
If my system weren't down with a crashed hard disk, I could have dusted off some old code and gotten a little more specific.