Go Down

Topic: FREESCALE MPL3115A2 PRESSURE SENSOR (Read 3 times) previous topic - next topic

8bit_Biker

Now I'm trying to receive the content of Data_PMSB_reg.
Here is my test:

Code: [Select]
#include <Wire.h>



void setup(){
/*Start communication */
Wire.begin();
Serial.begin(9600);
  // Put sensor as standby
    Wire.beginTransmission(0x60); //0x60 is sensor address
    Wire.write(0x26); //ctrl_reg
    Wire.write(0x00); //reset_reg
    Wire.endTransmission();
    delay(10);

// start sensor as Barometer Active
    Wire.beginTransmission(0x60);
    Wire.write(0x26);
    Wire.write(0x01); //start sensor as barometer
    Wire.endTransmission();
    delay(10);
    }

void getdata(byte *a, byte *b, byte *c){
   Wire.beginTransmission(0x60);
   Wire.write(0x01);        // Data_PMSB_reg address
   Wire.endTransmission();
   // now get the data from the sensor
   Wire.requestFrom(0x60, 3); // "please send me the contents of your first three registers"
   *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); //print aa for example
  delay(500);
}

8bit_Biker

I would like to give you some news:
with this code i could receive some strange value:
Code: [Select]
#include <Wire.h>

void setup(){
  Serial.begin(9600);
/*Start communication */
Wire.begin();

    }

void getdata(byte *a, byte *b, byte *c){
   Wire.beginTransmission(0x60);
   Wire.write(0x01);        // Data_PMSB_reg address
   Wire.endTransmission();
   // now get the data from the sensor
   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(){
    Serial.println("bli"); //print aa for example

    // Put sensor as standby
    Wire.beginTransmission(0x60); //0x60 is sensor address
    Wire.write(0x26); //ctrl_reg
    Wire.write(0x00); //reset_reg
    Wire.endTransmission();
    delay(10);

// start sensor as Barometer Active
    Wire.beginTransmission(0x60);
    Wire.write(0x26);
    Wire.write(0x01); //start sensor as barometer
    Wire.endTransmission();
    delay(10);
   
  byte aa,bb,cc;
  getdata(&aa,&bb,&cc);
  Serial.println(aa); //print aa for example
  Serial.println(bb); //print bb for example
  Serial.println(cc); //print cc for example
  Serial.println("bla"); //print aa for example

  delay(500);
}


("bli" and "bla" are test strings)

Value obtained by mine are "222 0 222" (E0 0 E0), and they never change even i change the barometer pressure. How can You explain this?

8bit_Biker

OK! I found where is the error!
I feel a little bit silly.... :smiley-eek:
Here the correct code to communicate with the sensor
Code: [Select]
#include <Wire.h>

void setup(){
  Serial.begin(9600);
/*Start communication */
Wire.begin();

    }

void getdata(byte *a, byte *b, byte *c){
   Wire.beginTransmission(0x60);
   Wire.write(0x01);        // Data_PMSB_reg address
   Wire.endTransmission();
   // now get the data from the sensor
   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(){
    // Put sensor as standby
    Wire.beginTransmission(0x60); //0x60 is sensor address
    Wire.write(0x26); //ctrl_reg
    Wire.write(0x00); //reset_reg
    Wire.endTransmission();
    delay(10);

// start sensor as Barometer Active
    Wire.beginTransmission(0x60);
    Wire.write(0x26);
    Wire.write(0x01); //start sensor as barometer
    Wire.endTransmission();
    delay(10);
   
  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);
}


Now i would like to convert the HEX value into Pascal: i need to create a library to do it. As wrote in the datasheet i must use this code:
Code: [Select]
void SCI_s18decun_Out (tbar_18 data)
{
byte a, b, c, d, e, f, g, h;
dword r;
/*
** Calculate
*/
a = (byte)((data.LWord>>6) / 10000000); //shift by 6
r = (data.LWord>>6) % 10000000;
b = (byte)(r / 1000000);
r %= 1000000;
c = (byte)(r / 100000);
r %= 100000;
d = (byte)(r / 10000);
r %= 10000;
e = (byte)(r / 1000);
r %= 1000;
f = (byte)(r / 100);
r %= 100;
g = (byte)(r / 10);
h = (byte)(r % 10);
** Format
*/
if (a == 0)
{
a = 0xF0;
if (b == 0)
{
b = 0xF0;
if (c == 0)
{
c = 0xF0;
if (d == 0) {
d = 0xF0;
if (e==0){
e = 0xF0;
if (f == 0){
f = 0xF0;
if ( g == 0 ) {
g = 0xF0;
}
}
}
}
}
}
}
/*
** Output result
*/
SCI_NibbOut (d);
SCI_NibbOut (e);
SCI_NibbOut (f);
SCI_NibbOut (g);
SCI_NibbOut (h);
}


and this:
Code: [Select]
void SCI_s2fracun_Out (tword data)
{
BIT_FIELD value;
word result;
byte a, b, c, d;
word r;
SCI_CharOut ('.');
/*
** Determine mantissa value
*/
result = 0;
value.Byte = data.Byte.hi;
if (value.Bit._5 == 1)
result += FRAC_2d1;
if (value.Bit._4 == 1)
result += FRAC_2d2;
/*
** Convert mantissa value to 2 decimal places
*/
r = result % 1000;
a = (byte)(result / 1000);
b = (byte)(r / 100);
r %= 100;
c = (byte)(r / 10);
d = (byte)(r % 10);
/*
** Output mantissa
*/
SCI_NibbOut (a);
SCI_NibbOut (b);
}

to convert the fractional component in OUT_P_LSB.

Could someones help me to create the ".CPP" library file?
Thank You all!

8bit_Biker

Nones could help me realizing the library for this sensor??  =(

8bit_Biker

Hi everybody,
this post reply only because I'v fixed my problem using the I2C library provided by this man! Thanks for the support!

http://dsscircuits.com/articles/arduino-i2c-master-library.html


Go Up