2012-11-15 61 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

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