Lib to read the MCP9808 over Initialized I2C bus

Files at this revision

API Documentation at this revision

Comitter:
mederic
Date:
Tue Apr 12 09:02:48 2016 +0000
Commit message:
1st realease

Changed in this revision

MCP9808.cpp Show annotated file Show diff for this revision Revisions of this file
MCP9808.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 46326feade89 MCP9808.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP9808.cpp	Tue Apr 12 09:02:48 2016 +0000
@@ -0,0 +1,60 @@
+#include "MCP9808.h"
+
+//***********************************/************************************
+//                              Constant                                //
+//***********************************/************************************
+#define MCP9808_I2C_ADDR    0x30 //( 18<<1)
+
+#define MCP9808_REG_CONFIG  0x01
+#define MCP9808_REG_TUPPER  0x02
+#define MCP9808_REG_TLOWER  0x03
+#define MCP9808_REG_TCRIT   0x04
+#define MCP9808_REG_TAMB    0x05
+#define MCP9808_REG_MANID   0x06
+#define MCP9808_REG_DEVID   0x07
+#define MCP9808_REG_RESOL   0x08
+
+#define MCP9808_TAMB_MASK   0x0F
+#define MCP9808_SIGN_POS    0x03
+
+//***********************************/************************************
+//                         Constructors                                 //
+//***********************************/************************************
+MCP9808::MCP9808(I2C *i2c, bool a0, bool a1, bool a2):_i2c(i2c)
+{
+    _addr = (MCP9808_I2C_ADDR | (a2<<3) | (a1<<2) | (a0<<1));
+}
+
+float MCP9808::getTemp(void)
+{
+    char reg = MCP9808_REG_TAMB;
+    float ta;
+    char buf[2];
+
+    _i2c->write(_addr, &reg, 1);
+    _i2c->read(_addr, buf, 2);
+    
+    /*
+    if(buf[0] & (1<<MCP9808_SIGN_POS))
+    {
+        buf[0] &= MCP9808_TAMB_MASK;
+        
+        ta = 256 - (buf[0]*16 + buf[1]*0.0625); 
+    }
+    else
+    {
+        buf[0] &= MCP9808_TAMB_MASK;
+        ta = (buf[0]*16 + buf[1]*0.0625);
+    }*/
+    
+    ta = ((buf[0]&MCP9808_TAMB_MASK) << 8) | buf[1];
+    ta /= 16.0;
+    
+    if(buf[0] & (1<<MCP9808_SIGN_POS))
+    {
+        ta -= 256;
+    }
+      
+    return ta;  
+}
+
diff -r 000000000000 -r 46326feade89 MCP9808.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP9808.h	Tue Apr 12 09:02:48 2016 +0000
@@ -0,0 +1,67 @@
+#ifndef MPC9808_H
+#define MPC9808_H
+
+#include "mbed.h"
+
+/** MCP9808 class.
+ *  Used for read MCP9808 ±0.5°C Maximum Accuracy Digital Temperature Sensor
+ *
+ * Example:
+ * @code
+ *#include "mbed.h"
+ *#include "MCP9808.h"
+ *
+ *DigitalOut myled(LED1);
+ *I2C i2cBus(D14,D15);
+ *MCP9808 therm(&i2cBus, true, true, true);
+ *
+ *int main() 
+ *{
+ *   while(1) 
+ *   {
+ *       myled = !myled; // Toggle LED
+ *       wait(0.2); // 200 ms
+ *       printf("Temperature=%03.6f*C\r\n",therm.getTemp());
+ *   }
+ *}
+ * @endcode
+ */  
+class MCP9808
+{
+    public:
+        /** Create MCP9808 instance connected to I2C bus
+        * @param *i2c I2C bus already initialized
+        * @param a0 bool indicate a0 pin state to compute I2C address
+        * @param a1 bool indicate a1 pin state to compute I2C address
+        * @param a2 bool indicate a2 pin state to compute I2C address
+        */
+        MCP9808(I2C *i2c, bool a0=false, bool a1=false, bool a2=false);
+        
+        /**Get MCP9808 abient temperature
+        * @returns temperature [°C]
+        */    
+        float getTemp(void);
+        
+#ifdef MBED_OPERATORS
+    /** An operator shorthand for getTemp()
+     *
+     * The float() operator can be used as a shorthand for getTemp() to simplify common code sequences
+     *
+     * Example:
+     * @code
+     * float x = therm.getTemp();
+     * float x = therm;
+     *
+     * if(therm.getTemp() > 20.25) { ... }
+     * if(therm > 20.25) { ... }
+     * @endcode
+     */
+    operator float(){return getTemp();}
+#endif
+              
+    private:
+        I2C *_i2c;
+        int _addr;
+};
+
+#endif