Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用arduino C,无法通过I2C总线读取寄存器数据 #1

Open
techqu opened this issue Feb 11, 2023 · 0 comments
Open

使用arduino C,无法通过I2C总线读取寄存器数据 #1

techqu opened this issue Feb 11, 2023 · 0 comments

Comments

@techqu
Copy link

techqu commented Feb 11, 2023

一直无法读取I2C的数据,比如left的button,使用arduinoIDE,代码如下:

// 引入Wire库文件
#include <Wire.h>

void setup() {
  // Wire初始化, 加入i2c总线
  // 如果未指定,则以主机身份加入总线。
  Wire.begin();
  // 初始化串口并设置波特率为9600
  Serial.begin(9600);
}

void loop() {
  Wire.beginTransmission(90);
  Wire.write(34);
  int error = Wire.endTransmission(0);
  delay(200);
  if (error != 0) {
    Serial.println(error);
  } else {
    byte a = Wire.requestFrom(90,1);
    Serial.print(a);
    // 当从从设备接收到信息时值为true
    while (Wire.available()) {
      // 接受并读取从设备发来的一个字节的数据
      char c = Wire.read();
      // 向串口打印该字节
      Serial.print(c);
    }
  }
  // 延时500毫秒
  delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant