Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

I2C communication with U6 in MATLAB

I2C U6 MATLAB

  • Please log in to reply
1 reply to this topic

#1 gsellers

gsellers
  • Members
  • 1 posts

Posted 11 July 2014 - 02:46 PM

Hi, I've been having trouble reading in data to my LabJack U6 from a 9DOF sensor stick, which has an accelerometer, magnetometer, and gyroscope built in, each of which uses I2C protocol to communicate. I tried first to configure the accelerometer, but keep running into problems with porting the pseudocode into MATLAB.  I need to write data first to take the sensor out of sleep mode, but whenever I try writing any data, MATLAB encounters an internal problem and needs to close. I think it may have something to do with the data type of the array that I'm sending, but I'm not sure. Here's my code. The first part, configuring the pins and I2C options, works fine. The second part does not.

 

% Pin numbers
scl = 1;
sda = 0;

% ADXL345 I2C Address 0x53
ADXL_ADDRESS = bitshift(hex2dec('53'),1);

%% Open LabJack
ljasm = NET.addAssembly('LJUDDotNet'); 
ljudObj = LabJack.LabJackUD.LJUD;

[ljerror, ljhandle] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U6, ...
    LabJack.LabJackUD.CONNECTION.USB, '0', true, 0);

%% Configure ports

% Reset ports to defaults
ljudObj.ePut(ljhandle, LabJack.LabJackUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);

% Configure the address byte of the chip
ljudObj.AddRequest(ljhandle,LabJack.LabJackUD.IO.PUT_CONFIG, ...
    LabJack.LabJackUD.CHANNEL.I2C_ADDRESS_BYTE,ADXL_ADDRESS,0,0);

% Configure SCL
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, ...
    LabJack.LabJackUD.CHANNEL.I2C_SCL_PIN_NUM,scl,0,0);

% Configure SDA
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, ...
    LabJack.LabJackUD.CHANNEL.I2C_SDA_PIN_NUM,sda,0,0);

% Configure I2C options 
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, ...
    LabJack.LabJackUD.CHANNEL.I2C_OPTIONS,0,0,0);

% Configure speed
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, ...
    LabJack.LabJackUD.CHANNEL.I2C_SPEED_ADJUST,0,0,0);

ljudObj.GoOne(ljhandle);

% Here's where problems start
numWrite = 1;
array = hex2dec('2D')
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.I2C_COMMUNICATION, ...
    LabJack.LabJackUD.CHANNEL.I2C_WRITE,numWrite,array,0);

ljudObj.GoOne(ljhandle);

 

I've also tried sending the array formatted in binary, in this case 0x2D > 45 > 00101101, and it creates the same error. I would do a check for LabJack errors, but MATLAB shuts down before it returns them anyway. I'm just not sure what's wrong. 

 

In addition, is there any easy way to configure multiple I2C address for communicating with multiple devices? I need to read from three different addresses, and I believe that means I need to reconfigure the I2C settings every time I read from a different address, is this correct? Is there a more efficient way to use multiple addresses without using low level functions?

 



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 11 July 2014 - 05:01 PM

Try creating the array you pass like this and see if it helps:

array = NET.createArray('System.BYTE', 1); %Array needs to have numWrite elements which is 1 in this case
array(1) = = hex2dec('2D')

When using .NET functions and arrays, you need to pass a .NET array to it. We show this in the stream example but with doubles. To see the UD functions and their parameters to pass in MATLAB, use "methodsview(LabJack.LabJackUD.LJUD)" after adding the LJUDDotNet assembly. Try catching errors like we do in our examples as it displays UD, .NET and MATLAB errors that could help.

 

As for multiple devices (that use I2C and are connected to the U6) and addresses, the UD driver only uses one I2C configuration at a time. The low-level command/response itself (which the UD driver uses) both configures, sends and receives data in one command/response:

 

http://labjack.com/s...rs-guide/5.2.21

 

When you use a different device or address, you need to reconfigure the SDA and SCL pins for the different device (assuming they are using different pins) and reconfigure for new address or other settings if they changed. For example:

%Using device 0 connected to FIO0 and FIO1
scl0 = 1;
sda0 = 0;
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_ADDRESS_BYTE, address0, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_SCL_PIN_NUM, scl0, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_SDA_PIN_NUM, sda0, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_OPTIONS, options0, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_SPEED_ADJUST, speed0, 0, 0);
ljudObj.GoOne(ljhandle);

%Do writes and reads for device 0

%Using device 1 connected to FIO2 and FIO3
scl1 = 3;
sda1 = 2;
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_ADDRESS_BYTE, address1, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_SCL_PIN_NUM, scl1, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_SDA_PIN_NUM, sda1, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_OPTIONS, options1, 0, 0);
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_CONFIG, LabJack.LabJackUD.CHANNEL.I2C_SPEED_ADJUST, speed1, 0, 0);
ljudObj.GoOne(ljhandle);

%Do writes and reads for device 1




Also tagged with one or more of these keywords: I2C, U6, MATLAB

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users