Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

NIdaqMX: how to gate CTR0 from Sample Clock?

Hi All,

Environment: NI-DAQmx, ANSI C-API, PCI-6229 card

I'm trying to do continuous AIN voltage readings while doing buffered edge counting on CTR0, and I'm trying to synchronize the counter readings with my AIN readings, but so far am not having much luck. I thought that since CTR0 requires an external clock for it's gate, I could route the "/Dev1/ai/SampleClock" signal to RTSI0, and then use RTSI0 as the gate for counter 0. So far, I'm not having any success. I can read many samples from the AIN task, but I always get 0 samples from the CTR task (acting as if the counter is not being gated).

What am I doing wrong here? I've attached a simple console test app below...

Thanks in advance for any help!

-bill





// console_6229.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "NIDAQmx.h"
#include "conio.h"
#include "iostream"

using namespace std;


void ErrText(int err, char *s, int siz){
if (err==0) return;
DAQmxGetExtendedErrorInfo (s,siz);
return;
}


int _tmain(int argc, _TCHAR* argv[])
{
TaskHandle h_AIN=0;
TaskHandle h_CTR=0;
int err;
char s[512];
int16 readArray1[10000];
uInt32 ctrArray_0[10000];
int32 sampsPerChanRead1=0;
int32 ctrPerChanRead_0=0;


err=DAQmxResetDevice("Dev1");
ErrText(err,s,sizeof(s));

err=DAQmxGetDevProductType("Dev1",&s[0],511); // const char device[], char *data, uInt32 bufferSize);
ErrText(err,s,sizeof(s));

cout << "Device type = " << s << endl;

err=DAQmxCreateTask("",&h_AIN); // create a task for the AINS
err=DAQmxCreateTask("",&h_CTR); // create a task for counter 0

__try
{
err=DAQmxCreateAIVoltageChan(h_AIN,"Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NULL);
ErrText(err,s,sizeof(s));

err=DAQmxCfgSampClkTiming (h_AIN, "",1000,DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000);
ErrText(err,s,sizeof(s));

// setup the counters to do buffered edge counting
err=DAQmxCreateCICountEdgesChan (h_CTR,"Dev1/ctr0","",DAQmx_Val_Rising,0,DAQmx_Val_CountUp);
ErrText(err,s,sizeof(s));

err=DAQmxExportSignal (h_AIN, DAQmx_Val_SampleClock,"RTSI0");
ErrText(err,s,sizeof(s));

err=DAQmxCfgSampClkTiming (h_CTR, "RTSI0",1000,DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000);
ErrText(err,s,sizeof(s));

// indicate that we will start the tasks in software (as opposed to a hardware trigger)
err=DAQmxDisableStartTrig(h_AIN);
ErrText(err,s,sizeof(s));

// start the slave tasks first (this "arms" the counter tasks)
err= DAQmxStartTask(h_CTR);
ErrText(err,s,sizeof(s));

// finally start the master tasks
err= DAQmxStartTask(h_AIN);
ErrText(err,s,sizeof(s));

while( !_kbhit() )
{
err=DAQmxReadBinaryI16(h_AIN, -1, 0,DAQmx_Val_GroupByScanNumber,&readArray1[0], 1000, &sampsPerChanRead1, NULL);
if((err) && (err!= -200284)) {
ErrText(err,s,sizeof(s));
}

err=DAQmxReadCounterU32(h_CTR,-1,0.0,&ctrArray_0[0],1000,&ctrPerChanRead_0,NULL);
ErrText(err,s,sizeof(s));

cout << "counter samples read=" << ctrPerChanRead_0;
cout << " AIN samples read=" << sampsPerChanRead1 << " \r";
Sleep(100);
}

_getch();
cout << endl;
cout << "finished - press a key..." << endl;
while( !_kbhit() ) {}
_getch();
}



__finally
{
DAQmxClearTask(h_AIN);
DAQmxClearTask(h_CTR);
}

return 0;
}
0 Kudos
Message 1 of 4
(3,927 Views)
Hello Bill-

I haven't tried running your application, but I suspect that the reason you are getting no samples on your counter task is because you don't have a signal connected to the input terminal (SOURCE) of the event counting counter. On M Series and TIO devices, if you have no SOURCE edges between GATEs (sample clock source for buffered edge counting) then the counter will not recognize any of the sample clock edges and will return no data. For situations such as this, you should turn on duplicate count prevention (DAQmxSetCIDupCountPrevent()). You can read more about duplicate count prevention in NI-DAQmx Help, but the jist of it is that this property allows you to have a SOURCE signal that is slower than your GATE signal. In traditional daq, this property was called "synchronous counting" for TIO devices.

Additionally, you shouldn't have to route the ai/SampleClock manually through RTSI. The driver should take care of this route when you simply select ai/SampleClock as the signal source ( ie-DAQmxCfgSampClkTiming (h_CTR, "ai/SampleClock",1000,DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000); )

I hope this helps!
gus....
Message 2 of 4
(3,914 Views)
Thanks for the suggestion Gus, I'll try it. I'm out of town this week, but will get back to it next week. You are correct in surmising that I don't have an input to the source pin (as this is just a test case with "throwaway" code). Before I left town, I was able to discover that the code *does* work on an E-series card - it doesn't work on an M-series card. Thanks for your reply and helpful suggestions!
-bill
0 Kudos
Message 3 of 4
(3,903 Views)
Gus,

Your suggestion was right on the mark - it now appears to work correctly. Thanks a million.

-bill
0 Kudos
Message 4 of 4
(3,866 Views)