Create I2C object
I = i2c('Vendor', BoardIndex, RemoteAddress)
I2C, or Inter-Integrated Circuit, is a chip-to-chip interface supporting two-wire communication. Instrument Control Toolbox™ I2C support lets you open connections with individual chips and to read and write over the connections to individual chips using either an Aardvark host adaptor or a NI-845x adaptor board.
I = i2c('Vendor', BoardIndex, RemoteAddress) constructs an i2c object associated with Vendor, BoardIndex, and RemoteAddress. Vendor must be set to either 'aardvark', for use with a Total Phase Aardvark adaptor, or to 'NI845x', for use with a NI-845x adaptor board, to use this interface. BoardIndex specifies the board index of the hardware and is usually 0. RemoteAddress specifies the remote address of the hardware. Note that to specify the remote address of 50 hex, you need to use the hex2dec function as shown in Examples.
The primary use cases involve the fread and fwrite functions. To identify I2C devices in the Instrument Control Toolbox, use the instrhwinfo function on the I2C interface, called i2c.
You can use these properties with the i2c object:
|BitRate||Must be a positive, nonzero value specified in kHz. The adaptor and chips determine the rate. The default is 100 kHz for both the Aardvark and NI-845x adaptors.|
|TargetPower||Aardvark only. Can be specified as none or both. The value both means to power both lines, if supported. The value none means power no lines, and is the default value.|
|PullupResistors||Can be specified as none or both.
The value both enables 2k pullup resistors to protect
hardware in the I2C device, if supported. This is the default value. |
Note that devices may differ in their use of pullups. The Aardvark adaptor and the NI-8452 have internal pullup resistors to tie both bus lines to VDD and can be programmatically set. The NI-8451 does not have internal pullup resistors that can be programmatically set, and so require external pullups. You should consult your device documentation to ensure that the correct pullups have been used.
|BoardSerial||Unique identifier of the I2C master communication device.|
|Vendor||Use to create i2c object. Must be set to aardvark, for use with Aardvark adaptor, or NI845x for use with the NI-845x adaptor.|
|BoardIndex||Use to create i2c object. Specifies the board index of the hardware. Usually set to 0.|
|RemoteAddress||Use to create i2c object. Specifies the remote address of the hardware. Specified as a string when you create the i2c object. For example, to specify the remote address of 50 hex, use '50h'.|
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.
Ensure that the Aardvark adaptor is installed so that you can use the i2c interface, and then look at the adaptor properties.
instrhwinfo('i2c') instrhwinfo('i2c', 'Aardvark')
Construct an i2c object called I using Vendor aardvark, with BoardIndex of 0, and RemoteAddress of 50h. Note that to specify the remote address of 50 hex, you need to use the hex2dec function as shown.
I = i2c('aardvark',0,hex2dec('50'));
Connect to the chip.
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(I,[0 'Hello Wo']);
The second byte of the string 'Hello World!' is 'rld!'. Its page address is 8.
A zero needs to be written to the i2c object, to start reading from the first byte of first page.
Read data back from the chip using the fread function. The chip returns the characters it was sent.
Disconnect the I2C device.
Clean up by clearing the object.
This example shows how to communicate with a sensor chip on a circuit board, with an address of 53 hex and a board index of 0, using the 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.
Ensure that the NI-845x adaptor is installed so that you can use the i2c interface, and then look at the adaptor properties.
instrhwinfo('i2c') instrhwinfo('i2c', 'NI845x')
Construct an i2c object called i2cobj using Vendor NI845x, with BoardIndex of 0, and RemoteAddress of 53h.
i2cobj = i2c('NI845x', 0, '53h');
Connect to the chip.
Write to the sensor chip. You need to read the documentation or data sheet of the chip in order to know what the remote address is and other information about the chip. In this case, the chip's registry can be opened by sending it a 0.
Read data back from the chip using the fread function. By sending it one byte, you can read back the device ID registry. In the case of this chip, the read-only device ID registry is 229. Therefore, that is what is returned when you send the byte.
fread(i2cobj, 1) ans = 229
Disconnect the I2C device.
Clean up by deleting and clearing the object.