Jump to content
  • 0

USB-1208FS C language


ThomMoun

Question

Hello,

I am experiencing difficulties using the cbw library from Measurement Computing.

I am working with the USB1208FS Plus card, and my current goal is to turn on and off an RGB LED using this card. The LED is connected to a breadboard.

I am using the DevC++ IDE, but I am encountering errors in the makefile during compilation. Do you think I may have imported the library and the cbw.h header file incorrectly? Or is there a specific function that needs to be called before any other to ensure smooth compilation?

I also tried using CodeBlocks, but my header file is either not being detected, or I have an issue with the precompiled file cbw.h.gch.

If anyone can provide me with one or two hints, even if they are small, I would greatly appreciate it.

Thank you in advance,

TM

Link to comment
Share on other sites

20 answers to this question

Recommended Posts

  • 0

Hello,

I have yet to try to use DevC++ IDE or Code: Blocks on Windows. MCC uses Microsoft Visual Studio. I recommend downloading the free Community version of Visual Studio for your project. Create a 32-bit C console project and add to it the cbw.h, cbw32.lib files and your code file. Below is a simple 32-bit C console program that toggles D0 on FirstPortA and D0 or FirstPortB.

#include "stdafx.h"

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


#define MAXNUMDEVS 100

void main()
{
    /* Variable Declarations */

    int ULStat = 0;

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

    int BoardNum = -1;

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

    printf("Digital output demonstration\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 IDs can be found in ULProps.txt located in 
        // C:\Program Files (x86)\Measurement Computing\DAQ

        if (DeviceDescriptor.ProductID == 0xE8) 
        {
            BoardNum = i;
            ULStat = cbCreateDaqDevice(BoardNum, DeviceDescriptor);
            printf("Device Name: %s\n", DeviceDescriptor.ProductName);
            break;
        }

    }

    if (BoardNum < 0)
    {
        printf("USB device not found...press any key to exit\n");
        getch();
        return;
    }

    ULStat = cbDConfigPort(BoardNum, FIRSTPORTA, DIGITALOUT);
    ULStat = cbDConfigPort(BoardNum, FIRSTPORTB, DIGITALOUT);

    unsigned short dataValue = 0;

    while (!_kbhit())
    {
        ULStat = cbDOut(BoardNum, FIRSTPORTA, dataValue);
        ULStat = cbDOut(BoardNum, FIRSTPORTB, dataValue);
        dataValue = 1;
        ULStat = cbDOut(BoardNum, FIRSTPORTA, dataValue);
        ULStat = cbDOut(BoardNum, FIRSTPORTB, dataValue);
        dataValue = 0;


        if (ULStat != 0){
            printf("Digital IO error\n");
            break;
        }

    }


    cbReleaseDaqDevice(BoardNum);

    printf("Completed...press any key to exit\n");
    getch();
}


 

Link to comment
Share on other sites

  • 0

Hello,

How can i use more than 2 ports as a digital output ? I am currently using FIRSTPORTA and FIRSTPORTB to turn on blue and green but as soon as i try using SECONDPORTA or B to turn on red color (config + out), a mistake appears (digital port number error). I noticed that on Instacal i can only test those two first ports, is it related ? How can i activate the others ?

Respectfully

 

TM

Link to comment
Share on other sites

  • 0

Hello,

Your device has two 8-bit ports, FIRSTPORTA and FIRSTPORTB, or 16 bits altogether. In my example code, the following two lines set D0 on FIRSTPORTA. 

dataValue = 1;
ULStat = cbDOut(BoardNum, FIRSTPORTA, dataValue);

dataValue is 0-255, depending on which bits you want to set. Using 128 will turn on D7, and 129 will turn on D0 & D7. 

You could use the cbDBitOut function instead. It uses only FIRSTPORTA, a bit number, and a bit level (0 or  1). Bit numbers are 0 to 15.

Best regards,

John

Link to comment
Share on other sites

  • 0

Hello,

Thanks, it works better with cbDBitOut !

Now am trying with analog sensors. By reading the datasheet i understand that it is not necessary to use a function as cbDConfigPort to prepare an analog port and i can directly use cbAOut and cbAIn. My sensor is a temperature sensor with 3 pins (adj, +, -), to collect its data i connected it to an anolog output to power it with 5V, and to an analog input to receive the voltage given back by the sensor. This solution does not work, so i think there are errors in my code or in my hardware configuration.

 

#define RANGE BIP5VOLTS

        ULStat = cbAOut(BoardNum, ADDA1, RANGE, 5);
        ULStat = cbAOut(BoardNum, ADDA2, RANGE, 1);

        ULStat = cbAIn(BoardNum, CHANNEL, RANGE, &temperature);

        if (ULStat != 0) {
            printf("Erreur lors de la lecture des données analogiques.\n");
            return 1;

i have the same issue with the distance sensor.

the temperature sensor  :

https://www.ti.com/lit/ds/symlink/lm335.pdf?ts=1685441744173&ref_url=https%3A%2F%2Fwww.ti.com%2Fproduct%2FLM335%2Fpart-details%2FLM335Z%2FNOPB%3Futm_source%3Dgoogle%26utm_medium%3Dcpc%26utm_campaign%3Docb-tistore-promo-asc_opn_en-cpc-storeic-google-wwe%26utm_content%3DDevice%26ds_k%3DLM335Z%2FNOPB%26DCM%3Dyes%26gclid%3DCjwKCAjwvdajBhBEEiwAeMh1U3xNuSXuxXPB2xmvuas6gYn7MyloL68uug54Y-4jCJYOqjNNP8sRTBoCuZoQAvD_BwE%26gclsrc%3Daw.ds

Link to comment
Share on other sites

  • 0

Hello,

I'm unfamiliar with the LM135 temperature sensor, but I did look at the datasheet. It would be better to power it with the +VO pin 30, which is +5 volts. The datasheet suggests powering it with the lowest possible current, and the application diagram suggests using a current-limiting resistor. For example, putting a 4.7k  ohm resistor in series with the +VO power will limit the current to just over one mA. It also suggests using a trim potentiometer for calibration, but you could devise a scheme to calibrate in software. 

Best regards,
John

Link to comment
Share on other sites

  • 0

Do you have the sensor's reference connected to the USB-1208FS-Plus ground pin, and is the output signal connected to CH0H (pin 1)? If I assume this is how you connect it, add the function cbAInputMode(BoardNum, SINGLE_END) before you drop into a loop to read the voltage.  Review the following code:

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


#define MAXNUMDEVS 100

void main()
{
    /* Variable Declarations */

    int ULStat = 0;

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

    int BoardNum = -1;

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

    printf("Digital output demonstration\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 IDs can be found in ULProps.txt located in 
        // C:\Program Files (x86)\Measurement Computing\DAQ

        if (DeviceDescriptor.ProductID == 0xE8) 
        {
            BoardNum = i;
            ULStat = cbCreateDaqDevice(BoardNum, DeviceDescriptor);
            printf("Device Name: %s\n", DeviceDescriptor.ProductName);
            break;
        }

    }

    if (BoardNum < 0)
    {
        printf("USB device not found...press any key to exit\n");
        getch();
        return;
    }

    float temp = 0.0f;
    cbAInputMode(BoardNum, SINGLE_ENDED);

    ULStat = cbDConfigPort(BoardNum, FIRSTPORTA, DIGITALOUT);
    ULStat = cbDConfigPort(BoardNum, FIRSTPORTB, DIGITALOUT);

    unsigned short dataValue = 0;

    while (!_kbhit())
    {
        ULStat = cbDOut(BoardNum, FIRSTPORTA, dataValue);
        ULStat = cbDOut(BoardNum, FIRSTPORTB, dataValue);
        dataValue = 1;
        ULStat = cbDOut(BoardNum, FIRSTPORTA, dataValue);
        ULStat = cbDOut(BoardNum, FIRSTPORTB, dataValue);
        dataValue = 0;


        if (ULStat != 0){
            printf("Digital IO error\n");
            break;
        }

        cbVIn(BoardNum, 0, BIP10VOLTS, &temp, 0);
        printf("%2.3f\n", temp);
        Sleep(100);

    }


    cbReleaseDaqDevice(BoardNum);

    printf("Completed...press any key to exit\n");
    getch();
}


 

Link to comment
Share on other sites

  • 0

Hello,

First of all, thanks a lot for your help ! Now I am able to control and acquire the datas of all my sensors.

I have some more few questions about the USB1208 card, those questions are more general and i did not found all my answers in the datasheet.

 

Is there a solution to control a digital output with PWM ?

Is it possible to use the hardware timer to run a C function periodically ? (toggling a led at a specific frequency for example)

 

Is it possible to run a function when a digital pin changes its input? (callback ?)

Can i count the events with an encoder ?

 

and how can i interact with an analog input ?

 

Thanks a lot in advance,

 

(btw it works with codeblocks but devC still does not work)

 

Best Regards

TM

 

 

Link to comment
Share on other sites

  • 0

Hello,

The USB-1208FS-Plus does not have PWM support.

The call-back event notifications are for the analog input and output. There's nothing for the digital IO. For more information about the event notifications, see the UL Help file. Specifically, search on USB-1208FS-Plus to get to your device page. https://www.mccdaq.com/pdfs/manuals/Mcculw_WebHelp/ULStart.htm

Best regards,
John

image.png

 

Link to comment
Share on other sites

  • 0

Hello John,

As always i thanks you a lot for your answer !

So i can use the callback function to time some of my analog adquisitions ?

I tried to time the toglling of my led with some trigger's related function :

 

cbSetConfig(BOARDINFO, BoardNum, 0, BIADTRIGCOUNT, BIDACTRIGCOUNT);

cbSetTrigger(BoardNum, TRIG_HIGH, 0, 0);

ULStat = cbDConfigPort(BoardNum, FIRSTPORTA, DIGITALOUT);

 

and then ULStat = cbDInScan(BoardNum, FIRSTPORTA, 10, 1, 1, MemeHandle,  &value);

 

but it does not work. It builds well but says me that this function (cbDInScan) is not adapted for my card USB1208. I will like to not depend of a loop to make it.

Is the trigger activated in the card or should i connect the trigger port ?

 

If i want to count some events through the CNT port, should i use cbEnableEvent().

 

Thanks again

 

TM

 

Link to comment
Share on other sites

  • 0

I would like to create a code that configures the SYNC port of my USB1208FSPlus to act as an external clock source (it seems possible according to the documentation). I will then connect this SYNC port to the TRIG_IN port on my card to trigger an event at each clock period. Additionally, I want to connect my LED and digital ports to this setup to toggle periodically the led without a loop.

I'm a bit unsure about which functions to use and how to establish the connections between the ports.

Regarding the counting of events, I came across the cbCIn() function. However, I'm unsure if it only works with cbCLoad. Will it count every event on a specific port?

 

thanks in advance

 

TM

 

 

Link to comment
Share on other sites

  • 0

The sync pin outputs the scan clock when not using the EXTTRIGGER scan option. The frequency is the scan rate, and the pulse width is about 10uS wide. The signal has no drive current, so connecting an LED will not work. 

cbCIn will read the counter input, and cbLoad is used to preload the counter with a value. For example, if you want the count to begin at some value other than zero.

Link to comment
Share on other sites

  • 0

Okay i tried several things and nothing works.

Can you clearly tell me what functions should i use to time an event, i want to adquire the data of a distance sensor every 2 seconds without using a loop. For the moment I try like that

    cbVIn(BoardNum, 0, BIP10VOLTS, &dist, 0, BACKGROUND);  

    cbAInScan(BoardNum, 0, 0, 5, &rate, BIP10VOLTS, meme, CONTINUOUS);

 

+ I'm also trying with the counter, i want to count the event of a switch button "How many times did i press it" i did that :

 USHORT switchState; //unsigned short
    USHORT Count;

    ULStat = cbDConfigPort(BoardNum, FIRSTPORTA, DIGITALIN);

    while (!_kbhit()) {

        ULStat = cbDBitIn(BoardNum, FIRSTPORTA,0, &switchState);
        cbCIn32(BoardNum, 0, &Count);
        // we use the cnt port to count the number of button pressing


        printf("Number of clic %u\n", Count);
        Sleep(500);
    }

 

but it does not work.

 

Thanks in advance

Best regards

TM

Link to comment
Share on other sites

  • 0

Hello,

I'm sorry, but I am unfamiliar with your distance sensor. If it outputs digital TTL pulses, and you want to determine the time between each pulse, it cannot be done with the USB-1208FS-Plus. A more suitable device for timing events is the USB-CTR04.

Best regards,

John

Link to comment
Share on other sites

  • 0

It's not about the specifications of the sensor; it's more about selecting the appropriate functions to perform acquisitions at a specific frequency. I want to periodically gather information from a port.

+ Regarding the counter, are my uses of the event-counting functions correct?

TM

Link to comment
Share on other sites

  • 0

Okay so i tried this :

 

    cbAInputMode(BoardNum, SINGLE_ENDED);

    long numberOfSamples = 1024;
    HGLOBAL mem;
    mem = cbWinBufAlloc32(2048);

  // int data = cbCInScan(BoardNum, 0, 0, numberOfSamples, 1, mem, CONTINUOUS);
   int data = cbAInScan(BoardNum, 0, 1, numberOfSamples, 20, BIP10VOLTS, mem, CONTINUOUS);

   for (int i = 0; i < numberOfSamples; i++) {

       printf("Echantillon %d : %d\n", i, data[&i]);

   }

    cbStopBackground(BoardNum, mem);
    cbWinBufFree(mem);

 

in order to adquire 1024 samples, 20 samples/s/channel. My sensor is linked to chan0.

but i have an unexpected mistake :

Unhandled exception at 0x000000006087BFC9 (cbw64.dll) in PROJ2.exe: 0xC0000005: Access violation reading location 0x0000000000000014.

 

Can you help me ?

 

+ If you have any informations about counting event please tell me ?

 

Thanks in advance

 

TM

PS : seems that cbCInScan does exist for the USB1208FS-Plus

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...