Bläddra i källkod

AHT10 dont work.

Vladimir N. Shilov 3 år sedan
förälder
incheckning
729ad5d36f
4 ändrade filer med 93 tillägg och 54 borttagningar
  1. 27 27
      Dryer.layout
  2. 1 1
      inc/sensor.h
  3. 47 17
      src/main.c
  4. 18 9
      src/sensor.c

+ 27 - 27
Dryer.layout

@@ -2,11 +2,6 @@
 <CodeBlocks_layout_file>
 	<FileVersion major="1" minor="0" />
 	<ActiveTarget name="Debug" />
-	<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="2098" topLine="0" />
-		</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">
 		<Cursor>
 			<Cursor1 position="1017" topLine="24" />
@@ -17,59 +12,64 @@
 			<Cursor1 position="4343" topLine="110" />
 		</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="src\i2c.c" open="0" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1354" topLine="42" />
+			<Cursor1 position="1062" topLine="33" />
 		</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="68" />
 		</Cursor>
 	</File>
-	<File name="inc\sensor.h" 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="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="390" topLine="0" />
+			<Cursor1 position="2150" topLine="95" />
 		</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="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="2252" topLine="56" />
+			<Cursor1 position="1177" 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\rtos\rtos.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2518" topLine="50" />
+			<Cursor1 position="2098" 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="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="88" topLine="0" />
+			<Cursor1 position="2700" topLine="6" />
 		</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\sensor.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="951" topLine="0" />
+			<Cursor1 position="350" topLine="0" />
 		</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="inc\main.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1062" topLine="33" />
+			<Cursor1 position="2252" topLine="76" />
 		</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="2700" topLine="43" />
+			<Cursor1 position="5259" topLine="131" />
 		</Cursor>
 	</File>
-	<File name="src\sensor.c" open="1" top="0" tabpos="4" 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="68" />
+			<Cursor1 position="88" topLine="0" />
 		</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="src\main.c" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="5259" topLine="131" />
+			<Cursor1 position="3104" topLine="78" />
+		</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">
+		<Cursor>
+			<Cursor1 position="951" topLine="0" />
 		</Cursor>
 	</File>
 </CodeBlocks_layout_file>

+ 1 - 1
inc/sensor.h

@@ -19,7 +19,7 @@ typedef enum {
 
 /* Data type */
 typedef struct {
-  int8_t Humidity;
+  uint8_t Humidity;
   int8_t Temperature;
 } aht10_t;
 

+ 47 - 17
src/main.c

@@ -9,6 +9,7 @@
 #include "ssd1306xled/font8x16.h"
 #include "ssd1306xled/ssd1306xled.h"
 #include "ssd1306xled/ssd1306xledtx.h"
+#include "tinyavrlib/num2str.h"
 
 /* Defines */
 
@@ -39,43 +40,73 @@ int main(void) {
   ssd1306_init();
 
   /* Clear flags */
-  Flag.newTempSet = 0;
+  Flag.newTempSet = 1;
   Flag.AHT10state = 0;
 
   /* Set tasks */
-  RTOS_SetTask(checkTemperatureSetpoint, 10, 100);
+  RTOS_SetTask(checkTemperatureSetpoint, 5, 50);
   RTOS_SetTask(sensorStart, 50, 1000);
-  RTOS_SetTask(sensorGetData, 1050, 1000);
+  RTOS_SetTask(sensorGetData, 800, 1000);
   RTOS_SetTask(LedOn, 1, 1000);
+  RTOS_SetTask(LedOff, 201, 1000);
 
 //  ssd1306_fill2(0xff, 0x00);
   ssd1306_clear();
-  ssd1306tx_init(ssd1306xled_font6x8data, 0);
+//  ssd1306tx_init(ssd1306xled_font6x8data, 0);
   // ssd1306xled_font8x16data
   ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 0, 0, "Hello, World! :)");
-  ssd1306_setpos(0, 3);
-  ssd1306tx_string("SSD1306xLED Library");
+//  ssd1306_setpos(0, 3);
+//  ssd1306tx_string("SSD1306xLED Library");
 //  ssd1306_setpos(0, 3);
 //  ssd1306tx_string("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[5]^_`");
-  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 0, 5, "TINUSAUR Project");
-  ssd1306_setpos(0, 7);
-  ssd1306tx_string("http://tinusaur.org");
-  tdelay_ms(2000);
+//  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 0, 5, "TINUSAUR Project");
+//  ssd1306_setpos(0, 7);
+//  ssd1306tx_string("http://tinusaur.org");
+  tdelay_ms(1000);
+
   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:");
+  char buffer[6] = {'\0'};
+  uint8_t digits = 0;
 
   /* Infinity loop */
   do {
+
   if (Flag.newTempSet != 0) {
     Flag.newTempSet = 0;
-    ssd1306_setpos(1, 5);
+    /*
+    ssd1306_setpos(25, 0);
     ssd1306tx_numdec(TemperatureSetpoint);
+    ssd1306tx_string(" C");
+    */
+    if (TemperatureSetpoint != 0) {
+      digits = usint2decascii(TemperatureSetpoint, buffer);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 0, buffer+digits);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 56, 0, " C ");
+    } else {
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 0, " Off");
+    }
   }
 
-    ssd1306_setpos(1, 3);
+    //ssd1306_setpos(13, 3);
     if (Flag.AHT10state == 0) {
+        /*
       ssd1306tx_numdec(Sensor.Temperature);
+      ssd1306tx_string(" C");
+      ssd1306_setpos(13, 5);
+      ssd1306tx_numdec(Sensor.Humidity);
+      ssd1306tx_string(" P");
+      */
+      digits = usint2decascii((int8_t)Sensor.Temperature, buffer);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, buffer+digits);
+      digits = usint2decascii(Sensor.Humidity, buffer);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, buffer+digits);
     } else {
-      ssd1306tx_string("AHT10 error");
+      //ssd1306tx_string("AHT10 error");
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, "Error");
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, "     ");
     }
 
     RTOS_DispatchTask();
@@ -106,8 +137,8 @@ static void board_Init(void) {
   DDRB = 0x22; // PB1 - Triac control, PB5 - Green LED
   DDRC = 0x30; // I2C output
   PORTB = 0x03; // close Triac, enable pull-up for D8
-  PORTC = 0x3e; // I2C = 1, enable pull-up for switch
-  PORTD = 0xfc; // enable pull-up for D0-D7 (Switch)
+  PORTC = 0x3e; // I2C = 1
+  PORTD = 0xff; // enable pull-up for D0-D7 (Switch)
 
   /* Timer0 - RTOS & tdelay_ms() */
   TCCR0B = ((0<<CS02)|(1<<CS01)|(1<<CS00));
@@ -160,7 +191,7 @@ static void checkTemperatureSetpoint(void) {
       TemperatureSetpoint = 0;
     }
   } else {
-    pvalue = ~(PINB) | (1<<PB0);
+    pvalue = (~(PINB)) & (1<<PB0);
     if (pvalue != 0) {
       TemperatureSetpoint = 90;
     } else {
@@ -200,7 +231,6 @@ static void sensorGetData(void) {
 
 static void LedOn(void) {
   PORTB |= (1<<PB5);
-  RTOS_SetTask(LedOff, 200, 0);
 }
 
 static void LedOff(void) {

+ 18 - 9
src/sensor.c

@@ -17,6 +17,15 @@ typedef union {
     uint8_t u8ll;
     #endif
   } s8;
+  struct {
+    #ifdef LITTLE_ENDIAN  // Byte-order is little endian
+    uint8_t u16l;
+    uint8_t u16h;
+    #else                 // Byte-order is big endian
+    uint8_t u16h;
+    uint8_t u16l;
+    #endif
+  } s16;
 } nt32_t;
 
 /* Defines */
@@ -98,25 +107,25 @@ aht10_st_t AHT10_GetData(aht10_t * data)
     return AHT10_St_Bsy;
   }
   /* Calculate values */
-  uint32_t result;
-  nt32_t tmp;
+  nt32_t tmp, result;
 
   /* Humidity = Srh * 100% / 2^20 */
   tmp.s8.u8hl = buf[1];
   tmp.s8.u8lh = buf[2];
   tmp.s8.u8ll = buf[3];
-  result = tmp.u32 >> 4;
-  result *= 100;
-  result /= 1048576;
-  data->Humidity = (int8_t)result;
+  result.u32 = tmp.u32 >> 4;
+  result.u32 *= 100;
+  result.s16.u16h >>= 4;
+  data->Humidity = result.s16.u16h;
 
   /* Temperature = St * 200 / 2^20 - 50 */
   tmp.s8.u8hl = buf[3] & 0xf;
   tmp.s8.u8lh = buf[4];
   tmp.s8.u8ll = buf[5];
-  result = tmp.u32 * 200;
-  result /= 1048576;
-  data->Temperature = (int8_t)(result - 50);
+  tmp.u32 *= 200;
+  tmp.s16.u16h >>= 4;
+  //tmp.s16.u16h -= 50;
+  data->Temperature = tmp.s16.u16h;
 
   return AHT10_St_OK;
 }