MPL115A2 (I2C) Pressure sensor interface

Dependents:   MPL115A2Sample dataloger_for_modelrocket ARLISS2012_Hidaka

Files at this revision

API Documentation at this revision

Comitter:
yamaguch
Date:
Sat May 14 17:29:51 2011 +0000
Parent:
1:b1d4ab9b1e84
Commit message:
0.91

Changed in this revision

MPL115A2.cpp Show annotated file Show diff for this revision Revisions of this file
MPL115A2.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
diff -r b1d4ab9b1e84 -r d77bd4340924 MPL115A2.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPL115A2.cpp	Sat May 14 17:29:51 2011 +0000
@@ -0,0 +1,47 @@
+#include "MPL115A2.h"
+
+#define c2f(ch, cl, nbits, fbits, zpad) float(short(ch << 8 | cl) >> 16 - nbits) / (1 << fbits + zpad)
+
+int MPL115A2::read(float *pressure, char *data) {
+    const char id = 0x60 << 1;
+    char cmd[2] = {0x12, 0x01}; // read both
+    char buf[16];
+
+    if (i2c.write(id, cmd, 2) != 0)
+        return -1;
+
+    wait_ms(1);
+
+    cmd[0] = 0;
+
+    if (i2c.write(id, cmd, 1, true) != 0)
+        return -1;
+
+    if (i2c.read(id, buf, 16) == 0) {
+        float padc = buf[0] << 2 | buf[1] >> 6;
+        float tadc = buf[2] << 2 | buf[3] >> 6;
+        float a0 = c2f(buf[4], buf[5], 16, 3, 0);
+        float b1 = c2f(buf[6], buf[7], 16, 13, 0);
+        float b2 = c2f(buf[8], buf[9], 16, 14, 0);
+        float c12 = c2f(buf[10], buf[11], 14, 13, 9);
+        float c11 = c2f(buf[12], buf[13], 11, 10, 11);
+        float c22 = c2f(buf[14], buf[15], 11, 10, 15);
+
+        float pcomp = a0 + (b1 + c11 * padc + c12 * tadc) * padc + (b2 + c22 * tadc) * tadc;
+
+        if (pressure != 0)
+            *pressure = pcomp * 650 / 1023 + 500;
+
+        if (data != 0)
+            memcpy(data, buf, 16);
+
+        return 0;
+    }
+
+    return -1;
+}
+
+float MPL115A2::readPressure() {
+    float pressure;
+    return read(&pressure, 0) == 0 ? pressure : 0;
+}
\ No newline at end of file
diff -r b1d4ab9b1e84 -r d77bd4340924 MPL115A2.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPL115A2.h	Sat May 14 17:29:51 2011 +0000
@@ -0,0 +1,52 @@
+#ifndef MPL115A2
+#define MPL115A
+
+#include "mbed.h"
+
+/**
+ * MPL115A2 (I2C) pressure sensor
+ */
+
+class MPL115A2 {
+public:
+    /**
+    * Create an MPL115A(I2C) interface with specified pins
+    *
+    * @param PinName sda SDA pin (default = p9)
+    * @param PinName scl SCL pin (default = p10)
+    */
+    MPL115A2(PinName sda = p9, PinName scl = p10) : i2c(sda, scl) {
+    }
+
+    /**
+    * Read current pressure and/or temperature
+    *
+    * @param puressure pointer to a float area to receive pressure
+    * @param padc pointer to an int area to receive pressure adc value
+    * @param tadc pointer to an int area to receive temperature adc value
+    * @param data pointer to an array of adc's and coefficients (byte array of size 16)
+    *
+    * @returns 0 (success), or -1 (I2C error)
+    */
+
+    int read(float *pressure, char *data);
+    
+    /**
+    * Read current pressure
+    *
+    * @returns current pressure (in hPa)
+    */
+    float readPressure();
+
+    /**
+    * Operator shorthand for readPressure()
+    */
+    operator float() {
+        return readPressure();
+    }
+
+private:
+    I2C i2c;
+};
+
+#endif
\ No newline at end of file
diff -r b1d4ab9b1e84 -r d77bd4340924 main.cpp
--- a/main.cpp	Tue Apr 26 11:30:35 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-#include "mbed.h"
-#include "MPL115A2.h"
-
-int main() {
-    MPL115A2 mpl115a2(p28, p27);
-
-    while (1) {
-        printf("temperature = %3.1f degree, pressure = %d hPa\n",
-               mpl115a2.readTemperature(), mpl115a2.readPressure());
-        wait(1);
-    }
-}
\ No newline at end of file
diff -r b1d4ab9b1e84 -r d77bd4340924 mbed.bld
--- a/mbed.bld	Tue Apr 26 11:30:35 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912