Bladeren bron

Added work Clock. Improve output.

Vladimir N. Shilov 3 jaren geleden
bovenliggende
commit
fc590d6c1a
3 gewijzigde bestanden met toevoegingen van 134 en 65 verwijderingen
  1. 30 30
      Dryer.layout
  2. 10 0
      inc/main.h
  3. 94 35
      src/main.c

+ 30 - 30
Dryer.layout

@@ -2,79 +2,79 @@
 <CodeBlocks_layout_file>
 	<FileVersion major="1" minor="0" />
 	<ActiveTarget name="Debug" />
-	<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="src\main.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2451" topLine="66" />
+			<Cursor1 position="3263" topLine="99" />
 		</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="inc\i2c.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1017" topLine="24" />
+			<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">
+	<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="4343" topLine="110" />
+			<Cursor1 position="5259" topLine="23" />
 		</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="src\sensor.c" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="257" topLine="0" />
+			<Cursor1 position="3362" topLine="54" />
 		</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\tinyavrlib\num2str.c" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="1177" topLine="0" />
+			<Cursor1 position="2031" topLine="30" />
 		</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\i2c.c" open="0" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="88" topLine="0" />
+			<Cursor1 position="2571" topLine="71" />
 		</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="inc\sensor.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="3098" topLine="72" />
+			<Cursor1 position="415" topLine="0" />
 		</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\bresenham.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2615" topLine="0" />
+			<Cursor1 position="786" topLine="13" />
 		</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="23" />
+			<Cursor1 position="1177" topLine="0" />
 		</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="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="3362" topLine="54" />
+			<Cursor1 position="2615" 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="lib\rtos\rtos.c" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2571" topLine="71" />
+			<Cursor1 position="4415" topLine="129" />
 		</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\ssd1306xledtx.c" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="2252" topLine="0" />
+			<Cursor1 position="2451" topLine="66" />
 		</Cursor>
 	</File>
-	<File name="inc\bresenham.h" open="1" top="0" tabpos="11" 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="786" topLine="13" />
+			<Cursor1 position="951" topLine="0" />
 		</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\font8x16.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="3534" topLine="43" />
+			<Cursor1 position="2391" topLine="24" />
 		</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\main.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
 		<Cursor>
-			<Cursor1 position="951" topLine="0" />
+			<Cursor1 position="1518" topLine="23" />
 		</Cursor>
 	</File>
 </CodeBlocks_layout_file>

+ 10 - 0
inc/main.h

@@ -21,6 +21,10 @@
 #define TIMER0_PRESCALER  64
 #define	TIMER0_CNT        (0x100 - (F_CPU / TIMER0_PRESCALER / TIMER0_HZ))
 
+#define TIMER1_HZ         1
+#define TIMER1_PRESCALER  1024
+#define	TIMER1_CNT        (uint16_t)(0x10000 - (F_CPU / TIMER1_PRESCALER / TIMER1_HZ))
+
 /**
  * Bitwise macro
  */
@@ -52,6 +56,12 @@ typedef   signed short     s16;
 typedef   signed long      s32;
 typedef   signed long long s64;
 
+typedef struct {
+  uint8_t HH;
+  uint8_t MM;
+  uint8_t SS;
+} clock_t;
+
 /**
  * Interrupt macro
  */

+ 94 - 35
src/main.c

@@ -22,11 +22,13 @@
 static volatile struct {
   uint8_t newTempSet: 1;
   uint8_t AHT10state: 1;
-  uint8_t rezerv:     6;
+  uint8_t ClockRun:   1;
+  uint8_t rezerv:     5;
 } Flag;
-static uint8_t TemperatureSetpoint = 0;
+static uint8_t TemperatureSetpoint = 0, oldTempSet = 0;
 static aht20_t Sensor;
 static bresenham_t Heater;
+static clock_t Clock = {0};
 
 /* Function prototypes */
 static void board_Init(void);
@@ -38,6 +40,11 @@ static void LedOff(void);
 static void HeaterCtrl(void);
 
 int main(void) {
+  /* Clear flags */
+  Flag.newTempSet = 1;
+  Flag.AHT10state = 0;
+  Flag.ClockRun = 0;
+
   /* Init all */
   board_Init();
   RTOS_Init();
@@ -46,10 +53,6 @@ int main(void) {
   tdelay_ms(40);
   ssd1306_init();
 
-  /* Clear flags */
-  Flag.newTempSet = 1;
-  Flag.AHT10state = 0;
-
   /* Bresenham init.
    * step - 10 ms, period - 1 sek, heater - off */
   bresenham_Init(&Heater, HEATER_POWER_MAX);
@@ -73,41 +76,69 @@ int main(void) {
   tdelay_ms(1000);
 
   ssd1306_clear();
-  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 1, 0, "Pwr:"); // 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, 8, 0, "Pwr:"); // Set
+  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 8, 2, "  T: xx.x C");
+  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 8, 4, "  H: yy.y %");
+  ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 6, ":  :");
   char buffer[9] = {0};
-  uint8_t digits = 0;
+  uint8_t i = 0, d = 0;
 
   /* Infinity loop */
   do {
-
-  if (Flag.newTempSet != 0) {
-    Flag.newTempSet = 0;
-    if (TemperatureSetpoint != 0) {
-      digits = usint2decascii(TemperatureSetpoint, buffer);
-      strncpy(buffer+5, " % \0", 4); // C
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 0, buffer+digits);
-    } else {
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 0, " Off");
+    /* Action for new temperature setting */
+    if (Flag.newTempSet != 0) {
+      Flag.newTempSet = 0;
+      if (TemperatureSetpoint != 0) {
+        if (oldTempSet == 0) {
+        Flag.ClockRun = 1;
+          Clock.SS = 0;
+          Clock.MM = 0;
+          Clock.HH = 0;
+        }
+        usint2decascii(TemperatureSetpoint, buffer);
+        strncpy(buffer+5, " % \0", 4); // C
+        ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 0, buffer+3);
+      } else {
+        Flag.ClockRun = 0;
+        ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 0, " Off");
+      }
+      // set new Heater Power
+      bresenham_setValue(&Heater, TemperatureSetpoint);
     }
-    // set new Heater Power
-    bresenham_setValue(&Heater, TemperatureSetpoint);
-  }
 
+    /* Output Sensor Data */
     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);
-
-      digits = usint2decascii(Sensor.Humidity, buffer);
-      strncpy(buffer+5, " %\0", 3);
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, buffer+digits);
+      strncpy(buffer, "\0", 6);
+      i = Sensor.Temperature / 10;
+      d = Sensor.Temperature % 10;
+      usint2decascii(i, buffer);
+      //strncpy(buffer+5, " C\0", 3);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 2, buffer+3);
+      usint2decascii(d, buffer);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 72, 2, buffer+4);
+
+      strncpy(buffer, "\0", 6);
+      i = Sensor.Humidity / 10;
+      d = Sensor.Humidity % 10;
+      usint2decascii(i, buffer);
+      //strncpy(buffer+5, " %\0", 3);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 4, buffer+3);
+      usint2decascii(d, buffer);
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 72, 4, buffer+4);
     } else {
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 3, " I2C ");
-      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 40, 5, "Error");
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 2, "Er");
+      ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 48, 4, "Er");
     }
 
+    /* Output Clock */
+    strncpy(buffer, "\0", 6);
+    usint2decascii(Clock.HH, buffer);
+    ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 32, 6, buffer+3);
+    usint2decascii(Clock.MM, buffer);
+    ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 56, 6, buffer+3);
+    usint2decascii(Clock.SS, buffer);
+    ssd1306tx_stringxy((uint8_t const *)ssd1306xled_font8x16data, 80, 6, buffer+3);
+
     RTOS_DispatchTask();
 
     // nothing to do - sleep, wait for interrupt
@@ -144,7 +175,10 @@ static void board_Init(void) {
   TCNT0 = TIMER0_CNT;
   TIMSK0 |= (1<<TOIE0);
 
-  /* Timer1 */
+  /* Timer1 - Clock */
+  TCCR1B = (1<<CS12 | 0<<CS11 | 1<<CS10);
+  TCNT1 = TIMER1_CNT;
+  TIMSK1 |= (1<<TOIE1);
 
   /* Timer2 - refresh screen values */
   //TCCR2 = 0x00;
@@ -159,7 +193,7 @@ static void board_Init(void) {
  * Setup Temperature Set Point by valcoder value
  */
 static void checkTemperatureSetpoint(void) {
-  static uint8_t oldTempSet = 0;
+  static uint8_t prev = 0;
   uint8_t pvalue;
 
   pvalue = ~(PIND);
@@ -201,9 +235,10 @@ static void checkTemperatureSetpoint(void) {
     }
   }
 
-  if (oldTempSet != TemperatureSetpoint) {
+  if (prev != TemperatureSetpoint) {
     Flag.newTempSet = 1;
-    oldTempSet = TemperatureSetpoint;
+    oldTempSet = prev;
+    prev = TemperatureSetpoint;
   }
 }
 
@@ -253,6 +288,30 @@ static void HeaterCtrl(void) {
 /**
  * I n t e r r u p t s
  */
+#if defined(__ICCAVR__)
+#pragma vector=TIMER1_OVF_vect
+__interrupt void TIMER1_OVF_ISR(void)
+#elif defined(__GNUC__)
+ISR(TIMER1_OVF_vect)
+#endif
+{
+  /* reload timer counter */
+  TCNT1 = TIMER1_CNT;
+
+  /* Clock */
+  if (Flag.ClockRun != 0) {
+    Clock.SS ++;
+    if (Clock.SS > 59) {
+      Clock.SS = 0;
+      Clock.MM ++;
+      if (Clock.MM > 59) {
+        Clock.MM = 0;
+        Clock.HH ++;
+      }
+    }
+  }
+}
+
 #if defined(__GNUC__)
 /**
  * @brief заглушка для неиспользуемых прерываний