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

Linux C duty cycle example for USB-CTR04/08

Expand / Collapse
 

Linux C duty cycle example for USB-CTR04/08


This is a product specific USB-CTR04 and USB-CTR08 C example for Linux. It configures channel 0 for pulsewidth mode and channel 1 for period mode. Because a duty cycle measurement is pulsewidth divided by period, duty cycle is display along with pulsewidth and period. Attached at the bottom of this article is the C code file.

/*
    UL call demonstrated:          ulCInConfigScan()

    Purpose:                          Performs a continuous scan of the
                                      specified  channels

    Demonstration:                    Pulsewidth & Period modes


    Steps:
    1. Call ulGetDaqDeviceInventory() to get the list of available DAQ devices
    2. Call ulCreateDaqDevice() to to get a handle for the first DAQ device
    3. Call ulConnectDaqDevice() to establish a UL connection to the DAQ device
    4. Call ulCConfigScan() to configure the channels
    5. Call ulCInScan() to start the scan of channels
    6. Call ulCInScanStatus() to check the status of the background operation
    7. Display the data for each channel
    8. Call ulCInScanStop() to stop the background operation
    9. Call ulDisconnectDaqDevice() and ulReleaseDaqDevice() before exiting the process.
*/

#include <stdio.h>
#include <stdlib.h>
#include "uldaq.h"
#include "utility.h"

#define MAX_DEV_COUNT  100
#define MAX_SCAN_OPTIONS_LENGTH 256
#define ActualTickSize 0.00000002083
int main(void)
{
int descriptorIndex = -1;
DaqDeviceDescriptor devDescriptors[MAX_DEV_COUNT];
DaqDeviceInterface interfaceType = USB_IFC;
DaqDeviceHandle daqDeviceHandle = 0;
unsigned int numDevs = MAX_DEV_COUNT;

// set some variables that are used to acquire data
int FirstChannel = 0;
int LastChannel = 1;
int ChannelCount = 2;

const int samplesPerCounter = 100;
double SampleRate = 100;
int index = 0;

unsigned long long* buffer = NULL;
UlError err = ERR_NO_ERROR;

int i = 0;
int __attribute__((unused)) ret;
char c;

// Get descriptors for all of the available DAQ devices
err = ulGetDaqDeviceInventory(interfaceType, devDescriptors, &numDevs);

if (err != ERR_NO_ERROR)
goto end;

// verify at least one DAQ device is detected
if (numDevs == 0)
{
printf("No DAQ device is detected\n");
goto end;
}

printf("Found %d DAQ device(s)\n", numDevs);
for (i = 0; i < (int) numDevs; i++)
{
printf("  [%d] %s: (%s)\n", i, devDescriptors[i].productName, devDescriptors[i].uniqueId);
// USB-CTR04 id = 0x12E, USB-CTR08 id = 0x127
if((devDescriptors[i].productId == 0x12E) || (devDescriptors[i].productId == 0x127))
{
            descriptorIndex = i;
            daqDeviceHandle = ulCreateDaqDevice(devDescriptors[descriptorIndex]);
            break;
        }
    }
    if(descriptorIndex == -1)
    {
        printf("\nERROR: USB-CTR04 or USB-CTR08 not found\n");
        return 0;
    }

printf("Handle = %d\n", daqDeviceHandle);
printf("\nConnecting to device %s - please wait ...\n", devDescriptors[descriptorIndex].devString);

// create a connection to the device
err = ulConnectDaqDevice(daqDeviceHandle);

if (err != ERR_NO_ERROR)
goto end;


// configure channel for pulsewidth mode
err = ulCConfigScan(daqDeviceHandle,
                    0,
                    CMT_PULSE_WIDTH,
                    CMM_DEFAULT,
                    CED_RISING_EDGE,
                    CTS_TICK_20PT83ns,
                    CDM_NONE,
                    CDT_DEBOUNCE_500ns,
                    CF_DEFAULT);
    if (err != ERR_NO_ERROR)
goto end;
    // configure channel for period mode
  err = ulCConfigScan(daqDeviceHandle,
                    1,
                    CMT_PERIOD,
                    CMM_DEFAULT,
                    CED_RISING_EDGE,
                    CTS_TICK_20PT83ns,
                    CDM_TRIGGER_AFTER_STABLE,
                    CDT_DEBOUNCE_500ns,
                    CF_DEFAULT);
    if (err != ERR_NO_ERROR)
    {
goto end;
    }

// allocate a buffer to receive the data
buffer = (unsigned long long*) malloc(ChannelCount * samplesPerCounter * sizeof(unsigned long long));

if(buffer == NULL)
{
printf("\nOut of memory, unable to create scan buffer\n");
goto end;
}



printf("\n%s ready\n", devDescriptors[descriptorIndex].devString);
printf("    Function demonstrated: ulCConfigScan()\n");
printf("    Counter: %d - %d\n", FirstChannel, LastChannel);
printf("    Samples per channel: %d\n", samplesPerCounter);
printf("    Rate: %f\n", SampleRate);
printf("\nHit ENTER to continue\n");

ret = scanf("%c", &c);

ret = system("clear");

double freq = 1000;
double duty = 0.2;
double delay = 0.0;

//Timer output used as test signal, attach TMR0 to both counter inputs
    err = ulTmrPulseOutStart(daqDeviceHandle, 0, &freq, &duty, 0, &delay, TMRIS_LOW, PO_DEFAULT );

// start the acquisition
err = ulCInScan(daqDeviceHandle,
                    FirstChannel,
                    LastChannel,
                    samplesPerCounter,
                    &SampleRate,
                    SO_DEFAULTIO | SO_CONTINUOUS,
                    CINSCAN_FF_CTR32_BIT ,
                    buffer);

if(err == ERR_NO_ERROR)
{
ScanStatus status;
TransferStatus transferStatus;
int i = 0;

// get the initial status of the acquisition
ulCInScanStatus(daqDeviceHandle, &status, &transferStatus);

while(status == SS_RUNNING && err == ERR_NO_ERROR && !enter_press())
{
// get the current status of the acquisition
err = ulCInScanStatus(daqDeviceHandle, &status, &transferStatus);

if(err == ERR_NO_ERROR)
{
// reset the cursor to the top of the display and
// show the termination message
resetCursor();
printf("Hit 'Enter' to terminate the process\n\n");
printf("Active DAQ device: %s (%s)\n\n", devDescriptors[descriptorIndex].productName, devDescriptors[descriptorIndex].uniqueId);
printf("actual scan rate = %f\n\n", SampleRate);

index = transferStatus.currentIndex;
printf("currentScanCount =  %-10llu \n", transferStatus.currentScanCount);
printf("currentTotalCount = %-10llu \n", transferStatus.currentTotalCount);
printf("currentIndex =      %-10d \n\n", index);

if(index >= 0)
{
// display the data
                    float pulsewidth = (float)((buffer[index] * ActualTickSize) * 1000);
                    float period = (float)((buffer[index +1] * ActualTickSize) * 1000);
                    clearEOL();
                    //pulsewidth
                    printf("chan %d \t= %2.3f mS\n",0, pulsewidth);
                    //period channel
                    printf("chan %d \t= %2.3f mS\n",1, period );
                    printf("duty cycle \t= %2.3f %\n", (pulsewidth/period) * 100);

usleep(100000);
}
}
}

// stop the acquisition if it is still running
if (status == SS_RUNNING && err == ERR_NO_ERROR)
{
err = ulCInScanStop(daqDeviceHandle);
}

}

// disconnect from the DAQ device
ulDisconnectDaqDevice(daqDeviceHandle);

end:

// release the handle to the DAQ device
ulReleaseDaqDevice(daqDeviceHandle);

// release the scan buffer
    free(buffer);

if(err != ERR_NO_ERROR)
{
char errMsg[ERR_MSG_LEN];
ulGetErrMsg(err, errMsg);
printf("Error Code: %d \n", err);
printf("Error Message: %s \n", errMsg);
}

return 0;
}




Rate this Article:

Attachments



Add Your Comments


For comments email [email protected].

Details
Article ID: 50849

Last Modified:9/15/2021 12:04:58 PM

Article has been viewed 1,589 times.

Options