Skip to content

Commit

Permalink
added getPT() to get both simultaneously
Browse files Browse the repository at this point in the history
This can get both pressure and temperature simultaneously, saving a
little time and eliminating repetitive code in getPressure() and
getTemperature() while retaining the functionality.
  • Loading branch information
sellensr committed Nov 7, 2012
1 parent daed02b commit b8a8830
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
63 changes: 28 additions & 35 deletions Adafruit_MPL115A2.cpp
Expand Up @@ -17,6 +17,7 @@
v1.0 - First release
v1.1 - Rick Sellens added casts to make bit shifts work below 22.6C
- get both P and T with a single call to getPT
*/
/**************************************************************************/
#if ARDUINO >= 100
Expand Down Expand Up @@ -65,10 +66,10 @@ void Adafruit_MPL115A2::readCoefficients() {
Wire.endTransmission();

Wire.requestFrom(MPL115A2_ADDRESS, 8);
a0coeff = ((i2cread() << 8) | i2cread());
b1coeff = ((i2cread() << 8) | i2cread());
b2coeff = ((i2cread() << 8) | i2cread());
c12coeff = (((i2cread() << 8) | i2cread())) >> 2;
a0coeff = (( (uint16_t) i2cread() << 8) | i2cread());
b1coeff = (( (uint16_t) i2cread() << 8) | i2cread());
b2coeff = (( (uint16_t) i2cread() << 8) | i2cread());
c12coeff = (( (uint16_t) (i2cread() << 8) | i2cread())) >> 2;

/*
Serial.print("A0 = "); Serial.println(a0coeff, HEX);
Expand Down Expand Up @@ -120,31 +121,10 @@ void Adafruit_MPL115A2::begin() {
*/
/**************************************************************************/
float Adafruit_MPL115A2::getPressure() {
uint16_t pressure, temp;
float pressureComp;

// Get raw pressure and temperature settings
Wire.beginTransmission(MPL115A2_ADDRESS);
i2cwrite((uint8_t)MPL115A2_REGISTER_STARTCONVERSION);
i2cwrite((uint8_t)0x00);
Wire.endTransmission();

// Wait a bit for the conversion to complete (3ms max)
delay(5);

Wire.beginTransmission(MPL115A2_ADDRESS);
i2cwrite((uint8_t)MPL115A2_REGISTER_PRESSURE_MSB); // Register
Wire.endTransmission();

Wire.requestFrom(MPL115A2_ADDRESS, 4);
pressure = (( (uint16_t) i2cread() << 8) | i2cread()) >> 6;
temp = (( (uint16_t) i2cread() << 8) | i2cread()) >> 6;
float pressureComp,centigrade;

// See datasheet p.6 for evaluation sequence
pressureComp = _mpl115a2_a0 + (_mpl115a2_b1 + _mpl115a2_c12 * temp ) * pressure + _mpl115a2_b2 * temp;

// Return pressure as floating point value
return ((65.0F / 1023.0F)*(float)pressureComp) + 50;
getPT(&pressureComp, &centigrade);
return pressureComp;
}


Expand All @@ -154,6 +134,18 @@ float Adafruit_MPL115A2::getPressure() {
*/
/**************************************************************************/
float Adafruit_MPL115A2::getTemperature() {
float pressureComp, centigrade;

getPT(&pressureComp, &centigrade);
return centigrade;
}

/**************************************************************************/
/*!
@brief Gets both at once and saves a little time
*/
/**************************************************************************/
void Adafruit_MPL115A2::getPT(float *P, float *T) {
uint16_t pressure, temp;
float pressureComp;

Expand All @@ -171,16 +163,17 @@ float Adafruit_MPL115A2::getTemperature() {
Wire.endTransmission();

Wire.requestFrom(MPL115A2_ADDRESS, 4);

pressure = (( (uint16_t) i2cread() << 8) | i2cread()) >> 6;
temp = (( (uint16_t) i2cread() << 8) | i2cread()) >> 6;
//Serial.print("t = "); Serial.println(temp, HEX);
float centigrade = temp;
centigrade -= 498;
centigrade /= -5.35;
centigrade += 25;

// See datasheet p.6 for evaluation sequence
pressureComp = _mpl115a2_a0 + (_mpl115a2_b1 + _mpl115a2_c12 * temp ) * pressure + _mpl115a2_b2 * temp;

// Return pressure and temperature as floating point values
*P = ((65.0F / 1023.0F) * pressureComp) + 50.0F; // kPa
*T = ((float) temp - 498.0F) / -5.35F +25.0F; // C

return centigrade;
}



1 change: 1 addition & 0 deletions Adafruit_MPL115A2.h
Expand Up @@ -55,6 +55,7 @@ class Adafruit_MPL115A2{
void begin(void);
float getPressure(void);
float getTemperature(void);
void getPT(float *P, float *T);

private:
float _mpl115a2_a0;
Expand Down
30 changes: 30 additions & 0 deletions examples/getPT/getPT.ino
@@ -0,0 +1,30 @@
#include <Wire.h>
#include <Adafruit_MPL115A2.h>

Adafruit_MPL115A2 mpl115a2;

void setup(void)
{
Serial.begin(9600);
Serial.println("Hello!");

Serial.println("Getting barometric pressure ...");
mpl115a2.begin();
}

void loop(void)
{
float pressureKPA = 0, temperatureC = 0;

mpl115a2.getPT(&pressureKPA,&temperatureC);
Serial.print("Pressure (kPa): "); Serial.print(pressureKPA, 4); Serial.print(" kPa ");
Serial.print("Temp (*C): "); Serial.print(temperatureC, 1); Serial.println(" *C both measured together");

pressureKPA = mpl115a2.getPressure();
Serial.print("Pressure (kPa): "); Serial.print(pressureKPA, 4); Serial.println(" kPa");

temperatureC = mpl115a2.getTemperature();
Serial.print("Temp (*C): "); Serial.print(temperatureC, 1); Serial.println(" *C");

delay(1000);
}

0 comments on commit b8a8830

Please sign in to comment.