UAVRO / ms5611

Dependents:   Q2_Stabi

Fork of ms5611 by Kevin Braun

Files at this revision

API Documentation at this revision

Comitter:
loopsva
Date:
Mon Dec 22 23:38:34 2014 +0000
Parent:
6:41c370fa1f7b
Child:
8:461f68bc94f2
Commit message:
Added second constructor so user can choose the polarity of the ms5611's CSB pin.

Changed in this revision

ms5611.cpp Show annotated file Show diff for this revision Revisions of this file
ms5611.h Show annotated file Show diff for this revision Revisions of this file
--- a/ms5611.cpp	Mon Jun 24 19:11:28 2013 +0000
+++ b/ms5611.cpp	Mon Dec 22 23:38:34 2014 +0000
@@ -24,10 +24,26 @@
 uint32_t C[8];                  //coefficient storage
 
 //--------------------------------------------------------------------------------------------------------------------------------------//
-// Constructor and destructor
+// Constructor and destructor - default to be compatible with legacy m5611 driver
 
 ms5611::ms5611(PinName sda, PinName scl)  : _i2c(sda, scl) {
         _i2c.frequency(400000);
+        _i2cWAddr = MS5611_ADDR_W;
+        _i2cRAddr = MS5611_ADDR_R;
+}
+
+//--------------------------------------------------------------------------------------------------------------------------------------//
+// Constructor and destructor - new, to allow for user to select i2c address based on CSB pin
+
+ms5611::ms5611(PinName sda, PinName scl, CSBpolarity CSBpin)  : _i2c(sda, scl) {
+        _i2c.frequency(400000);
+        _i2cWAddr = MS5611_ADDR_W;
+        _i2cRAddr = MS5611_ADDR_R;
+        if(CSBpin == CSBpin_1) {
+            _i2cWAddr -= 2;
+            _i2cRAddr -= 2;
+        
+        }
 }
 
 //********************************************************
@@ -40,9 +56,9 @@
     int twst;
     _i2c.start();
     if(readMode == true) {
-        twst = m_i2c_write(MS5611_ADDR_R);
+        twst = m_i2c_write(_i2cRAddr);
     } else {
-        twst = m_i2c_write(MS5611_ADDR_W);
+        twst = m_i2c_write(_i2cWAddr);
     }
     return(twst);
 }
@@ -142,7 +158,7 @@
     }
     m_i2c_send(MS5611_CMD_ADC_READ);
     
-    ret = _i2c.read(MS5611_ADDR_R, cobuf, 3, false);
+    ret = _i2c.read(_i2cRAddr, cobuf, 3, false);
     if(ret) printf("\n*** ms5611 ADC Read Error ");
     temp = (cobuf[0] << 16) + (cobuf[1] << 8) + cobuf[2];
     return temp;
@@ -161,7 +177,7 @@
     cobuf[0] = 0;
     cobuf[1] = 0;
     m_i2c_send(MS5611_CMD_PROM_RD + coef_num * 2); // send PROM READ command
-    ret = _i2c.read(MS5611_ADDR_R, cobuf, 2, false);
+    ret = _i2c.read(_i2cRAddr, cobuf, 2, false);
     if(ret) printf("\n*** ms5611 PROM Read Error ");
     rC = cobuf[0] * 256 + cobuf[1];
     return rC;
--- a/ms5611.h	Mon Jun 24 19:11:28 2013 +0000
+++ b/ms5611.h	Mon Dec 22 23:38:34 2014 +0000
@@ -41,7 +41,10 @@
  * #include "mbed.h"
  * #include "ms5611.h" 
  *
- * ms5611 ms(p9, p10);                        // i2c pins used
+ * //ms5611 ms(p9, p10);                        // i2c pins used
+ * ms5611 ms(p9, p10, ms5611::CSBpin_0);      // NEW!! with rev 7. User can set polarity of CSB pin
+ * //ms5611 ms(p9, p10, ms5611::CSBpin_1);
+ *
  * Serial pc(USBTX, USBRX);                   // local terminal interface
  *
  *
@@ -74,7 +77,7 @@
  
 //_____ M A C R O S
 
-#define MS5611_ADDR_W 0xEE // Module address write mode
+#define MS5611_ADDR_W 0xEE // Module address write mode (CSBpin = 0);
 #define MS5611_ADDR_R 0xEF // Module address read mode
 #define MS5611_CMD_RESET 0x1E // ADC reset command
 #define MS5611_CMD_ADC_READ 0x00 // ADC read command
@@ -96,11 +99,27 @@
 class ms5611 {
 
 public:
+    enum CSBpolarity {
+        CSBpin_0,  //CSB pin is grounded, I2C address is 0xEE and 0xEF
+        CSBpin_1,  //CSB pin is tied to Vdd, I2C address is 0xEC and 0xED
+    };
     /** Create a MS5611 object using the specified I2C object
-     *
-     * @param constructor, - the I2C object to communicate with
+     *   - User fixed I2C address 0xEE, CSB pin = 0
+     *   - This is the default legacy constructor
+     * @param sda - mbed I2C interface pin
+     * @param scl - mbed I2C interface pin
      */
     ms5611(PinName sda, PinName scl);
+    /** Create a MS5611 object using the specified I2C object
+     *   - User defined use of the CSB pin
+     *   - CSB pin = 0, user set I2C address to 0xEE
+     *   - CSB pin = 1, user set I2C address to 0xEC 
+     * @param sda - mbed I2C interface pin
+     * @param scl - mbed I2C interface pin
+     * @param ms5611::CSBpin_0 - CSB pin tied to ground
+     * @param ms5611::CSBpin_1 - CSB pin tied to VDD
+     */
+    ms5611(PinName sda, PinName scl, CSBpolarity CSBpin);
     /** Initialize the MS5611 and set up the coefficients
      *    First - reset the MS5611
      *    Second - load coefficient values from the MS5611 PROM
@@ -150,6 +169,8 @@
     float getSeaLevelBaroM(float known_alt);
     
 private:
+    char _i2cWAddr;
+    char _i2cRAddr;
     int m_i2c_start(bool readMode);
     void m_i2c_stop(void);
     unsigned char m_i2c_write(unsigned char data);