Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer, sleep delay - speed/precision needed

Status
Not open for further replies.

marvg007

Programmer
Jan 1, 2001
6
US
Hi, first off, to know why I need precise accuracy in this function, down to the exact millisecond is because I am trying to produce a frequency through my parallel port. I timed the call and return of the _outp()(function to write to the port in VSC++6.0) to be around 1/200,000th of a second. I only need my program to be exact to the 1/1000th of a second so that isn't much of a time consumer. On the other hand, the Sleep(ms) function is a MAJOR time consumer. I can only achieve at most 100Hz doing this:

while(1){

_outp(0x00378, 0x01); //Switch pin 0 to High
Sleep(1);
_outp(0x00378, 0x00); //Switch pin 0 to Low
Sleep(1);
}
The Supposed result should be that it pauses for 1 ms between switching from on to off, meaning that it does a cycle(on off) in 2 ms. This theoretically mean that it would give a frequency of 1000/2 per second or 500Hz.But it gives me 100Hz, 100 cycles a second at most!

and no, I don't think the while loop has anything to do with it since I batch process it, doing it 10 times before repeating a while.(I even experimented with no while, doing it 100 times)

I tried making my own delay function using clock() but the calls to clock() take Significantly longer than the calls to Sleep(). I'm thinking Sleep might be coded in assembly.

I need help finding an assembly written function to be used with C++ or such that might be able to help me make near precise frequencies. I know Micheal Abrash's Zen Timer does some pretty accurate calculations. Can someone show me a way to do this? Thanks!
 
Should Run as a windows console application since I'll be compiling in Visual C++ 6.0. So I'd say Windows Dos Box
 
I do not believe that you can realize it with a windows based operating System. With my experiences you could get a Stable frequency of about 20 Hz.

I would recommend you to switch to a real-time operating system like PSOS+ or similar.


hnd
hasso55@yahoo.com

 
I do have a snippet of info I pulled off usenet last year. I haven't tried this myself, but here is the quote:

"You could consider investigating the windows multi-media api (of course
assuming that windows is the platform you're working with). In Windows 3.1
(16 bit), the system DLL MMSYSTEM.DLL exports the multi-media API, which
should give you the required granularity. In Win16, the following functions
should be of interest to you: TIMEGETDEVCAPS, TIMESETEVENT, TIMEKILLEVENT,
TIMEBEGINPERIOD, TIMEENDPERIOD, TIMEGETTIME, TIMEGETSYSTEMTIME. You could
write a wrapper class for this DLL. In Win32 (Windows 95 and NT), the same
API is exported from the system DLL WINMM.DLL. I have'nt looked at it too
closely, but i'm sure it's what you're looking for. If you are using a
Windows Compiler, then there should be an appropriate header file supplied
by the compiler vendor, to provide declarations and prototypes for these
functions; look for MMSYSTEM.H, WINMM.H etc.


HTH.

John Mc Hale"

But like marvg007 says, there is no way to guarantee that some other task won't keep the system busy for too long and while will result in inaccurate timing inside your task. BTW, console apps are true windows apps and don't run in a DOS box. They just look the part.



Kim_Christensen@telus.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top