htu21d_for_weather_shield

Dependents:   SPARKFUN_WEATHER_SHIELD

Fork of htu21d by Kevin Braun

Revision:
1:d3ed713f8354
Parent:
0:2dab43acb3a4
Child:
2:8fbe84ed61e6
--- a/htu21d.cpp	Wed May 14 00:31:30 2014 +0000
+++ b/htu21d.cpp	Thu May 15 19:12:11 2014 +0000
@@ -1,7 +1,9 @@
 /**
-HTU21D driver for mbed.  Includes RTOS hooks if RTOS is detected during compile
+HTU21D / HPP828E031 driver for mbed.
+- Includes RTOS hooks if RTOS is detected during compile.
 Author: Kevin Braun
 **/
+
 #include "htu21d.h"
 
 #ifdef RTOS_H
@@ -12,11 +14,15 @@
 //Contstructor
 
 htu21d::htu21d(PinName sda, PinName scl) : _i2c(sda, scl) {
-//    _i2c = new I2C(sda, scl);
+    _i2c.frequency(400000);
+}
+
+htu21d::htu21d(PinName sda, PinName scl, int i2cFrequency) : _i2c(sda, scl) {
+    _i2c.frequency(i2cFrequency);
 }
 
 //--------------------------------------------------------------------------------------------------------------------------------------//
-//De-contstructor
+//Destructor
 
 htu21d::~htu21d() {
 }
@@ -36,9 +42,10 @@
     if(htu21 == 1) {
         _i2c.write(HTU21DRESET);            //soft reset, must wait 15mS
         _i2c.stop();
+        wait_ms(16);                        //must wait a least 15mS for reset to finish
+        htu21d::getSNReg();                 //go load up the s/n registers
     }
-    wait_ms(16);                            //must wait a least 15mS for reset to finish
-    
+
 #ifdef RTOS_H
     MutexI2cWait.unlock();
 #endif
@@ -166,7 +173,7 @@
             _i2c.stop();
         }
         wait_us(50);
-    } while((htu21cnt < 1000) && (htu21 == 0)); //htu21cnt takes 510 to get temp, 160 for humidity
+    } while((htu21cnt < 10000) && (htu21 == 0)); //htu21cnt takes 510 to get temp, 160 for humidity
  
 #ifdef RTOS_H
     MutexI2cWait.unlock();
@@ -232,3 +239,58 @@
     double h21DtzD = (h21DtzB * h21DtzC) / (h21DtzA - h21DtzC);
     return (h21DtzD);
 }
+
+//--------------------------------------------------------------------------------------------------------------------------------------//
+//Get the HTU21D serial number registers. Returns 64 bit register.
+//should return 0x4854 00xx xxxx 32xx
+
+void htu21d::getSNReg() {
+
+#ifdef RTOS_H
+    MutexI2cWait.lock();
+#endif
+
+    //get 16 bit SNC register, 8 bit SNC-CRC, 16 bit SNA register, 8 bit SNA-CRC
+    int htu21 = 0;
+    _i2c.start();
+    htu21 = _i2c.write(HTU21Di2cWRITE);     //i2c, 1 = ack
+    if(htu21 == 1) {
+        _i2c.write(HTU21SNAC1);
+        _i2c.write(HTU21SNAC2);
+        _i2c.start();
+        htu21 = _i2c.write(HTU21Di2cREAD);
+        HTU21sn.HTU21D_snc = _i2c.read(1) << 8;
+        HTU21sn.HTU21D_snc |= _i2c.read(1);
+        HTU21sn.HTU21D_crcc = _i2c.read(1);
+        HTU21sn.HTU21D_sna = _i2c.read(1) << 8;
+        HTU21sn.HTU21D_sna |= _i2c.read(1);
+        HTU21sn.HTU21D_crca = _i2c.read(0);
+        _i2c.stop();
+    }
+    
+    //get 32 bit SNB register, 32 bit SNB-CRC - regs are intermixed
+    htu21 = 0;
+    _i2c.start();
+    htu21 = _i2c.write(HTU21Di2cWRITE);     //i2c, 1 = ack
+    if(htu21 == 1) {
+        _i2c.write(HTU21SNB1);
+        _i2c.write(HTU21SNB2);
+        _i2c.start();
+        htu21 = _i2c.write(HTU21Di2cREAD);
+        HTU21sn.HTU21D_snb = _i2c.read(1) << 24;
+        HTU21sn.HTU21D_crcb = _i2c.read(1) << 24;
+        HTU21sn.HTU21D_snb |= _i2c.read(1) << 16;
+        HTU21sn.HTU21D_crcb |= _i2c.read(1) << 16;
+        HTU21sn.HTU21D_snb |= _i2c.read(1) << 8;
+        HTU21sn.HTU21D_crcb |= _i2c.read(1) << 8;
+        HTU21sn.HTU21D_snb |= _i2c.read(1);
+        HTU21sn.HTU21D_crcb |= _i2c.read(0);
+        _i2c.stop();
+    }
+    
+#ifdef RTOS_H
+    MutexI2cWait.unlock();
+#endif
+
+}
+