2012-11-15 60 views
0

有沒有人有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傳感器進行通信,沒有任何問題。

回答

1

您的問題可能是由於飛思卡爾器件需要重複啓動I2C通信來讀取。原始的Arduino雙線庫(Wire使用的TWI庫)不支持Repeated-Start。

我知道這一點,因爲我不得不爲我的項目之一重寫TWI以支持重複啓動(中斷驅動,主控和從屬)。不幸的是,我從來沒有得到上傳我的代碼,但其他人在這裏做了基本相同的事情(至少對於你需要的Master): http://dsscircuits.com/articles/arduino-i2c-master-library.html

丟失Wire庫並使用它們的I2C庫代替。

+0

它完美地解決了我的需求!非常非常感謝你!!! –