Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

USB-CTR Series cbDaqInScan Timing Mode C++ example

Expand / Collapse
 

USB-CTR Series cbDaqInScan Timing Mode C++ example


The following example program demonstrates (using Microsoft C) how to continuously read a counter channel and the digital port simultaneously. The counter is set to Timing mode which means the count starts on the input and stops on the gate. The counts returned represent ticks of a clock, to convert to time the count was multiplied by the time indicated by the tick size constant in the cbCConfigScan function. The code is listed below with comments. The full project (MS Visual Studio 2008) attached below.

Also demonstrated is device discovery which doesn't require the user to run InstaCal. InstaCal must still be installed because doing so installs the device drivers. At the bottom of this article you will find a Zip file that contains the Visual Studio 2008 project.

Program Output





// VC-2008_USB_CTR_TimingMode_DaqInScan.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"

/* Include files */
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "cbw.h"

#define MAXNUMDEVS 100 //used for device descovery
#define CHANCOUNT 8 //total number of config array channels that make up two unsigned long channels
#define CHANCOUNT64BIT 2 //total number of 64 bit channels

#define BUFFSIZE 1000 * CHANCOUNT // buffer size
#define HALFBUF BUFFSIZE / 2
#define RATE 50 //sample rate

#define COUNTER 0
#define OutTimerNum 0
#define DebounceTime CTR_DEBOUNCE_NONE
#define DebounceMode CTR_TRIGGER_AFTER_STABLE
#define EdgeDetection CTR_RISING_EDGE
#define TickSize CTR_TICK20833PT3ns    // 0 equates to 20.833 uS ticksize
#define tick 0.000020833f

void main ()
   {
   /* Variable Declarations */
 
   int BoardNum = -1; 
   int ULStat = 0;
   long rate = RATE;
   long Pre = 0;
   long count = BUFFSIZE;
   long curCount,curIndex;
   short Status;

   HGLOBAL MemHandle = 0;
   ULONGLONG *ADData = NULL;

   short ChanArray[CHANCOUNT];  // DaqInScan configuration arrays
   short ChanTypeArray[CHANCOUNT];
   short GainArray[CHANCOUNT];

   int numberOfDevices = MAXNUMDEVS;
   DaqDeviceDescriptor inventory[MAXNUMDEVS];
   DaqDeviceDescriptor DeviceDescriptor;

   float Rev = (float)CURRENTREVNUM;
   ULStat = cbDeclareRevision(&Rev);
   cbErrHandling(PRINTALL, STOPALL);

   printf ("Demonstration to read counter 0 using Timing mode and digital input port\n\n");
//Ignore InstaCal device discovery
cbIgnoreInstaCal();

//locate USB devices
ULStat = cbGetDaqDeviceInventory(USB_IFC, inventory, &numberOfDevices);
for(int i = 0; i < numberOfDevices; i++)
{
DeviceDescriptor = inventory[i];

//Product ID for USB-CTR04 = 0x12E
//Product ID for USB-CTR08 = 0x127

//Product IDs can be found in ULProps.txt located in 
// C:\Program Files (x86)\Measurement Computing\DAQ

if(DeviceDescriptor.ProductID == 0x127)
{
BoardNum = i;
ULStat = cbCreateDaqDevice(BoardNum, DeviceDescriptor);
break;
}
}

   if(BoardNum < 0)
   {
   printf("USB device not found...press any key to exit\n");
   getch();
   return;
   }
   
   //put counter in TIMING mode and invert gate for stop on falling edge; use 20.83uS tick size
   if(cbCConfigScan( BoardNum,
0,
TIMING+TIMING_MODE_INVERT_GATE,
CTR_DEBOUNCE_NONE,
CTR_TRIGGER_AFTER_STABLE,
CTR_RISING_EDGE,
CTR_TICK20833PT3ns ,
0)) printf("Error Code %d",ULStat);


//set Digital port to input mode
    if(cbDConfigPort( BoardNum,
AUXPORT,
DIGITALIN)) printf("Error Code %d",ULStat);


 
   //Channel configuration arrays for cbDaqInScan; the default is 64 bit; this means that each channel must
   //must have four pseudo channels.
   ChanArray[0] = 0;
   GainArray[0] = NOTUSED;
   ChanTypeArray[0] = CTRBANK0;

   ChanArray[1] = 0;
   GainArray[1] = NOTUSED;
   ChanTypeArray[1] = CTRBANK1;

   ChanArray[2] = 0;
   GainArray[2] = NOTUSED;
   ChanTypeArray[2] = CTRBANK2;

   ChanArray[3] = 0;
   GainArray[3] = NOTUSED;
   ChanTypeArray[3] = CTRBANK3;


   ChanArray[4] = AUXPORT;
   GainArray[4] = NOTUSED;
   ChanTypeArray[4] = DIGITAL8;

   ChanArray[5] =  AUXPORT;;
   GainArray[5] = NOTUSED;
   ChanTypeArray[5] = PADZERO; //Needed for 64 ULONGLONG arrary

   ChanArray[6] =  AUXPORT;
   GainArray[6] = NOTUSED;
   ChanTypeArray[6] = PADZERO; //Needed for 64 ULONGLONG arrary

   ChanArray[7] =  AUXPORT;
   GainArray[7] = NOTUSED;
   ChanTypeArray[7] = PADZERO; //Needed for 64 ULONGLONG arrary

   //Allocate data buffer for the driver
   MemHandle = cbWinBufAlloc64(BUFFSIZE); 
   //get access to the buffer using a pointer
   ADData = (ULONGLONG*)MemHandle;


   //use cbDaqInScan to simultaneously read both the counter and the digital port
   if(cbDaqInScan ( BoardNum, 
ChanArray,
ChanTypeArray,
GainArray,
CHANCOUNT,
&rate,
&Pre,
&count, 
MemHandle, 
BACKGROUND + CONTINUOUS))printf("Error Code %d",ULStat);

   while(!_kbhit())
   {
if(cbGetStatus(BoardNum,&Status,&curCount,&curIndex,DAQIFUNCTION))
{
printf("Error Code %d\n",ULStat);
break;
}

if(curIndex != -1)
printf ("%2.4lf sec\t%llu\n", (double)(ADData[curIndex/4] * tick),ADData[(curIndex + 4)/4]);


Sleep(100);
   }
   getch();
   //stop task and free up memory
   cbStopBackground(BoardNum,DAQIFUNCTION);
   cbWinBufFree(MemHandle);
   printf("Completed...press any key to exit\n"); 
   getch();
}




Rate this Article:

Attachments



Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50728

Last Modified:10/5/2017 1:52:27 PM

Article has been viewed 3,050 times.

Options