04-25-2011 12:41 PM
Hi,
This might appear to be simple problem: we have a about 1kHz sine wave (0-5V), each pulse width might differ a little bit, and it has about 9500 cycles. The goal is to find out how many cycles (Low-to-High edges) are there during the 9.5 second scan. We are trying to use PCI 6071E as the counter, the sine wave signal is connected to GPCTR0_SOURCE. It seems to give wrong answers using the following program. Could anyone please suggest the correct way to do it? Thanks
/*********************************************************************
* ANSI C++ program: 24-bit digital counter
* To compile in cmd.exe "cl PeakCounter.cc /EHsc"
*********************************************************************/
#include <stdio.h>
#include "NIDAQmx.h"
#include <math.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#pragma comment(lib, "NIDAQmx.lib")
#pragma comment(lib, "odbccp32.lib")
#pragma comment(lib, "odbc32.lib")
using namespace std;
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int main(void)
{
int error=0;
TaskHandle taskHandle=0;
uInt32 data;
char errBuff[2048]={'\0'};
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateCICountEdgesChan(taskHandle,"Dev2/ctr0","",DAQmx_Val_Rising,0,DAQmx_Val_CountUp)); //pin -PCI6071E, initial=0
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
printf("Continuously polling. Press Ctrl+C to interrupt\n");
while( 1) {
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadCounterScalarU32(taskHandle,10.0,&data,NULL)); //read counter timeout 10sec
printf("\rCount: %d", data);
fflush(stdout);
}
Error:
puts("");
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}
Solved! Go to Solution.
04-26-2011 07:17 PM
Hi,
The troubles you are running into are likely coming because your program attempts to count edges on an analog sine wave signal. The best way to work around this might be to read the sine wave on an analog input channel and compare it to a threshold level of perhaps 4.5 Volts and increment a count everytime the signal surpasses this threshold.
The Knowledge Base article, Using an Analog Input for an Edge Count or Encoder Task Using DAQmx , discusses this issue and the idea I suggested above. The example code is written in LabVIEW, but the overall idea is the same and DAQmx commands are used. For an example of acquiring analog input with DAQmx check some of the example programs that ship with the software. (Start >>Programs >>National Instruments >> NI-DAQ >> Text Based Code Support >>ANSI C Examples)
I hope that helps!
04-27-2011 11:50 AM
I see. Then we will likely to use other devices as a counter. Thanks!
04-27-2011 05:40 PM - edited 04-27-2011 05:43 PM
If you do want to use the 6071E to count the periods of your sine wave, you can do so by configuring an analog trigger then counting the edges of the Analog Comparison Event. The Analog Comparison Event is a digital signal generated by the analog trigger circuitry, and will repeat continuously with your sine wave once the task has started. The downside is that you have to use your analog subsystem to create the trigger signal.
For an example see here.
*EDIT* I just noticed you are developing using the C API. If you do want to go this route and need help implementing it in the C API just let us know. The general idea is exactly the same as what I've described and what is implemented in the LabVIEW code that I pointed you to.
Best Regards,