time delay function

This function has been using in the old code. Now, that we put a program in the new hardware we have a problem with data collection that is offset by some number. One probably reason is timing issues. Our delays range from 0.1 to 5 seconds - nothing extreme.

1
2
3
4
5
6
7
8
9
10
11
void delay(float elapse)	// delay for elapse seconds
{
    long i,j;
    float delta=1.0e-3;
	HANDLE timerobject;

    i= (long)(elapse/delta);	// interval in milisec
    timerobject = CreateWaitableTimer(NULL,TRUE,NULL);
	WaitForSingleObject(timerobject,i);
	CloseHandle(timerobject);
}



1
2
3
4
5
6
7
8
9
10
11
void step_125(void) // this is Z-axis stepping motor
{
	int i;
	outbyte_isa(V_PORT_C,0x00);
	for(i=0;i<STEPNUMBER;i++){
		outbyte_isa(V_PORT_C,0x20);  // toggle the 5th bit in port C
		delay(0.1);                  // wait for 0.1 sec 
		outbyte_isa(V_PORT_C,0x00);
	    delay(0.1);
    }
}
Last edited on
To understand what your delay function does, have a look here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682492(v=vs.85).aspx
and subsequently here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx

If I understand correctly (I am not sure) your application seems to depend on windows to tell it when the delay is over. If there is a good reason for this you should keep it, but otherwise you might want to check out the possibilities of std::thread and std::chrono to implement the delay in your program and thus keep it more independent of the operating system.

You could start here for example:
http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
Last edited on
is there a good reason to choose std::chrono vs Sleep() function?

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx
Topic archived. No new replies allowed.