Jump to content
  • 0

Differentiating Ports 0-7 when sending 8bit triggers with cbDOut (MC 1208-FS-Plus)


Darcy Diesburg

Question

Hello,

I am trying to configure my Measurement Computing 1208FS-Plus DAQ to send 8-bit triggers that are a binary representation of 0-255 (i.e., 1 =1 0 0 0 0 0 0 0). I'm using compiled mex files with functions from the Universal Library in MATLAB 2021b and have the Dat Acquisition Toolbox and add-on for Measurement Computing DAQs installed. The DAQ is set up and calibrated in Instacal in differential mode.

Here is the C code I am using:

#include "mex.h"
#include "cbw.h"

// Define the DAQ board number
#define BOARD_NUM 0

// Define the digital port
#define PORT_NUM FIRSTPORTA

// The entry point for the MEX function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    if (nrhs != 1) {
        mexErrMsgIdAndTxt("MATLAB:sendTTL:invalidNumInputs", "One input required.");
    }

    // Get the input value
    if (!mxIsDouble(prhs[0]) || mxGetNumberOfElements(prhs[0]) != 1) {
        mexErrMsgIdAndTxt("MATLAB:sendTTL:inputNotScalar", "Input must be a scalar.");
    }

    double inputValue = mxGetScalar(prhs[0]);
    if (inputValue < 0 || inputValue > 255) {
        mexErrMsgIdAndTxt("MATLAB:sendTTL:inputOutOfRange", "Input must be between 0 and 255.");
    }

    // Convert the input value to an integer
    int value = (int)inputValue;

    // Configure the digital port
    int configResult = cbDConfigPort(BOARD_NUM, PORT_NUM, DIGITALOUT);
    if (configResult != 0) {
        char errorMessage[256];
        cbGetErrMsg(configResult, errorMessage);
        mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDConfigPortFailed", "Failed to configure the prort. Error code: %d. Error message: %s", configResult, errorMessage);
    }

    // Send the TTL signal
    int result = cbDOut(BOARD_NUM, PORT_NUM, value);
    if (result != 0) {
        char errorMessage[256];
        cbGetErrMsg(result,errorMessage);
        mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to send TTL signal. Error code: %d. Error message: %s", result, errorMessage);
    }

        // Wait for 5 milliseconds
#ifdef _WIN32
    Sleep(5); // Sleep for 5 milliseconds on Windows
#else
    usleep(5000); // Sleep for 5000 microseconds (5 milliseconds) on Unix/Linux
#endif

    // Set the digital output low
    result = cbDOut(BOARD_NUM, PORT_NUM, 0);
    if (result != 0) {
        char errorMessage[256];
        cbGetErrMsg(result, errorMessage);
        mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to reset TTL signal. Error code: %d. Error message: %s", result, errorMessage);
    }
}

The problem I am encountering is that any integer I submit to the code once it is compiled (e.g., sendTTL([insert number here]) is triggering a falling TTL ALL of the Port A pins, rather than just the ones that should be triggered for just that number, so it only ever sends a decodable 255 for any integer. Could you give any advice on troubleshooting this?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

The cbDConfigPort() function performs a port reset and should be called once, usually in the initialization section of your program. You could also put an IF statement around it that is executed once. Also, you'll want to gate how fast you update the port, limiting it to 200 per second at maximum.

Link to comment
Share on other sites

  • 0

Thanks for the feedback, @JRys. I am indeed calling cbDConfigPort() once, but all pins are being triggered on the subsequent call to cbDOut. Are you saying that all the pins flipping could be because cbDConfigPort() may be called more than once, rather than cbDOut executing correctly?

Link to comment
Share on other sites

  • 0
Posted (edited)

@JRys I removed the configuration call to a different mex file I only call once and reconfigured for bitwise ouput, however the DAQ is still flipping all the port A bits when I call the mex function below, even though it explicitly calls for bit 0 only). I tried using cbDConfigBit in the configuration file to address but when the config file is setup with cbDConfigBit instead of cbDConfigPort, I get the following errror: 'Error message: This function can not be used
with this board.'

#include "mex.h"
#include "cbw.h"

// Define the DAQ board number
#define BOARD_NUM 0

// Define the digital port

#define PORT_NUM FIRSTPORTA

// The entry point for the MEX function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    if (nrhs != 1) {
        mexErrMsgIdAndTxt("MATLAB:sendTTL:invalidNumInputs", "One input required.");
    }

    // Get the input value
    if (!mxIsDouble(prhs[0]) || mxGetNumberOfElements(prhs[0]) != 1) {
        mexErrMsgIdAndTxt("MATLAB:sendTTL:inputNotScalar", "Input must be a scalar.");
    }

    double inputValue = mxGetScalar(prhs[0]);
    if (inputValue < 0 || inputValue > 7) {
        mexErrMsgIdAndTxt("MATLAB:sendTTL:inputOutOfRange", "Input must be between 0 and 7.");
    }

    // Convert the input value to an integer
    int bitFlip = (int)inputValue;

    // Send the TTL signal
    int result = cbDBitOut(BOARD_NUM, PORT_NUM, bitFlip, 1);
    if (result != 0) {
        char errorMessage[256];
        cbGetErrMsg(result,errorMessage);
        mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to send TTL signal. Error code: %d. Error message: %s", result, errorMessage);
    }

        // Wait for 5 milliseconds
#ifdef _WIN32
    Sleep(6); // Sleep for 6 milliseconds on Windows
#else
    usleep(6000); // Sleep for 6000 microseconds (5 milliseconds) on Unix/Linux
#endif

    // Set the digital output low
    result = cbDBitOut(BOARD_NUM, PORT_NUM, bitFlip, 0);
    if (result != 0) {
        char errorMessage[256];
        cbGetErrMsg(result, errorMessage);
        mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to reset TTL signal. Error code: %d. Error message: %s", result, errorMessage);
    }
}

Edited by Darcy Diesburg
Link to comment
Share on other sites

  • 0

Updating - these new issues seem to have been a problem on the receiving hardware end, things are working now with the separated configuration script and TTL triggers with cbDOut alone (using original syntax). Thanks for the assistance with the original points - it's working now!

Link to comment
Share on other sites

  • 0

Use cbDConfigPort to set the direction. cbDConfigBit is used with devices whose port type is AUXPORT.

I've attached my C test program, which cycles through setting each bit high and then low. The following is a logic analyzer output showing the state of each bit. 

When checking the lines by probing the screw tops, ensure that the terminal is anchored on a wire or screw down. Loose screws make poor contact.

image.png

DigitalTest.cpp

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...