如何使用 Arduino Wire 库读取 8 位 I2C 寄存器:最小示例

以下代码演示如何通过 I2C 读取 1 字节(8 位)长的寄存器。它几乎适用于所有 I2C 设备如 EEPROM、ADC 等,前提是你有正确的配置。注意某些设备如 LAN9303 有略微不同的寻址方案或其他特性。在我看来,最有效的方法是先尝试标准的寄存器读取方式并从那里开始。

**注意此代码为了简单起见未实现错误处理。**此外,我们使用 delay() 而不是 Wire.available() 等待数据。这是一个最小示例,因此对读者造成的困惑最小。我们将在后续文章中提供带错误处理的完整示例。

delay(2); // 等待数据可用

i2c_read_8bit_example.cpp
const uint8_t SLAVE_I2C_ADDRESS = 0b1010;
const uint16_t SLAVE_I2C_REGISTER_ADDRESS = 0x50;

Wire.beginTransmission(SLAVE_I2C_ADDRESS);
Wire.write(SLAVE_I2C_REGISTER_ADDRESS);
Wire.endTransmission();
Wire.requestFrom(SLAVE_I2C_ADDRESS, 1); // This register is 8 bits = 1 byte long
delay(2); // 等待数据可用
// 直接读入 uint8_t
uint8_t buf = (uint8_t)Wire.read();
// 打印寄存器值
Serial.printf("Register value: %02x\r\n", buf);

另请参阅:


Check out similar posts by category: Arduino, C/C++, Electronics, Embedded, PlatformIO