Main Content

通过I2C接口传输数据

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 theI2C界面。

Aardvark示例

此示例显示了如何使用Aardvark适配器在电路板上与电路板上的EEPROM芯片进行通信,其地址为0。

与EEPROM芯片交流:

  1. 确保安装AardVark适配器,以便您可以使用I2C界面。

    instrhwinfo('i2c'
    ans = hardwareInfo具有属性:inplantedAdaptors:{'aardvark''ni845x'} jarfileversion:'版本4.1'对硬件的访问可以由支持软件包提供。金宝app转到“支持软件包安装金宝app程序”以了解更多信息。
  2. 查看适配器属性。

    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.

    确保已安装了Aardvark软件驱动程序,并且Aardvark.dll在MATLAB路径上。有关详细信息,请参阅I2CInterface Usage Requirements and Guidelines

  3. Create the I2C object calledEEPROM,使用以下属性。

    %供应商= aardvark%boardIndex = 0%remoteadDress = 50Heeprom = i2c(“ aardvark”,0,'50h'
    I2CObject : 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

    您必须提供这三个输入来创建对象。要确定芯片的地址,请咨询其文档或数据表。您还可以通过扫描测试和测量工具中的仪器来找到地址。在工具中,右键单击I2C节点和选择扫描I2C适配器。扫描发现的任何芯片均在硬件树中列出。清单包括芯片的遥控地址。

  4. 连接到芯片。

    fopen(eeprom)
  5. '你好世界!'到Eeprom芯片。数据是在I2C中逐页编写的。每个页面包含八个字节。在编写的每个数据字节之前,需要提及页面地址。

    The first byte of the string'你好世界!''你好哇'。它的页面地址为0。

    fwrite(Eeprom,[0'你好哇')))

    字符串的第二个字节'你好世界!''rld!''。它的页面地址是8。

    fwrite(Eeprom,[8'rld!'')))
  6. 使用芯片读取数据弗雷德功能。芯片返回角色发送给它的角色。

    To start reading from the first byte of the first page, write a zero to theI2Cobject.

    fwrite(eeprom,0)
    char(fread(Eeprom,12))'
    ans = 'Hello World!'
  7. Disconnect the I2C device and clear the object from the workspace.

    fclose(Eeprom)清晰EEPROM

NI USB-845X示例

这个例子展示了何w 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:

  1. Ensure that the NI USB-845x adapter is installed so that you can use theI2C界面。

    instrhwinfo('i2c'
    ans = hardwareInfo具有属性:inplantedAdaptors:{'aardvark''ni845x'} jarfileversion:'版本4.1'对硬件的访问可以由支持软件包提供。金宝app转到“支持软件包安装金宝app程序”以了解更多信息。
  2. 查看NI USB-845X适配器属性。

    instrhwinfo('i2c',,,,'NI845x'
    ans = hardwareInfo具有属性:adaptorDllllname:'c:\ program文件\ matlab \ r2019b \ r2019b \ toolbox \ instrument \ instrument \ instrundaptors \ win64 \ mwni845xi2c.dll'adaptordllllllllsion:'adaptorname:'adaptorname:'adaptorname:'adaptorname:'ni8455x'dougn'sabledsinsInsin Indi niruse's.已安装板ID:[1×0 double]检测到的板:{0×1个cell} objectConstructorname:'i2c('ni845x',boardIndex,remoteaddress);''vendordllname:'ni845x.dll'VendordriverDescription:“国家仪器NI USB 845X驱动程序”对您的硬件的访问权限可能由支持包提供。金宝app转到“支持软件包安装金宝app程序”以了解更多信息。

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

  3. Create the I2C object calledi2cobj,使用这些属性:

    % Vendor = NI845x%boardIndex = 0%remoteadDress = 53hi2cobj = i2c('NI845x',0,'53H');

    您必须提供这三个输入来创建对象。要确定芯片的地址,请咨询其文档或数据表。您还可以通过扫描测试和测量工具中的仪器来找到地址。在工具中,右键单击I2C节点和选择扫描I2C适配器。扫描发现的任何芯片均在硬件树中列出。清单包括芯片的遥控地址。

  4. 连接到芯片。

    fopen(i2cobj)
  5. 写入传感器芯片。阅读芯片的文档或数据表,以了解远程地址以及有关芯片的其他信息。通常,芯片制造商会提供单独的读写地址。适配器板仅采用一个地址(读取地址),然后处理转换以读取和编写地址。

    In this case, the chip’s device ID register is at address0,因此您需要为芯片写一个0,以指示您想读或写入寄存器。

    fwrite(i2cobj,0)
  6. 使用芯片读取数据弗雷德功能。通过发送一个字节,您可以读回设备ID注册表。对于此芯片,仅读取设备ID注册表为229。

    弗雷德(i2cobj,1)
    ANS = 229
  7. Disconnect the I2C device and clear the object from the workspace.

    fclose(i2cobj)清晰i2cobj

也可以看看

||||

Related Topics