10-22-2009 12:33 AM
Is there an API available in LabWindows to memorise clock ticks or calculate time lapsed?
It is needed for use with a do while loop. I want to perform an action repeatedly for 260milliseconds in a while loop and then come out of the while loop once the 260 milliseconds is up.
What is the best way to go about this?
Solved! Go to Solution.
10-22-2009 02:10 AM - edited 10-22-2009 02:13 AM
Depending on the precision you expect from timing, you may go from the simplest Timer () solution to more complex SDI APIs calls.
The simplest solution (nedds anything but CVI):
double tini;
tini = Timer ();
while (Timer () - tini < 0.26) {
doSometing ();
}
Advanced solution are the use of QueryPerformanceCounter () and QueryPerformanceFrequency () SDK functions. Here you can find an interesting discussion about them.This solution implies the use of the Windows SDK available in CVI full versions.
This code should work fine:
#include <windows.h>
void yourFunction (LARGE_INTEGER t) {
BOOLEAN error;
LARGE_INTEGER tini, tn, delta;
error = QueryPerformanceCounter (&tini);
if (!error) {
// Hardware error to handle someway
return;
}
do {
error = QueryPerformanceCounter (&tn);
delta.QuadPart = tn.QuadPart - told.QuadPart;
if (delta.LowPart >= t.LowPart)
break;
doSomething ();
} while (1);
return;
}
10-22-2009 02:19 AM
additionnally, if you are comfotable with asynchronous execution, you can use the asynchronous timer instrument provided with CVI (look for NewAsyncTimer() in the CVI documentation), or you can use the almost equivalent Windows SDK function CreateWaitableTimer().
i would recommend the use of an asynchronous API, because it does not lock the program flow in one single function, so the resulting software is more flexible if you need to add another feature later.