Main Content

enableI2C

启用I2C接口

描述

例子

enableI2C(mypi启用I2C总线以其默认巴士的速度100000bps. You can then use the Raspberry Pi™ hardware board pins I2C1_SDA (GPIO 2) and I2C1_SCL (GPIO 3) as I2C pins and interface the Raspberry Pi board with any I2C device to exchange data. The I2C bus is enabled by default. To disable I2C, usedisableI2C

例子

enableI2C(mypi,,,,i2cbusspeed启用I2C总线并覆盖当前的总线速度。

例子

全部收缩

在Raspberry Pi内核上启用并禁用I2C接口,并分别使用Raspberry Pi硬件板引脚进行I2C功能和GPIO。从TMP102 I2C温度传感器与Raspberry Pi硬件板连接的TMP102 I2C温度传感器中读取温度数据。有关TMP102温度传感器的更多信息,请参阅其数据表。您还可以在Raspberry Pi和其他I2C设备之间交换数据。有关更多信息,请参阅特定I2C设备的数据表。在Raspberry Pi Linux上执行这些步骤®终端以确保在硬件内核中启用I2C接口。

  1. Run this command:

    sudo raspi-config

  2. 选择接口选项>I2C

    Raspberry Pi内核I2C接口选项选择Linux终端

  3. 选择是的when prompted to enable the I2C interface.

    Raspberry Pi Kernel I2C Interfacing enabling options

  4. 选择是的当提示时,自动加载I2C内核模块。

  5. 选择Finish

  6. 选择是的当提示重新启动时。

提示

You can also enable the I2C interface using theRaspberry Pi资源监视器应用

To create a connection from MATLAB®to the Raspberry Pi board, execute this command at the MATLAB Command Window.

mypi = raspi;
DeviceAddress: 'raspberrypi-hysdu8X38o' Port: 18734 BoardName: 'Raspberry Pi 3 Model B+' AvailableLEDs: {'led0'} AvailableDigitalPins: [4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27] AvailableSPIChannels: {‘CE0’,’CE1’} AvailableI2CBuses: {'i2c-1'} I2CBusSpeed: 100000 AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
GPIO标头销I2C_SDA (GPIO 2)andI2C_SCL(GPIO 3)用于I2C功能。GPIO引脚2和3未列出AvailableDigitalPinsproperty as the I2C functionality is enabled by default and the pins are currently being utilized by the I2C functionality.

To display the pin mapping of the Raspberry Pi hardware board, execute this command at the MATLAB Command Window.

Showpins(mypi);
覆盆子PI 3型B+ GPIO的I2C PIN标准

这revised pin map of the Raspberry Pi hardware board shows the location of the I2C pins,I2C_SDA (GPIO 2)andI2C_SCL(GPIO 3),在I2C-1bus.

在物理上连接你I2C-based蛋彩画ture sensor to the I2C pins, get the addresses of the I2C devices attached to the I2C bus'I2C-1'

scani2cbus(mypi,'I2C-1');
ans = 1×9个小区阵列{'0x48'} {'0x50'} {'0x51'} {'0x52'} {'0x53'} {'0x54'} {'0x54'} {'0x55'}'}

Create a connection from MATLAB to the I2C sensor at I2C address '0x48'。

tempsensor = i2cdev(mypi,'I2C-1',,,,'0x48');

这temperature reading of the sensor is digitized into12处于正常模式的位,8MSB和4LSB中的位。每个LSB等于0.0625摄氏摄氏度。编写寄存器地址以从第一个字节中读取,然后从中读取两个字节。将数据类型设置为UINT8

写(tempsensor,0x0,'uint8');data = read(tempsensor,2,'uint8');temp =(double(bitshift(int16(data(1)),4))) + double(bitshift(int16(data(data(2)),-4),-4))) * 0.0625;
温度= 30.0625

在禁用它之前,请清除Active I2C传感器连接。禁用I2C功能后,可以将Raspberry Pi I2C引脚用作GPIO引脚。

cleartempSensor;disablei2c(mypi);mypi = raspi;
DeviceAddress: 'raspberrypi-hysdu8X38o' Port: 18734 BoardName: 'Raspberry Pi 3 Model B+' AvailableLEDs: {'led0'} AvailableDigitalPins: [2 3 4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27] AvailableSPIChannels: {‘CE0’,’CE1’} AvailableI2CBuses: {} I2CBusSpeed: 100000 AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
GPIO 2andGPIO 3引脚现在可用,可以用作GPIO引脚。没有I2C巴士可用于I2C接口。

在再次使用I2C之前,启用I2C功能。

enablei2c(mypi);

When you enable I2C, you can change thei2cbusspeed(mypi)财产。

disablei2c(mypi);enablei2c(mypi,400000);i2cbusspeed(mypi);
ans = 400000

输入参数

全部收缩

连接到Raspberry Pi硬件板,指定为raspi目的。

I2Cbus speed, specified as either100000or400000

Example:100000

数据类型:双倍的

Introduced in R2014a