Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I Synchronize two counters in CVI using NI-DAQmx

Hi Tom,

Thanks again for all your suggestions. I am starting to understand the way to use the DAQmx libraries.
I tried routing the intrenal output from counter 0 to the gate of counter 1 using your suggestion, and I do not get any error this time.
I have set counter 1 to count edges from the default input terminal, and I use a 1 kHz signal from the oscilloscope which has 5.0 volt amplitude. This is my signal at this stage, and I want to count edges from it during the GATE generated by counter 0.
Counter 1 is clearly counting edges from the 1kHz signal, however, it seems to ignore the gate pulse sent by counter 0 and it continues to count beyond it.
I know this because I start the counting task on counter 1, which should not increment the count value untill the Gate task is executed, then once the gate is over, I expect the counter 1 to retain the value of count reached.
Then I I wait for a variable delay time to mage sure that the gate is over.
Then I reads counter 1, and I always get a count that depends o the delay time that I use.
It seems to me that counter 1 is enbles regardless of the gate that I am trying to use to control its counting activity. DO I need to set something else in counter 1 in order to accept the gate from counter 0?

Here is a callback that I use to call the tasks and the two task definitions:

int CVICALLBACK Start (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
TaskHandle THgate = 0, Counting = 0;
double dGate_in_ms=0.001;
unsigned long ulcount=0;
double dfreq=0.0;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal (panel0, PANEL_NUMERIC_Gate, &dGate_in_ms);
// set counter 1 to count edges:
CreateCounter(&Counting);
// set counter 0 to generate a gate pulse for gating counter 1
CreateGatePulse(&THgate,dGate_in_ms/dms_per_s);

// route the ouptput of counter 0 to the gate terminal of counter 1
DAQmxConnectTerms ("/Dev1/Ctr0InternalOutput", "/Dev1/Ctr1Gate", DAQmx_Val_DoNotInvertPolarity);


/*Start counting on counter 1. The count will remain in zero until the gate
provided by counter 0 arrives*/
DAQmxStartTask(Counting);

/* here comes the gate pulse, which enables counter 1 for
counting edges on counter 1 source default terminal*/
DAQmxStartTask(THgate);

//Wait for the gate task to be complete, and read the count at counter 1:
Delay (0.1);

/*Read count value on counter 1. The gate is over, so the count remains at
the value it reached at the end the gate*/
DAQmxReadCounterScalarU32 (Counting, 10.0, &ulcount, NULL);

//Stop the tasks
DAQmxStopTask(Counting);
DAQmxStopTask (THgate);


//calculate the frequency:
SetCtrlVal (panel0, PANEL_NUMERICCountReached, (unsigned int)(ulcount));
dfreq=((double)(ulcount))*(double)(dms_per_s)/(double)(dGate_in_ms) ;

SetCtrlVal (panel0, PANEL_NUMERIC_Freq, dfreq);
//Disconect the terminals
DAQmxDisconnectTerms ("/Dev1/Ctr0InternalOutput", "/Dev1/Ctr1Gate");
//Clear the tasks

DAQmxClearTask(Counting);
DAQmxClearTask (THgate);

break;
}
return 0;
}

int32 CreateGatePulse(TaskHandle *taskOut1,double dgate_time)
{
unsigned long ulTicksGate=2;
int32 DAQmxError = 0;
TaskHandle taskOut;

DAQmxErrChk(DAQmxCreateTask("GatePulse", &taskOut));
ulTicksGate=(unsigned long)(dgate_time*10000000.0);
DAQmxErrChk(DAQmxCreateCOPulseChanTicks (taskOut, "Dev1/ctr0", "", "/Dev1/10MHzRefClock", DAQmx_Val_Low, 0.0, 2,ulTicksGate));

*taskOut1 = taskOut;

Error:
return DAQmxError;
}


int32 CreateCounter(TaskHandle *taskOut2)
{
unsigned long ulTicksGate=2;
int32 DAQmxError = 0;
TaskHandle taskOut;
DAQmxErrChk(DAQmxCreateTask("Counter", &taskOut));
DAQmxErrChk(DAQmxCreateCICountEdgesChan (taskOut, "Dev1/ctr1", "Counter", DAQmx_Val_Rising, 0, DAQmx_Val_CountUp));
*taskOut2 = taskOut;
Error:
return DAQmxError;
}
0 Kudos
Message 11 of 13
(1,370 Views)

Hi Fundadero-

In order to gate the second counter (ctr1) you actually need to specify a pause trigger for the task.  There are three necessary parameters that must be set.  These are:

 

  DAQmxErrChk (DAQmxSetPauseTrigType(taskHandle, DAQmx_Val_DigLvl)); 
  DAQmxErrChk (DAQmxSetDigLvlPauseTrigSrc(taskHandle, "/Dev1/Ctr0InternalOutput")); 
  DAQmxErrChk (DAQmxSetDigLvlPauseTrigWhen(taskHandle, DAQmx_Val_Low));

 

I have attached a modified CVI example that counts edges as gated by the internal output of counter 0 using this method.  Please let us know if you have any additional questions.

Thanks and have a good weekend-

Message Edited by Tom W. on 07-01-2005 02:37 PM

Tom W
National Instruments
0 Kudos
Message 12 of 13
(1,360 Views)
Tom,

Thanks for all your help and gidence. This works beautifully.


Fundadero
0 Kudos
Message 13 of 13
(1,353 Views)