瀏覽代碼

AHT10 troubles :-(

Vladimir N. Shilov 3 年之前
父節點
當前提交
59e5c8f61c
共有 5 個文件被更改,包括 70 次插入54 次删除
  1. 26 26
      Dryer.layout
  2. 3 2
      inc/sensor.h
  3. 1 1
      lib/tinyavrlib/num2str.c
  4. 13 10
      src/main.c
  5. 27 15
      src/sensor.c

+ 26 - 26
Dryer.layout

@@ -2,69 +2,69 @@
 <CodeBlocks_layout_file>
 	<FileVersion major="1" minor="0" />
 	<ActiveTarget name="Debug" />
-	<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="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="951" topLine="0" />
+			<Cursor1 position="3533" topLine="60" />
 		</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\rtos\rtos.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2451" topLine="66" />
+			<Cursor1 position="2661" topLine="15" />
 		</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="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="3534" topLine="60" />
+			<Cursor1 position="5259" topLine="131" />
 		</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\font8x16.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1062" topLine="33" />
+			<Cursor1 position="1017" topLine="24" />
 		</Cursor>
 	</File>
-	<File name="src\main.c" open="1" top="1" tabpos="1" 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="1393" topLine="41" />
+			<Cursor1 position="951" topLine="0" />
 		</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="src\main.c" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="88" topLine="0" />
+			<Cursor1 position="2244" topLine="67" />
 		</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="inc\sensor.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2098" topLine="0" />
+			<Cursor1 position="350" 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="inc\sensor.h" open="1" top="0" tabpos="4" 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="371" topLine="0" />
+			<Cursor1 position="2252" topLine="56" />
 		</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="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1017" topLine="24" />
+			<Cursor1 position="1892" topLine="77" />
 		</Cursor>
 	</File>
-	<File name="src\sensor.c" open="1" top="0" tabpos="3" 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="2212" topLine="96" />
+			<Cursor1 position="2469" topLine="107" />
 		</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\ssd1306xled\ssd1306xled.h" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="5259" topLine="131" />
+			<Cursor1 position="1177" 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="inc\i2c.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2252" topLine="56" />
+			<Cursor1 position="88" 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">

+ 3 - 2
inc/sensor.h

@@ -19,8 +19,9 @@ typedef enum {
 
 /* Data type */
 typedef struct {
-  uint8_t Humidity;
-   int8_t Temperature;
+   uint8_t Status;
+  uint16_t Humidity;
+   int16_t Temperature;
 } aht10_t;
 
 /* Function */

+ 1 - 1
lib/tinyavrlib/num2str.c

@@ -83,7 +83,7 @@ uint8_t usint2binascii(uint16_t num, char *buffer) {
 		if (digits == USINT2BINASCII_MAX_DIGITS - 1) {
 			if (digit == 0) {
 				if (pos < USINT2BINASCII_MAX_DIGITS - 1) // Check position, so single "0" will be handled properly.
-					digit = -3; // Use: "-16" for space (' '), "-3" for dash/minus ('-'), "0" for zero ('0'), etc.
+					digit = 0; // Use: "-16" for space (' '), "-3" for dash/minus ('-'), "0" for zero ('0'), etc.
 			} else {
 				digits = pos;
 			}

+ 13 - 10
src/main.c

@@ -46,10 +46,10 @@ int main(void) {
 
   /* Set tasks */
   RTOS_SetTask(checkTemperatureSetpoint, 5, 50);
-  RTOS_SetTask(sensorStart, 50, 1000);
-  RTOS_SetTask(sensorGetData, 800, 1000);
-  RTOS_SetTask(LedOn, 1, 1000);
-  RTOS_SetTask(LedOff, 201, 1000);
+  RTOS_SetTask(sensorStart, 1, 2000);
+  RTOS_SetTask(sensorGetData, 501, 2000);
+  RTOS_SetTask(LedOn, 2, 1000);
+  RTOS_SetTask(LedOff, 202, 1000);
 
   ssd1306_clear();
 //  ssd1306tx_init(ssd1306xled_font6x8data, 0);
@@ -60,8 +60,8 @@ int main(void) {
 
   ssd1306_clear();
   ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 0, "Set:");
-  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 3, "  T:");
-  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 5, "  H:");
+  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 2, "  T:");
+  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 4, "  H:");
   char buffer[9] = {0};
   uint8_t digits = 0;
 
@@ -82,15 +82,18 @@ int main(void) {
     if (Flag.AHT10state == 0) {
       digits = usint2decascii(Sensor.Temperature, buffer);
       strncpy(buffer+5, " C\0", 3);
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, buffer+digits);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 2, buffer+digits);
 
       digits = usint2decascii(Sensor.Humidity, buffer);
       strncpy(buffer+5, " %\0", 3);
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, buffer+digits);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 4, buffer+digits);
+
+      digits = usint2binascii(Sensor.Status, buffer);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 16, 6, buffer+8);
     } else {
       //ssd1306tx_string("AHT10 error");
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, "Error");
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, "     ");
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 2, "Error");
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 4, "     ");
     }
 
     RTOS_DispatchTask();

+ 27 - 15
src/sensor.c

@@ -1,6 +1,8 @@
 #include "sensor.h"
 #include "i2c.h"
 
+extern void tdelay_ms(uint16_t msek);
+
 /* Defines */
 #define I2C_ADDR    0x70
 
@@ -24,7 +26,7 @@ aht10_st_t AHT10_Init(void)
     return AHT10_St_Err;
   }
 
-  res = I2C_WriteByte(CMD_SRESET);
+  res = I2C_WriteByte(CMD_INIT);
   if (res != I2C_Ret_OK) {
     return AHT10_St_Err;
   }
@@ -90,19 +92,26 @@ aht10_st_t AHT10_GetData(aht10_t * data)
 
   /* Now read the value with NACK */
   uint8_t buf[6];
-  res = I2C_ReadByte(buf, 0);
+  I2C_ReadByte(&buf[0], 1);
+  I2C_ReadByte(&buf[1], 1);
+  I2C_ReadByte(&buf[2], 1);
+  I2C_ReadByte(&buf[3], 1);
+  I2C_ReadByte(&buf[4], 1);
+  res = I2C_ReadByte(&buf[5], 0);
   if (res != I2C_Ret_OK) {
     return AHT10_St_Err;
   }
 
   I2C_Stop();
 
+  data->Status = buf[0];
+
   if ((buf[0] & STATUS_BUSY) != 0) {
     return AHT10_St_Bsy;
   }
+
   /* Calculate values */
   uint32_t result;
-  //nt32_t result = {0};
 
   /* Humidity = Srh * 100% / 2^20 */
   result = buf[1];
@@ -111,20 +120,23 @@ aht10_st_t AHT10_GetData(aht10_t * data)
   result <<= 8;
   result |= buf[3];
   result >>= 4;
-  result *= 100;
-  result /= 1048576;
-  data->Humidity = (uint8_t)result;
+  result *= 1000;
+  result /= 256;
+  result /= 256;
+  data->Humidity = (uint16_t)(result / 16);
 
+  int32_t result2;
   /* Temperature = St * 200 / 2^20 - 50 */
-  result = buf[3] & 0xf;
-  result <<= 8;
-  result |= buf[4];
-  result <<= 8;
-  result |= buf[5];
-  result *= 200;
-  result /= 1048576;
-  result -= 50;
-  data->Temperature = (int8_t)result;
+  result2 = buf[3] & 0xf;
+  result2 <<= 8;
+  result2 |= buf[4];
+  result2 <<= 8;
+  result2 |= buf[5];
+  result2 *= 2000;
+  result2 /= 256;
+  result2 /= 256;
+  result2 /= 16;
+  data->Temperature = (int16_t)(result2 - 500);
 
   return AHT10_St_OK;
 }