arduino - 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);
}
解决方案
推荐阅读
- python - 在 python 中导入 VLC 模块时出现属性错误
- r - 删除有条件的重复项
- java - 为什么这个消费者抱怨编译时错误?
- tensorflow - 在 Tensorflow 中 GPU 的工作速度比 CPU 慢,为什么?
- java - 如何在 JavaFX 中为网页加载过程制作进度条?
- javascript - 我需要在一个网页中制作多个模式
- android - 地理定位器出错:如果我在 pubspec.yaml 文件中添加 ^3.0.0,则无法构建 android 应用程序
- sql - SQL 父子和继承
- excel - Characters.FontStyle 不适用于具有自定义数字格式的单元格
- jquery - 如何在页面回发时停止 JQgrid 刷新或重新加载,并且 jggrid 应该保持不变