🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #2158

Re: How can I get a timed delay of 1 millisecond ?

#2158
From: Bruce Wright
Date: Tue, 11 Sep 1990 22:36
39 lines
2577 bytes
In article <6250@castle.ed.ac.uk>, elee24@castle.ed.ac.uk (H Bruce) writes: 
> What is the best way to get a program to wait for one milliosecond ?
> (I am using Microsoft C V5.1).

The usual way to get a timed delay of one millisecond on the
PC is to time a loop that't long enough to take a significant
amount of time even on a 486, and then scale that appropriately
to execute the loop the proper number of times for your PC.  This
can allow the delay to be pretty nearly one millisecond, without the
usual truncation problems you would have with a one millisecond
timer interrupt, which could have an error of up to one millisecond
if it's implemented as the usual multi-shot system timer interrupt.

The problem with this is that you generally can't do anything
useful during that loop (otherwise it might take different
amounts of time ...).  Getting around this problem requires that
you have a 1 millisecond (or smaller) timer, which makes you more
hardware-specific than the above method.  You can either put a
third-party timer on the machine (often sold in conjunction with
A-D boards), or you can reprogram the 8253 timer if your machine
is a "true" compatible (which most are these days, but it depends
on how paranoid you need to be for this application).  The problem
with reprogramming the 8253 (besides hardware compatibility issues)
is that you will then need to steal the timer interrupt from DOS,
and if you want the DOS time to remain accurate you will need to
provide DOS with a simulated interrupt every once in a while;  
then of course you will probably want to give the interrupt back
to DOS when you're through (if you don't and exit anyway you will
crash ... though if you are running a real-time process you may
not ever want to exit).

If you want further details on reprogramming the 8253 I can dig
up the information, but I don't have anything _quite_ like what
you want lying around & it has enough hardware-specific code that 
I can't just write it off the top of my head.

Hope this helps a bit ...

						Bruce C. Wright
Technical Details
Message-ID: <4064@rtifs1.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!uvaarpa!mcnc!rti!bcw
References: <6250@castle.ed.ac.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!uvaarpa!mcnc!rti!bcw
From: bcw@rti.rti.org (Bruce Wright)
Newsgroups: comp.os.msdos.programmer
Subject: Re: How can I get a timed delay of 1 millisecond ?
Summary: Delays on the PC
Message-ID: <4064@rtifs1.UUCP>
Date: 11 Sep 90 22:36:01 GMT
References: <6250@castle.ed.ac.uk>
Organization: Research Triangle Institute, RTP, NC
Lines: 39