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

Linux C frequency divider example for USB-CTR04/08

Expand / Collapse

Linux C frequency divider example for USB-CTR04/08

This is a product specific C example that uses one counter to divide an input frequency and a second counter to measure the new frequency. To do this it enable the counter output and uses range limited counting. The counter will count to a specified value setting the output line high. Then on a second value it set the output line low. This example is set to output a 50% duty cycle but if you play with the output register values you can get other duty cycles. The complete program is listed below and attached at the bottom of the article.

    UL call demonstrated:          ulCInConfigScan()

    Purpose:                          Performs a continuous scan of the
                                      specified  channels

    Demonstration:                    Output On and Range Limit On

    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.

    ***** test signal is supply by TMR0. Connect it to counter 0 input
    ***** connect counter 0 output to counter 1 input, which is used to measure divide frequency

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

#define MAX_DEV_COUNT  100
#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 = 1;
int LastChannel = 1;
int ChannelCount = 1;

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]);
    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 count with range bound
err = ulCConfigScan(daqDeviceHandle,
                    CMM_OUTPUT_ON |CMM_RANGE_LIMIT_ON,
    if (err != ERR_NO_ERROR)
goto end;

    err = ulCLoad(daqDeviceHandle,0,CRT_MIN_LIMIT, 0);
    err = ulCLoad(daqDeviceHandle,0,CRT_MAX_LIMIT, 99);

    if (err != ERR_NO_ERROR)
goto end;

//divide the input frequency by 100; Output will be low from 0-49, high from 50-99
err = ulCLoad(daqDeviceHandle,0, CRT_OUTPUT_VAL0, 49);
    err = ulCLoad(daqDeviceHandle, 0, CRT_OUTPUT_VAL1, 99);
    // to change the duty cycle modify the CRT_OUPUT_VAL0

    if (err != ERR_NO_ERROR)
goto end;

    // configure channel for period mode
  err = ulCConfigScan(daqDeviceHandle,
    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: %.2f S/s\n", SampleRate);
printf("\nHit ENTER to continue\n");

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

ret = system("clear");

double freq = 100000;
double duty = 0.5;
double delay = 0.0;

//***** Timer output used as test signal, attach TMR0 to counter 0
    err = ulTmrPulseOutStart(daqDeviceHandle, 0, &freq, &duty, 0, &delay, TMRIS_LOW, PO_DEFAULT );
    if (err != ERR_NO_ERROR)
goto end;

    //***** Also wire counter 0 output to counter 1 input
err = ulCInScan(daqDeviceHandle,
                    SO_DEFAULTIO | SO_CONTINUOUS,
                    CINSCAN_FF_CTR32_BIT ,

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
printf("Hit 'Enter' to terminate the process\n\n");
printf("Active DAQ device: %s (%s)\n\n", devDescriptors[descriptorIndex].productName, devDescriptors[descriptorIndex].uniqueId);
printf("test frequency = %.1f Hz\n", freq);
printf("divide by 100\n\n");

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 period = (float)((buffer[index] * ActualTickSize));

                    //0 to 100 count

                    printf("New frequency: \t= %2.3f Hz\n", 1/period );


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


// disconnect from the DAQ device


// release the handle to the DAQ device

// release the scan 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:


Add Your Comments

For comments email

Article ID: 50850

Last Modified:9/15/2021 2:23:56 PM

Article has been viewed 142 times.