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 an Aardvark host adaptor.
I = i2c('Vendor', BoardIndex, RemoteAddress) constructs an i2c object associated with Vendor, BoardIndex, and RemoteAddress. Vendor must be set to aardvark, since you need to use a Total Phase Aardvark adaptor 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 the Aardvark adaptor.|
|TargetPower||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.|
|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.|
|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. Note that to specify the remote address
of 50 hex, you need to use the hex2dec function
as shown in Examples. |
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.