htu21d_for_weather_shield

Dependents:   SPARKFUN_WEATHER_SHIELD

Fork of htu21d by Kevin Braun

Revision:
2:8fbe84ed61e6
Parent:
1:d3ed713f8354
Child:
3:5c0f8e91d319
--- a/htu21d.cpp	Thu May 15 19:12:11 2014 +0000
+++ b/htu21d.cpp	Thu May 22 18:48:46 2014 +0000
@@ -10,6 +10,9 @@
 extern Mutex MutexI2cWait;
 #endif
 
+double theTempIs = 0.0;
+double theHumIs = 0.0;
+
 //--------------------------------------------------------------------------------------------------------------------------------------//
 //Contstructor
 
@@ -158,13 +161,20 @@
 #ifdef RTOS_H
     MutexI2cWait.lock();
 #endif
-
     _i2c.start();
     htu21 = _i2c.write(HTU21Di2cWRITE);
     _i2c.write(reg);            //read temp, no hold
+    _i2c.stop();
+#ifdef RTOS_H
+        MutexI2cWait.unlock();
+#endif
+
     if(htu21 == 0) return 0;    //HTU21T not responding
     do {
         htu21cnt++;
+#ifdef RTOS_H
+        MutexI2cWait.lock();
+#endif
         _i2c.start();
         htu21 = _i2c.write(HTU21Di2cREAD);
         if(htu21 == 1) {
@@ -172,19 +182,18 @@
             htu21data |= _i2c.read(0) & 0xFC;
             _i2c.stop();
         }
-        wait_us(50);
-    } while((htu21cnt < 10000) && (htu21 == 0)); //htu21cnt takes 510 to get temp, 160 for humidity
- 
 #ifdef RTOS_H
-    MutexI2cWait.unlock();
+        MutexI2cWait.unlock();                  //free up the I2C bus
+        Thread::wait(1);                        //allow other RTOS functions to sneak in
+#else
+        wait_us(1000);
 #endif
-
+    } while((htu21cnt < 100) && (htu21 == 0));  //htu21cnt takes 55 to get temp, 16 for humidity (at 1mS loops)
+        
     if(htu21 == 0) return 0;    //HTU21D ACK response timed out
     return(htu21data);          //return 14 bit value
 }
 
-double theTempIs = 0.0;
-double theHumIs = 0.0;
 //--------------------------------------------------------------------------------------------------------------------------------------//
 //get temperature from HTU21D in degrees C. Returns with 255.0 if HTU21D had timed out.