浏览代码

AHT20 work. Fix i2c read.

Vladimir N. Shilov 3 年之前
父节点
当前提交
96d4578cbc
共有 3 个文件被更改,包括 43 次插入48 次删除
  1. 25 25
      Dryer.layout
  2. 2 2
      inc/sensor.h
  3. 16 21
      src/sensor.c

+ 25 - 25
Dryer.layout

@@ -2,54 +2,54 @@
 <CodeBlocks_layout_file>
 	<FileVersion major="1" minor="0" />
 	<ActiveTarget name="Debug" />
-	<File name="src\sensor.c" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\ssd1306xled\ssd1306xledtx.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="3121" topLine="150" />
+			<Cursor1 position="951" topLine="0" />
 		</Cursor>
 	</File>
-	<File name="lib\ssd1306xled\ssd1306xledtx.c" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\tinyavrlib\num2str.c" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2451" topLine="66" />
+			<Cursor1 position="3534" topLine="43" />
 		</Cursor>
 	</File>
 	<File name="inc\sensor.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="274" topLine="0" />
+			<Cursor1 position="388" topLine="0" />
 		</Cursor>
 	</File>
-	<File name="inc\main.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="src\sensor.c" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2252" topLine="56" />
+			<Cursor1 position="3362" topLine="165" />
 		</Cursor>
 	</File>
-	<File name="lib\ssd1306xled\font8x16.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="src\i2c.c" open="0" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1017" topLine="24" />
+			<Cursor1 position="2571" topLine="71" />
 		</Cursor>
 	</File>
-	<File name="inc\i2c.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\ssd1306xled\font8x16.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="88" topLine="0" />
+			<Cursor1 position="1017" topLine="24" />
 		</Cursor>
 	</File>
-	<File name="src\i2c.c" open="0" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\ssd1306xled\ssd1306xled.h" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2571" topLine="71" />
+			<Cursor1 position="1177" topLine="0" />
 		</Cursor>
 	</File>
-	<File name="lib\ssd1306xled\ssd1306xled.h" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\ssd1306xled\ssd1306xledtx.c" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1177" topLine="0" />
+			<Cursor1 position="2451" topLine="66" />
 		</Cursor>
 	</File>
-	<File name="src\main.c" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="inc\main.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1393" topLine="41" />
+			<Cursor1 position="2252" topLine="56" />
 		</Cursor>
 	</File>
-	<File name="lib\ssd1306xled\ssd1306xled.c" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\rtos\rtos.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="5259" topLine="23" />
+			<Cursor1 position="2615" topLine="0" />
 		</Cursor>
 	</File>
 	<File name="lib\rtos\rtos.c" open="0" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
@@ -57,19 +57,19 @@
 			<Cursor1 position="4343" topLine="110" />
 		</Cursor>
 	</File>
-	<File name="lib\tinyavrlib\num2str.c" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="src\main.c" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="3534" topLine="43" />
+			<Cursor1 position="1393" topLine="41" />
 		</Cursor>
 	</File>
-	<File name="lib\rtos\rtos.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="lib\ssd1306xled\ssd1306xled.c" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2615" topLine="0" />
+			<Cursor1 position="5259" topLine="23" />
 		</Cursor>
 	</File>
-	<File name="lib\ssd1306xled\ssd1306xledtx.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
+	<File name="inc\i2c.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="951" topLine="0" />
+			<Cursor1 position="88" topLine="0" />
 		</Cursor>
 	</File>
 </CodeBlocks_layout_file>

+ 2 - 2
inc/sensor.h

@@ -20,8 +20,8 @@ typedef enum {
 
 /* Data type */
 typedef struct {
-  uint8_t Humidity;
-   int8_t Temperature;
+  uint16_t Humidity;
+   int16_t Temperature;
 } aht20_t;
 
 /* Function */

+ 16 - 21
src/sensor.c

@@ -52,7 +52,7 @@ aht20_st_t AHT20_Init(void)
   }
 
   /* get one byte with NACK */
-  res = I2C_ReadByte(&data, 1);
+  res = I2C_ReadByte(&data, 0);
   if (res != I2C_Ret_OK) {
     return AHT_St_Err;
   }
@@ -143,23 +143,15 @@ aht20_st_t AHT20_GetData(aht20_t * data)
     return AHT_St_Err;
   }
 
-  /* Now read the data */
-  uint8_t buf[8];
-  uint8_t rd, i;
-  /* read status and data */
-  for (i=0; i<6; i++) {
-    res = I2C_ReadByte(&rd, 0);
+  /* Now read 7 bytes of data */
+  uint8_t buf[8], i;
+
+  for (i=0; i<7; i++) {
+    res = I2C_ReadByte(&buf[i], 6-i);
     if (res != I2C_Ret_OK) {
       return AHT_St_Err;
     }
-    buf[i] = rd;
-  }
-  /* read crc */
-  res = I2C_ReadByte(&rd, 1);
-  if (res != I2C_Ret_OK) {
-    return AHT_St_Err;
   }
-  buf[6] = rd;
 
   I2C_Stop();
 
@@ -177,10 +169,12 @@ aht20_st_t AHT20_GetData(aht20_t * data)
   result <<= 8;
   result |= buf[3];
   result >>= 4;
-  result *= 100;
+  result *= 1000;
   result += 524288;
-  result /= 10248576;
-  data->Humidity = (uint8_t)result;
+  result /= 256;
+  result /= 256;
+  result /= 16;
+  data->Humidity = (uint16_t)result;
 
   /* Temperature = St * 200 / 2^20 - 50 */
   result = buf[3] & 0xf;
@@ -188,11 +182,12 @@ aht20_st_t AHT20_GetData(aht20_t * data)
   result |= buf[4];
   result <<= 8;
   result |= buf[5];
-  result *= 200;
+  result *= 2000;
   result += 524288;
-  result /= 1048576;
-  result -= 50;
-  data->Temperature = (int8_t)result;
+  result /= 256;
+  result /= 256;
+  result /= 16;
+  data->Temperature = (int16_t)(result - 500);
 
   return AHT_St_OK;
 }