Documentation

Transmitting Data Over the I2C Interface

The typical workflow involves adaptor discovery, connection, communication, and cleanup. Discovery can be done only at the adaptor level. You need to have either a Total Phase Aardvark host adaptor or a NI-845x adaptor board installed to use the i2c interface.

Aardvark Example

This example shows how to communicate with an EEPROM chip on a circuit board, with an address of 50 hex and a board index of 0, using the Aardvark adaptor.

To communicate with an EEPROM chip:

  1. Ensure that the Aardvark adaptor is installed so that you can use the i2c interface.

    instrhwinfo('i2c')

  2. Look at the adaptor properties.

    instrhwinfo('i2c', 'Aardvark')
    ans = 
    
              AdaptorDllName: [1x127 char]
           AdaptorDllVersion: 'Version 3.0.0'
                 AdaptorName: 'aardvark'
           InstalledBoardIds: 0
       ObjectConstructorName: 'i2c('aardvark', BoardIndex, RemoteAddress);'
               VendorDllName: 'aardvark.dll'
     VendorDriverDescription: 'Total Phase I2C Driver'
    

    Make sure that you have the Aardvark software driver installed and that the aardvark.dll is on your MATLAB path. For details, see I2C Interface Usage Requirements and Guidelines.

  3. Create the I2C object called eeprom, using these properties:

    % Vendor = aardvark
    % BoardIndex = 0
    % RemoteAddress = 50h
    
    eeprom = i2c('aardvark',0,'50h');

    You must provide these three parameters to create the object. Read the documentation of the chip in order to know what the remote address is.

      Tip   You can also see what the remote address of the chip is by scanning for instruments in the Test & Measurement Tool. In the tool, right-click the I2C node and select Scan for I2C adaptors. Any chips found by the scan is listed in the hardware tree. The listing includes the remote address of the chip.

  4. Connect to the chip.

    fopen(eeprom);
  5. Write 'Hello World!' to the EEPROM. Data is written page-by-page in I2C. Each page contains eight bytes. The page address needs to be mentioned before every byte of data written.

    The first byte of the string 'Hello World!' is 'Hello Wo'. Its page address is 0.

    fwrite(eeprom,[0 'Hello Wo']);

    The second byte of the string 'Hello World!' is 'rld!'. Its page address is 8.

    fwrite(eeprom,[8 'rld!']);
  6. Read data back from the chip using the fread function.

    A zero needs to be written to the i2c object, to start reading from the first byte of first page.

    fwrite(eeprom,0);
    char(fread(eeprom,16))'

    The chip returns the characters it was sent, as shown here.

  7. Clean up by deleting and clearing the object.

    fclose(eeprom);
    delete(eeprom);
    clear('eeprom');

NI-845x Example

This example shows how to communicate with an Analog Devices™ ADXL345 sensor chip on a circuit board, using an address of 53 hex and a board index of 0 on a NI-845x adaptor. In this case, the NI-845x adaptor board is plugged into the computer (via the USB port), and a circuit board containing the sensor chip is connected to the host adaptor board via wires. Note that the circuit has external pullups, as the NI-8451 adaptor used in this example does not have internal pullups.

To communicate with a sensor chip:

  1. Ensure that the NI-845x adaptor is installed so that you can use the i2c interface.

  2. Look at the NI-845x adaptor properties.

    Make sure that you have the NI-845x software driver installed. For details, see I2C Interface Usage Requirements and Guidelines.

  3. Create the I2C object called i2cobj, using these properties:

    % Vendor = NI845x
    % BoardIndex = 0
    % RemoteAddress = 53h
    
    i2cobj = i2c('NI845x', 0, '53h');

    You must provide these three parameters to create the object. Read the documentation of the chip in order to know what the remote address is.

      Tip   You can also see what the remote address of the chip is by scanning for instruments in the Test & Measurement Tool. In the tool, right-click the I2C node and select Scan for I2C adaptors. Any chips found by the scan is listed in the hardware tree. The listing includes the remote address of the chip.

  4. Connect to the chip.

    fopen(i2cobj)
  5. Write to the sensor chip. Read the documentation or data sheet of the chip in order to know what the remote address is and other information about the chip. Usually chip manufacturers provide separate read and write addresses. The adaptor boards only take one address (the read address) and handle conversions to read and write addresses.

    In this case, the chip's device ID register is at address 0, so you need to write a 0 to the chip indicating you would like to read or write to the register.

    fwrite(i2cobj, 0)
  6. Read data back from the chip's device ID register using the fread function. Reading 1 byte of data returns the device ID registry. In the case of this chip, the read-only device ID register value is 229. Therefore, that is what is returned when you send the byte.

    fread(i2cobj, 1)
    
    ans = 
    
       229
  7. Clean up by deleting and clearing the object.

    fclose(i2cobj);
    delete(i2cobj);
    clear('i2cobj');

Other Functionality

You can use these functions with the i2c object.

FunctionPurpose
fopenConnect interface object to instrument.
freadRead binary data from instrument.
fwriteWrite binary data to instrument.
methodsNames and descriptions of functions that can be used with i2c objects.
fcloseDisconnect interface object from instrument.
recordRecord data and event information to file.
propinfoDisplay instrument object property information.

For more information about these functions, see the functions documentation.

Was this topic helpful?