From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using a counter to create a timing delay

Hi all,

 

I am working on an application where I need to send some digital outputs seperated by precise timing delays.

 

So for example, I may want to output a 'high' to channel 1, then have a 20ms delays, then output a 'high' to channel 2, then have a 50ms delay, and so on.

 

I want the timing delays to be easily adjustable and I would like them to be precise.

 

The program is being set up for hundreds of tests and it will be run once per test. In between tests I will have the opportunity to modify timing delays.

 

I already have a cDAQ-9174 chassis and am planning to buy a NI 9401 high speed digital I/O card. It has been suggested to me that I use a counter (which is built into the chassis) to generate the timing delays, which sounds like a good idea to me. However, I don't really understand how to access the counters and configure the 'DAQmx create channel' function because there are so many options for the reference clock which I don't understand.

 

I am thinking that I need to implement something like this:

 

http://zone.ni.com/reference/en-XX/help/370466V-01/mxcncpts/hwtimedcounter/

 

However I'm if this is what I need and I'm not sure how I would go about testing this code. I guess I would need to connect some sort of output to the counter to see if it's working correctly.

 

Any help would be much appreciated.

 

 

0 Kudos
Message 1 of 11
(4,998 Views)

I have had a go at creating a basic counter Labview program and it does not work as I expect. Perhaps I am missing an important concept. I have attached a screenshot of my program. As you can see it's a basic 'CO-pulse generation-tick' function with a 20Mhz timebase. I am using cycles of 2 high ticks and 2 low ticks, so I expect that I should get 5000 cycles per second (20,000/(2+2)).In the while loop I divide the loop counter by 5000 so that 'quotent' should increment one per second. When I run my program for 60 seconds this is what the final values are:

 

Real time elapsed: 60s

Loop counter: 67,870,363

Quotent: 13

 

As you can see, I would expect quotent to be 60, but it is 13. Can anybody explain this to me?

0 Kudos
Message 2 of 11
(4,973 Views)

Just noticed that the attachment wasn't attached! Fixed.

 

 

counter example.PNG

0 Kudos
Message 3 of 11
(4,937 Views)
Your calculator is broken or you need to check your basic arithmetic skills. The loop counter divided by 5000000 is 13.574. The counter task has absolutely nothing to do with how fast the while loop iterates. Where did you get idea that it did?
0 Kudos
Message 4 of 11
(4,921 Views)

I thought it was similar to a digital output, where the 'Is task done' function waits untill an output is sent, and then iterates. By the sounds of it I am mistaken.

 

So then how do I track the counter? I would like to have the counter count up to 100,000 (with a 20MHz timebase, to give me a 5ms delay) and then so something else in the program.

0 Kudos
Message 5 of 11
(4,902 Views)

Hi Marc,

 

you should output a FINITE number of pulses…

 

Why not use a simple Wait function instead?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 11
(4,886 Views)

Hi GerdW,

 

Thanks for the tip. I am having a bit more success with the finite setting for the counter.

 

The reason I want to use a counter over a simple 'wait' function is because I want precise timing, as I mentioned in my first post. An engineer from NI recommended that I do it this way.

 

I have currently implemented this:

 

https://decibel.ni.com/content/docs/DOC-12165

 

However, because there is no option to select the physical clock, I am not sure if this is a physical clock or a software clock that is being used? I would appreciate any comments on this.

 

Ultimately, I want the counter to physically count a finite number of pulses and correlate that to a precise time that I can use for the delay in my program.

0 Kudos
Message 7 of 11
(4,846 Views)

After a bit of experimenting, now I'm not sure if the hardware time delay has any advantage over the software delay. I can get a constant 5ms delay using the 'wait' function, yet when I use the counter I can normally get a 5ms delay, but sometimes (perhaps one trial out of five) I get a 4 or 6ms delay. I have even seen a 1ms delay using the counter method. Perhaps my method for measuring is flawed though? I have attached some screenshots of my program. If anybody has any comments it would be appreciated.

 

my counter task front pannel.PNG

 

my counter task.PNG

0 Kudos
Message 8 of 11
(4,837 Views)

HI Marc,

 

your method of measuring time is highly flawed!

- Remove all those locals and use wires instead.

- You only read the time with a 1ms resolution. Due to rounding errors you can easily get 4, 5, and 6ms as result of your "measurement"!

- LabVIEW also provides a high resolution timer, found at vi.lib/utility/HighResolutionRelativeSeconds.vi

- Measuring times in the order of just several ms is also tricky in a multitasking environment like Windows…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 11
(4,831 Views)

Thanks GerdW, I will try your suggestions tomorrow. Do you think that in theory using a hardware counter is a more precise and repeatable way of getting my required time delay as opposed to the software 'wait (ms)' function?

 

I'm aware that the best way to do it would be to use an FPGA or a cRIO with labview RT, but I am working with the hardware which I have, which is a cDAQ chassis.

0 Kudos
Message 10 of 11
(4,814 Views)