Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

How to confugure PCI 6071E as a counter

Solved!
Go to solution

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;
}

0 Kudos
Message 1 of 4
(3,730 Views)

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!

 

 

 

Matt
NI Community Team
National Instruments
0 Kudos
Message 2 of 4
(3,700 Views)

I see. Then we will likely to use other devices as a counter. Thanks!

0 Kudos
Message 3 of 4
(3,695 Views)
Solution
Accepted by topic author eLions

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,

John Passiak
0 Kudos
Message 4 of 4
(3,687 Views)