Transmitting Data Over the I2C Interface
The typical workflow involves adapter discovery, connection, communication, and
cleanup. Discovery can be done only at the adapter level. You need to have either a
Total Phase Aardvark host adapter or a NI USB-845x adapter 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 adapter.
To communicate with an EEPROM chip:
Ensure that the Aardvark adapter is installed so that you can use the
i2c
interface.instrhwinfo('i2c')
ans = HardwareInfo with properties: InstalledAdaptors: {'Aardvark' 'NI845x'} JarFileVersion: 'Version 4.1' Access to your hardware may be provided by a support package. Go to the Support Package Installer to learn more.
Look at the adapter properties.
instrhwinfo('i2c','Aardvark')
ans = HardwareInfo with properties: AdaptorDllName: 'C:\Program Files\MATLAB\R2019b\toolbox\instrument\instrumentadaptors\win64\mwaardvarki2c.dll' AdaptorDllVersion: 'Version 4.1' AdaptorName: 'Aardvark' BoardIdsInUse: [1×0 double] InstalledBoardIDs: 0 DetectedBoardSerials: {'2237482577 (BoardIndex: 0)'} ObjectConstructorName: 'i2c('Aardvark', BoardIndex, RemoteAddress);' VendorDllName: 'aardvark.dll' VendorDriverDescription: 'Total Phase I2C Driver' Access to your hardware may be provided by a support package. Go to the Support Package Installer to learn more.
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.
Create the I2C object called
eeprom
, using the following properties.% Vendor = aardvark % BoardIndex = 0 % RemoteAddress = 50h eeprom = i2c('aardvark',0,'50h')
I2C Object : I2C-0-50h Communication Settings BoardIndex 0 BoardSerial 2237482577 BitRate: 100 kHz RemoteAddress: 50h Vendor: aardvark Communication State Status: closed RecordStatus: off Read/Write State TransferStatus: idle
You must provide these three inputs to create the object. To identify the address of the chip, consult its documentation or data sheet. You can also find the address 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.
Connect to the chip.
fopen(eeprom)
Write
'Hello World!'
to the EEPROM chip. 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!'])
Read data back from the chip using the
fread
function. The chip returns the characters sent to it.To start reading from the first byte of the first page, write a zero to the
i2c
object.fwrite(eeprom,0)
char(fread(eeprom,12))'
ans = 'Hello World!'
Disconnect the I2C device and clear the object from the workspace.
fclose(eeprom) clear eeprom
NI USB-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 USB-845x adapter. In this case, the NI USB-845x adapter board is plugged into the computer (via the USB port), and a circuit board containing the sensor chip is connected to the host adapter board via wires. Note that the circuit has external pullups, as the NI USB-8451 adapter used in this example does not have internal pullups.
To communicate with a sensor chip:
Ensure that the NI USB-845x adapter is installed so that you can use the
i2c
interface.instrhwinfo('i2c')
ans = HardwareInfo with properties: InstalledAdaptors: {'Aardvark' 'NI845x'} JarFileVersion: 'Version 4.1' Access to your hardware may be provided by a support package. Go to the Support Package Installer to learn more.
Look at the NI USB-845x adapter properties.
instrhwinfo('i2c','NI845x')
ans = HardwareInfo with properties: AdaptorDllName: 'C:\Program Files\MATLAB\R2019b\toolbox\instrument\instrumentadaptors\win64\mwni845xi2c.dll' AdaptorDllVersion: 'Version 4.1' AdaptorName: 'NI845x' BoardIdsInUse: [1×0 double] InstalledBoardIDs: [1×0 double] DetectedBoardSerials: {0×1 cell} ObjectConstructorName: 'i2c('NI845x', BoardIndex, RemoteAddress);' VendorDllName: 'Ni845x.dll' VendorDriverDescription: 'National Instruments NI USB 845x Driver' Access to your hardware may be provided by a support package. Go to the Support Package Installer to learn more.
Make sure that you have the NI USB-845x software driver installed. For details, see I2C Interface Usage Requirements and Guidelines.
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 inputs to create the object. To identify the address of the chip, consult its documentation or data sheet. You can also find the address 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.
Connect to the chip.
fopen(i2cobj)
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 adapter 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)
Read data back from the chip using the
fread
function. By sending one byte, you can read back the device ID registry. For this chip, the read-only device ID registry is 229.fread(i2cobj,1)
ans = 229
Disconnect the I2C device and clear the object from the workspace.
fclose(i2cobj) clear i2cobj
See Also
i2c
| fopen
| fread
| fwrite
| fclose