Page 1 of 1

SUB20 & Matlab

Posted: Wed Dec 22, 2010 4:19 pm
by myHermes
Hi,
I am searching information for reading and writing on a register using .NET component with Matlab environment.

In particular I have the slave address: 74
the register address 06

but I don't understand how to write and read from the register

Can anyone help me?

Re: SUB20 & Matlab

Posted: Thu Dec 23, 2010 5:14 am
by serg
Here is the matlab i2c sample code

Code: Select all


% Sets I2C frequency to 400kHz and writes one byte(aa) to a register(06) of
% a memory-like slave device ( bus address 74) All numbers are in hex.


NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();

% simulate do { ... } while(0) loop
for k=0:0
    
    % Open first available SUB-20 device
    if ~ sub20.Open(0)
        break;
    end
    
    % Set I2C clock frequency to 400kHz
    if ~ sub20.I2C_SetFrequency(400000)
        break;
    end
    
    % Create .NET data array
    Data = NET.createArray('System.Byte', 1);
    
    % Fill up the array with data bytes to be sent
    Data.Set(0, hex2dec('aa'));
    
    % Write 'Data' array to register 6 of a device 74
    if ~ sub20.I2C_Write( hex2dec('74'), hex2dec('06'), 1, Data)
        break;
    end
    
    % Read register 6 of the device 74 to the array 'Data'
    if ~ sub20.I2C_Read( hex2dec('74'), hex2dec('06'), 1, Data)
        break;
    end
    
end

err = sub20.GetLastError();

% Close SUB-20 device
sub20.Close()

if err > 0
    fprintf('Error %d\n', sub20.GetLastError() )
    error('Error!')
end