Micro:bit参考

Micro:bit参考 > Micropython API > I²C

I²C

The i2c module lets you communicate with devices connected to your board using the I²C bus protocol. There can be multiple slave devices connected at the same time, and each one has its own unique address, that is either fixed for the device or configured on it. Your board acts as the I²C master.
I2c 模块允许您使用 I²C 总线协议与连接到电路板的设备进行通信。可以同时连接多个从属设备,每个从属设备都有自己唯一的地址,该地址要么是固定的,要么是在其上配置的。您的电路板充当 I²C 主控器。

We use 7-bit addressing for devices because of the reasons stated here.
由于所述的原因,我们对设备使用 7 位寻址 这里 

This may be different to other micro:bit related solutions.
这可能与其他 micro:bit 相关的解决方案不同。

How exactly you should communicate with the devices, that is, what bytes to send and how to interpret the responses, depends on the device in question and should be described separately in that device’s documentation.
您应该如何与设备通信,即发送哪些字节以及如何解释响应,取决于相关设备,应在该设备的文档中单独描述。

Functions  函数 

microbit.i2c.init(freq=100000sda=pin20scl=pin19)

Re-initialize peripheral with the specified clock frequency freq on the specified sda and scl pins.
在指定的 sda 和 scl pins 上使用指定的 clock frequency freq 重新初始化外设。

Warning  警告

On a micro:bit V1 board, changing the I²C pins from defaults will make the accelerometer and compass stop working, as they are connected internally to those pins. This warning does not apply to the V2 revision of the micro:bit as this has separate I²C lines for the motion sensors and the edge connector.
在 micro:bit V1 板上,将 I²C 引脚从默认值更改为 I²C 引脚将使加速度计和指南针停止工作,因为它们在内部连接到这些引脚。此警告不适用于 V2 micro:bit 的修订版,因为它有单独的 I²C 线 用于运动传感器和 Edge 连接器。

microbit.i2c.scan()

Scan the bus for devices. Returns a list of 7-bit addresses corresponding to those devices that responded to the scan.
扫描总线中的设备。返回与响应扫描的设备相对应的 7 位地址列表。

microbit.i2c.read(addrnrepeat=False)

Read n bytes from the device with 7-bit address addr. If repeat is True, no stop bit will be sent.
使用 7 位地址 addr 从器件中读取 n 字节。如果重复 为 True,则不会发送停止位。

microbit.i2c.write(addrbufrepeat=False)

Write bytes from buf to the device with 7-bit address addr. If repeat is True, no stop bit will be sent.
使用 7 位地址 addr 将字节从 buf 写入设备。如果 repeat 为 True,则不会发送停止位。

Connecting  连接 

You should connect the device’s SCL pin to micro:bit pin 19, and the device’s SDA pin to micro:bit pin 20. You also must connect the device’s ground to the micro:bit ground (pin GND). You may need to power the device using an external power supply or the micro:bit.
你应该将设备的 SCL 引脚连接到 micro:bit 引脚 19,将设备的 SDA 引脚连接到 micro:bit 引脚 20。你还必须将设备的接地连接到 micro:bit 接地(引脚 GND)。你可能需要使用外部电源或 micro:bit 为设备供电。

There are internal pull-up resistors on the I²C lines of the board, but with particularly long wires or large number of devices you may need to add additional pull-up resistors, to ensure noise-free communication.
电路板的 I²C 线路上有内部上拉电阻,但对于特别长的电线或大量器件,您可能需要添加额外的上拉电阻,以确保无噪声通信。