有沒有人有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傳感器進行通信,沒有任何問題。
它完美地解決了我的需求!非常非常感謝你!!! –