Browse Source

My HTU21 is dead.

Vladimir N. Shilov 1 year ago
parent
commit
a9f18f77f0
2 changed files with 28 additions and 20 deletions
  1. 17 17
      lib/htu21.c
  2. 11 3
      src/main.c

+ 17 - 17
lib/htu21.c

@@ -16,12 +16,12 @@ static uint8_t CalcSht21Crc(uint8_t *data, uint8_t nbrOfBytes);
 
 htu_err_t htu_GetTemperature(int16_t * temperature) {
   buf[0] = HTU_T_MEASURE;
-  //i2cs = i2c_wr_data(addr, 1, buf);
-  I2C_WriteBytes(addr, 1, buf);
+  i2cs = i2c_wr_data(addr, 1, buf);
+  //I2C_WriteBytes(addr, 1, buf);
   if (i2cs == 0) {
-    Delay(90);
-    //i2c_rd_data(addr, 3, buf);
-    I2C_ReadBytes(addr, 3, buf);
+    Delay(100);
+    i2c_rd_data(addr, 3, buf);
+    //I2C_ReadBytes(addr, 3, buf);
     if(buf[2] == CalcSht21Crc(buf,2)) {
       val = buf[0];
       val <<= 8;
@@ -34,40 +34,40 @@ htu_err_t htu_GetTemperature(int16_t * temperature) {
       *temperature = val - 4685;
       return HTU_OK;
     } else {
-      *temperature = 1110;
+      *temperature = 1100;
       return HTU_CRC_Temp;
     }
   } else {
-    *temperature = 1111;
+    *temperature = 9900;
     return HTU_I2C;
   }
 }
 
 htu_err_t htu_GetHumidity(uint16_t * humidity) {
   buf[0] = HTU_H_MEASURE;
-  //i2cs = i2c_wr_data(addr, 1, buf);
-  I2C_WriteBytes(addr, 1, buf);
+  i2cs = i2c_wr_data(addr, 1, buf);
+  //I2C_WriteBytes(addr, 1, buf);
   if (i2cs == 0) {
-    Delay(60);
-    //i2c_rd_data(addr, 3, buf);
-    I2C_ReadBytes(addr, 3, buf);
-    if(1) { //buf[2] == CalcSht21Crc(buf,2)) {
+    Delay(100);
+    i2c_rd_data(addr, 3, buf);
+    //I2C_ReadBytes(addr, 3, buf);
+    if(buf[2] == CalcSht21Crc(buf,2)) {
       val = buf[0];
       val <<= 8;
       val |= (buf[1] & 0xfc);
       // Humidity = val * 125 / 2^16 - 6
       val *= 125;
-      val *= 10; // for .x %H
+      //val *= 10; // for .x %H
       val += 32768; // for round
       val /= 65536;
-      *humidity = val - 60;
+      *humidity = val - 6;
       return HTU_OK;
     } else {
-      *humidity = 1110;
+      *humidity = 1100;
       return HTU_CRC_Humidity;
     }
   } else {
-    *humidity = i2cs * 100;
+    *humidity = 9900;
     return HTU_I2C;
   }
 }

+ 11 - 3
src/main.c

@@ -53,15 +53,23 @@ void main(void)
   /* I2C Configuration */
   //i2c_master_init();
   I2C_Init();
+  uint8_t buf[3];
+  buf[0] = 0xfe;
+  i2c_wr_data(0x40, 1, buf);
+  Delay(20);
+  buf[0] = 0xe6;
+  buf[1] = 0x06;
+  i2c_wr_data(0x40, 1, buf);
+  Delay(20);
 
   /* Infinite loop */
   while (1) {
     htu_GetTemperature(&Temperature);
     showT();
-    Delay(410);
+    Delay(400);
     htu_GetHumidity(&Humidity);
     showH();
-    Delay(440);
+    Delay(400);
   }
 
 }
@@ -81,7 +89,7 @@ static void showT(void) {
 }
 
 static void showH(void) {
- uint16_t h = (Humidity + 5) / 10;
+ uint16_t h = Humidity; //(Humidity + 5) / 10;
  uint8_t a;
 
  LedDigits[4] = h / 100;