首页 > 解决方案 > Wire.read() 仅返回 7 位字节

问题描述

一般来说,我是 Arduino 编程和 I2C 的新手。我正在使用 Arduino Micro 从电容传感器 AD7150 中提取数据。

现在在尝试自己回答我的问题时,我已经看到了很多 Wire I2C 通信的基本设置,我觉得我已经搞定了。但由于某种原因,Wire.read() 只返回 7 位字节给我,而它应该是我所看到和理解的所有内容中的 8 位。另一个问题是,即使我读出了一个应该稳定的寄存器,即芯片 ID 寄存器,输出也会随着时间的推移而“摆动”。到目前为止,谷歌还没有提供帮助。也许你们中的一个人有神奇的答案?

#include <Wire.h>

#define AD 0x48
#define ID 0x17

byte data1;
byte data2;

uint16_t data_combined;


void setup() {

  analogReference(EXTERNAL);
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
  Wire.beginTransmission(AD); 
 
  Wire.write(ID);   
  Wire.endTransmission(); // I checked the output; is successful

delay(10);

Wire.requestFrom(0x48,2); // Unsure whether to ask for 1 or 2 bytes here; the ChipID, as far as I understand, should only be one byte, but I've been told that I2C transmissions always include at least 2 bytes

data1 = Wire.read(); // so I just save both bytes separately here.
data2 = Wire.read();

data_combined = word(data1,data2);

Serial.print("First byte from Chip ID register: ");
Serial.print(data1,BIN);
Serial.print(" | Second byte from Chip ID register: ");
Serial.print(data2,BIN);
Serial.print(" | Combined to make: ");
Serial.println(data_combined,BIN);
Serial.println("---");

  delay( 200);
}

标签: arduino

解决方案


推荐阅读