C++ 使用I2C协议从磁镜读取值
我对这一切还很陌生。如果有什么明显的事情,请原谅 我一直在为磁镜的数据表而挣扎。出于某种原因,似乎一切都在运转,但当我向它挥动磁铁时,我并没有在序列中得到任何响应 这里有一些信息C++ 使用I2C协议从磁镜读取值,c++,arduino,microcontroller,C++,Arduino,Microcontroller,我对这一切还很陌生。如果有什么明显的事情,请原谅 我一直在为磁镜的数据表而挣扎。出于某种原因,似乎一切都在运转,但当我向它挥动磁铁时,我并没有在序列中得到任何响应 这里有一些信息 #include <Wire.h> void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); // start serial communication at 960
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
}
void loop() {
int reading = 0;
int Address = 30;
Wire.beginTransmission(Address);
Wire.write(byte(0x03));
Wire.write(byte(0x04));
Wire.endTransmission();
Wire.requestFrom(Address, 2);
delay(10);
if (2 <= Wire.available()) {
reading = Wire.read();
reading = reading << 8;
reading |= Wire.read();
Serial.println(int(reading));
}
delay(250); // wait a bit since people have to read the output :)
}
但是如果我删除下面的行Wire.write(字节(0x03))代码>,我的输出不变。来自设备的值应该表示为2的补码
所以一开始我以为我不知道如何向设备发送多个字节,但经过一些研究,我发现我做得对(我想)
然后,如果我只放入Wire.write(字节(0x03))代码>我收到“0”作为响应。阅读数据表时,我发现响应0表示该命令无效
我在这篇文章中包括了数据表。有人能给我指出正确的方向吗?我正在使用的IC是一个LSM303DLHC,我在这个“”中使用它
这是你的电话号码
下图是公交车通信的图片
我相信下面的代码可以做到这一点,就像数据表中的表11:
Wire.beginTransmission(Address); // START and write to device address 0x1E
Wire.write(byte(0x03)); // Set the register pointer to sub-address 0x03
Wire.write(byte(0x04)); // Write a value of 0x04 to sub-address 0x3
Wire.endTransmission(); // STOP.
然后我怀疑设备的寄存器指针会自动从寄存器0x03递增到0x04。然后,代码的其余部分可能会从子地址0x04和0x05读取两个字节
您没有表达代码的意图,但我怀疑上面的内容不是您的意图。我猜您打算从设备地址0x1E、子地址0x03和子地址0x04读取两个字节。是这样吗
您应该执行如表13所述的操作
Wire.beginTransmission(Address); // START and write to device address 0x1E
Wire.write(byte(0x03)); // Set the register pointer to sub-address 0x03
Wire.requestFrom(Address, 2); // REPEAT-START and read 2 bytes from sub-address 0x03 and 0x04
请出示一张MCVE。目前您没有包含标题,并且省略了读取
的声明和设置()
函数。@MarkSetchell抱歉,感谢您的回复,您的权利,我正在考虑显示我认为可能存在问题的内容,但您的权利我现在将更新此内容。你能告诉我MCVE是什么意思吗?它在这里。。。该API采用7位地址(30-0x1E)。从作用域开始,库正在正确设置r/w位。我认为问题在于,当第20页指定需要写-读操作时,OP正在进行写和读操作。尝试结束传输(错误)。也不知道他为什么要将0x04写入0x03寄存器。请在此处查看第78行之后的内容。。。
Wire.beginTransmission(Address); // START and write to device address 0x1E
Wire.write(byte(0x03)); // Set the register pointer to sub-address 0x03
Wire.requestFrom(Address, 2); // REPEAT-START and read 2 bytes from sub-address 0x03 and 0x04