有没有人有MPL3115A2飞思卡尔I2C压力传感器的使用经验? 我需要在关于Arduino UNO r3的项目中使用它,但是我无法正确地在它们之间进行通信。这里是我的代码:飞思卡尔压力传感器MPL3115A2与Arduino的I2C通信
#include <Wire.h>
void setup(){
Serial.begin(9600);
/*Start communication */
Wire.begin();
// Put sensor as in Standby mode
Wire.beginTransmission((byte)0x60); //0x60 is sensor address
Wire.write((byte)0x26); //ctrl_reg
Wire.write((byte)0x00); //reset_reg
Wire.endTransmission();
delay(10);
// start sensor as Barometer Active
Wire.beginTransmission((byte)0x60);
Wire.write((byte)0x26); //ctrl_reg
Wire.write((byte)0x01); //start sensor as barometer
Wire.endTransmission();
delay(10);
}
void getdata(byte *a, byte *b, byte *c){
Wire.beginTransmission(0x60);
Wire.write((byte)0x01); // Data_PMSB_reg address
Wire.endTransmission(); //Stop transmission
Wire.requestFrom(0x60, 3); // "please send me the contents of your first three registers"
while(Wire.available()==0);
*a = Wire.read(); // first received byte stored here
*b = Wire.read(); // second received byte stored here
*c = Wire.read(); // third received byte stored here
}
void loop(){
byte aa,bb,cc;
getdata(&aa,&bb,&cc);
Serial.println(aa,HEX); //print aa for example
Serial.println(bb,HEX); //print bb for example
Serial.println(cc,HEX); //print cc for example
delay(5000);
}
我收到的数据是:05FB9(例如)。当我更改寄存器地址(请参阅Wire.write((byte)0x01); // Data_PMSB_reg address
)时,我期望数据发生变化,但它不会!你能解释一下吗? 你可以找到文档和数据表on the NXP website。
我无法正确理解他们如何相互沟通。我用Arduino和一些其他具有相同通信协议的I2C传感器进行通信,没有任何问题。
它完美地解决了我的需求!非常非常感谢你!!! –