Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

digital counter using USB 6008 DAQ board

I am using NI USB-6008 board to count output from channel A of the Bourns digital encoder (100 cycles per revolution) .

I compiled example cntDigEvents from /Applications/National Instruments/NI-DAQmx Base/examples/ directory on Mac OS 10.3 and board is connected to USB -1 port

I am using all default values from the example below.

If I am rotating encoder until program stops, I am getting some values but when I am rotating it only for several seconds it shows 0 (please see below):
It appears that I also have about 40 seconds lag during the program start and record execution.

Thanks,

andrei


Tephra:~/Applications/National Instruments/NI-DAQmx Base/examples/mlt avk$ make
gcc -I../../includes -O2 cntDigEvents1.c -framework nidaqmxbase -framework nidaqmxbaselv -framework "LabVIEW 7.0 Runtime" -o cntDigEvents1
Tephra:~/Applications/National Instruments/NI-DAQmx Base/examples/mlt avk$ ./cntDigEvents1
Count: 0
Tephra:~/Applications/National Instruments/NI-DAQmx Base/examples/mlt avk$ make
gcc -I../../includes -O2 cntDigEvents1.c -framework nidaqmxbase -framework nidaqmxbaselv -framework "LabVIEW 7.0 Runtime" -o cntDigEvents1
Tephra:~/Applications/National Instruments/NI-DAQmx Base/examples/mlt avk$ ./cntDigEvents1
Count: 232
Tephra:~/Applications/National Instruments/NI-DAQmx Base/examples/mlt avk$ ./cntDigEvents1
Count: 87
Tephra:~/Applications/National Instruments/NI-DAQmx Base/examples/mlt avk$ ./cntDigEvents1
Count: 84



//************ cntDigEvents file
#include "NIDAQmxBase.h"
#include
#include

#define DAQmxErrChk(functionCall) { if( DAQmxFailed(error=(functionCall)) ) { goto Error; } }

static int gRunning;

int main(int argc, char *argv[])
{
// Task parameters
int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048]={'\0'};
time_t startTime;

// Channel parameters
char chan[] = "Dev1/ctr0";

// Data read parameters
uInt32 data;
float64 timeout = 10.0;


DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxBaseCreateCICountEdgesChan(taskHandle,chan,"",DAQmx_Val_Falling,0,DAQmx_Val_CountUp));
DAQmxErrChk (DAQmxBaseStartTask(taskHandle));
gRunning = 1;
// The loop will quit after 10 seconds
startTime = time(NULL);
while( gRunning && time(NULL) DAQmxErrChk (DAQmxBaseReadCounterScalarU32(taskHandle,timeout,&data,NULL));
printf("\rCount: %d",data);
}

Error:
puts("");
if( DAQmxFailed(error) )
DAQmxBaseGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
DAQmxBaseStopTask(taskHandle);
DAQmxBaseClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmxBase Error: %s\n",errBuff);
return 0;
}
0 Kudos
Message 1 of 4
(4,210 Views)
Hello Andrei.

So your counter data is lagging 40 seconds? Do you have multiple USB devices in one computer? Disconnect everything but the USB 6008 and your mouse and keyboard. Your program looks like it shouldn't have the delay so I tried running it, but there are many errors. Is this the most recent copy? Please let me know if you have any questions.

Have a great day!
Marni S.
0 Kudos
Message 2 of 4
(4,205 Views)
Hi Mami.

The only other USB devices I have are mouse and a keyboard.

Looks like it takes about 30 seconds for DAQ library funcrion call : DAQmxErrChk (DAQmxBaseCreateTask("port29",&taskHandle));

what kind of errors you are getting?

This is the recent test code:
It looks like once board is ready it works without delay...
Thanks a lot,

andrei
//************************cut below this line ***********

*
* ANSI C Example program:
* melter.c
*
* Example Category:
* CI
*
*********************************************************************/
#include "NIDAQmxBase.h"
#include
#include

#define DAQmxErrChk(functionCall) { if( DAQmxFailed(error=(functionCall)) ) { goto Error; } }

static int gRunning;

int main(int argc, char *argv[])
{
// Task parameters
int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048]={'\0'};
time_t startTime;

// Channel parameters
char chan[] = "Dev1/ctr0";
char name[20];
// Data read parameters
uInt32 data=0;
uInt32 startCounterValue = 0;
float64 timeout = 0.0;
printf("Initializing DAQ board\n");
DAQmxErrChk (DAQmxBaseCreateTask("port29",&taskHandle));
printf("Board is ready, Type S (start) and press enter key\n");
scanf("%s",&name);
DAQmxErrChk (DAQmxBaseCreateCICountEdgesChan(taskHandle,chan,"",DAQmx_Val_Falling,startCounterValue,DAQmx_Val_CountUp));
printf("READY2\n");
DAQmxErrChk (DAQmxBaseStartTask(taskHandle));
gRunning = 1;
// The loop will quit after 5 seconds

//C function gets time
startTime = time(NULL);
int counter1 = 1;

printf("READY\n");
while(counter1 <= 100) {

DAQmxErrChk (DAQmxBaseReadCounterScalarU32(taskHandle,timeout,&data,NULL));
printf("Iteration: %d\n",counter1);
printf("Count: %d\n",data);
//printf("Time: %d\n",time());
counter1++;
}
printf("Will run test for 10 seconds\n");
// The loop will quit after 10 seconds
startTime = time(NULL);
while( gRunning && time(NULL) DAQmxErrChk (DAQmxBaseReadCounterScalarU32(taskHandle,timeout,&data,NULL));
printf("\rCount: %d",data);
}

counter1 = 1;
while(counter1 <= 100) {

DAQmxErrChk (DAQmxBaseReadCounterScalarU32(taskHandle,timeout,&data,NULL));
printf("Iteration: %d\n",counter1);
printf("Count: %d\n",data);
//printf("Time: %d\n",time());
counter1++;
}

//*DAQmxErrChk (DAQmxBaseClearTask(taskHandle));*/
Error:
puts("");
if( DAQmxFailed(error) )
DAQmxBaseGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
DAQmxBaseStopTask(taskHandle);
DAQmxBaseClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmxBase Error: %s\n",errBuff);
return 0;
}
0 Kudos
Message 3 of 4
(4,173 Views)
Hello Andrei,

Do you get this delay when you run on of the examples that come with the NI-DAQmx Base driver? (C:\Program Files\National Instruments\NI-DAQmx Base\Examples) I got a little bit of a delay with the examples but it was only about 10 seconds or so. On your example, try DAQmxErrChk (DAQmxBaseCreateTask("",&taskHandle)) instead of DAQmxErrChk (DAQmxBaseCreateTask("port29",&taskHandle)). Does this speed it up at all? Please let me know if you have any questions. Have a great day!

Sincerely,
Marni S.
0 Kudos
Message 4 of 4
(4,158 Views)