# LabVIEW

cancel
Showing results for
Did you mean:

## timer overshoot

Good day

I have written code to produce a squarewave with varying frequency, and a second squarewave with constant frequency. I am outputting these squarewaves to the soundcard and routing it to the USB-6009 PFI0 counter input to count the number of edges output from the soundcard. I want the sequence of events to be as follows:

1. output the varying squarewave and count the edges.

2. when the edge count reaches 1000, break out and immediately output the constant squarewave.

3. similarly count the constant squarewave edges until the count reaches 1000 and switch back to the varying squarewave, and so on...

I have used a case structure to switch between the varying and constant squarewaves, and set  up conditions to check if the counter has reached 1000 edges. The problem is that the counter sometimes overshoots the condition check, i.e. continues counting to greater than 1000 steps, thus leaving the code in the current case and not switching to the next case.

Is there a way to improve this code such that the condition is immediately recognised and the case switches at exactly 1000 steps? I have attached my VI...

Message 1 of 8
(3,299 Views)

## Re: timer overshoot

First, make sure you use the same representation for all your constants.  If you made your 1000 constants a U32 datatype instead of I32, you wouldn't have a coercion dot on the equals? comparison.

Are you certain that you only get 1 tick or less of increase on the counter for each iteration of the loop?  Suppose you have 999 on your counter, then the next iteration 2 edges coming in making the count 1001.  The value will never equal 1000 thus it will never stop.  Perhaps you should use >= on your comparison.

Message 2 of 8
(3,282 Views)

## Re: timer overshoot

It seems that the problem is the latter. On the first iteration of the inner most while loop, the edge count is approx. 450, and on the second iteration, the edge count is approx. 1020. It is important that the edge count does not exceed 1000, so i do not want to use a greater than or equal to comparison...

I also checked the sound() command by extending the duration of the sound such that it would produce twice as many edges when played to its full length. The result was that when the edge count reached 1000 the sound kept playing until it completed its full duration. In addition to the above problem, is there a way to cut the sound short while it is playing? There is a VI called Sound Output Stop, but i have written my sound() command in Mathscript since the LabVIEW sound operations produce a lot of noise and unwanted pulses.

I have already tried a predictive approach whereby i calculate the waveform required to produce only 1000 steps and output that. There is a large error in the sound production that is random and variable, thus i would prefer to perform the above operations in real time to react to the actual output.

Message 3 of 8
(3,267 Views)

## Re: timer overshoot

Please try putting a Wait in your "count squarewave edges" while loop.
Regards,
Claire Reid
National Instruments
Message 4 of 8
(3,240 Views)

## Re: timer overshoot

Introducing a wait function into the "count squarewave edges" while loop reduces the rate at which the while loop checks the edge count. I surely need it to check the count more often so that it switches case as soon as it counts edge number 1000, rather than missing it between checks as it seems to be doing... I have been told that the windows clock is limited to a 100ms period, i.e. the while loop is restricted to loop at this rate. Is there a way around this to increase the speed of the while loop, and hence the edge count check? For high-speed data acquisition it makes no sense to be limited by an operating system configured for more trivial tasks...
Message 5 of 8
(3,203 Views)

## Re: timer overshoot

Are you doing hardware or software timing? Also, what is the frequency of this squarewave?
Regards,
Claire Reid
National Instruments
Message 6 of 8
(3,181 Views)

## Re: timer overshoot

I am using software timing through LabVIEW8.2. The variable squarewave comprises frequencies between 0 and 7400Hz. The constant squarewave is at 5000Hz.
Message 7 of 8
(3,167 Views)

## Re: timer overshoot

Please see the post on this forum from AltenBach discussing while loops in a Windows OS.

Regards,
Claire Reid
National Instruments
Message 8 of 8
(3,134 Views)