Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use a counter for time measurement with NI-DAQmx and C++?

Hi,

I need my C++ program to read time (number of elapsed ticks) using the counter on PCI-6229. I had this written and working for the non-MX driver, and now I need to re-write it for NI-DAQmx.

Here are the steps that I want to accomplish:

1. Route the 80MHz timebase to the input of "Dev1/ctr0"

2. Start the counter and let it count the ticks.

3. After a while, read the number of ticks from the counter.

Here is how I am trying to do it now (without success):

           DAQmxCreateTask ( "" , &taskHandle ) ;
           DAQmxCreateCICountEdgesChan ( taskHandle , "Dev1/ctr0" , "" , DAQmx_Val_Rising, 0 , DAQmx_Val_CountUp ) ;
           DAQmxConnectTerms ( "/Dev1/80MHzTimebase" ,  "/Dev1/Ctr0Source" , DAQmx_Val_DoNotInvertPolarity ) ;
           DAQmxStartTask ( taskHandle ) ;
           then DAQmxReadCounterScalarU32 ( taskHandle , 0.25 , &curCount , NULL ) ;
           and DAQmxClearTask ( taskHandle ) ;

No errors are generated, but I do not see the tick count being incremented when I repeatedly read the counter.

Advice will be greatly appreciated!!


0 Kudos
Message 1 of 5
(4,709 Views)
Got it, the timebase had to be routed to "PFI8" instead of "Ctr0Source".
0 Kudos
Message 2 of 5
(4,679 Views)

Hi Peter,

Welcome to the forums!   I just want to make sure I understand what you are trying to do.  You would like to count the rising edges of the 80 MHz clock using DAQmx in C++.  In order to route the timebase to the counter, you need to use the signal name (PFI8) instead of the counter (Ctr0Source) for the counter source input.  This name is found in Measurement Automation Explorer (MAX) by right-clicking on the device and choosing “Device Pinouts.”  This will physically put the 80 MHz clock on the PFI8 line, so make sure you reset the device or disconnect the terminals after you are done. 

That being said, may I ask what you are trying to do by reading the number of elapsed ticks?  If you are trying to monitor a hardware timed pulse, it would be better to count the frequency or period of that pulse.  This would provide better accuracy, because you are not relying on how fast DAQmx Read can poll the hardware.  If you are simply looking for a software based timer, I would recommend taking a look at this MSDN forum post. 

Using the GetTickCount function instead will free up your counter and provide comparable accuracy to counting the edges of the timebase.

I hope that helps you out, let me know if you have any further questions about this. Good luck with your project.

Rod T.
0 Kudos
Message 3 of 5
(4,677 Views)
Hi Roderick,

Yes, the idea is to write a more-or-less precise timer. It does work if the timebase is routed to "Dev1/PFI8", as you are saying.
I've always used the counters on NI cards for this purpose, but QueryPerformanceTimer() API from windows looks like a good alternative. Thank you very much for your detailed response!

Peter
0 Kudos
Message 4 of 5
(4,667 Views)
"QueryPerformanceCounter()", rather.
0 Kudos
Message 5 of 5
(4,664 Views)