LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Timing loops

This is a quick question that I need advice on. I am loading a counter with a different value at a specific frequency. Presently I am using a basic while loop with a delay of the time step to get the frequency. Is this an effective method to perform this task or should I be doing something else? I need this task to be as precise as possible as the frequency will be quite high. I am open to any suggestions (code is simplified)

while (iStop != 1 && iStep <= 5000)
{
LoadValue = iLoadValues[iStep]; /* Load Counter */
iStep++;
ProcessSystemEvents();
Delay(dTimeStep);
}

Thank you for any assistance you provide.

RED
0 Kudos
Message 1 of 5
(3,481 Views)
My opinion, and I'm not an expert here.

The moment you said "as precise as possible" you lost it. It won't be, particulary since you are calling ProcessSystemEvents(), which will take a widely variable period of time.

The best you can do in LabWindows is to run a function like this in a separate thread, with no call to ProcessSystemEvents.

Some code like:
timerFlag = 1;
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, TimerThread, NULL, NULL);


int CVICALLBACK TimerThread(void *ctrlID)
{
while(timerFlag)
{
Your code, without ProcessSystemEvents()
}
}
0 Kudos
Message 2 of 5
(3,472 Views)
Thanks hendra,

Exactly what I thought. I've been trying to read up on multitreading as I have never done it before and it appeared to be a possible solution. Thanks for your suggestion (expert or not). My main concern is the timing so I'll give it a go. If others have opinions I take all the help I can get.

Cheers,

RED
0 Kudos
Message 3 of 5
(3,468 Views)
Hi RED,

Try using asynchronous timers (see asynctmr.fp and associated example programs). The callbacks execute in a separate, high-priority thread.

However, don't expect reliable timing if dTimeStep is under about 10 milliseconds.

Colin.
0 Kudos
Message 4 of 5
(3,463 Views)
Thanks Colin,

I'll try it soon. I've heard mention of the 10 millisecond rule before, thanks for the heads up.

Cheers,

RED
0 Kudos
Message 5 of 5
(3,440 Views)