Преглед изворни кода

Доработал вывод. Сделал установку времени и простое управление. RC1.

Vladimir N. Shilov пре 7 година
родитељ
комит
95682c80c5
3 измењених фајлова са 253 додато и 118 уклоњено
  1. 42 37
      MegaClock.ewp
  2. 196 79
      main.c
  3. 15 2
      main.h

+ 42 - 37
MegaClock.ewp

@@ -1069,12 +1069,12 @@
         <debug>0</debug>
         <option>
           <name>GGEnhancedCore</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>Variant Memory</name>
           <version>0</version>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>ExePath</name>
@@ -1107,7 +1107,7 @@
         </option>
         <option>
           <name>LCEnableBitDefs</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>LCHeapSize</name>
@@ -1171,51 +1171,55 @@
         </option>
         <option>
           <name>GGEepromUtil</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>GGEepromUtilSize</name>
-          <state>0</state>
+          <state>512</state>
         </option>
         <option>
           <name>GRuntimeLibSelect</name>
           <version>0</version>
-          <state>4</state>
+          <state>1</state>
         </option>
         <option>
           <name>RTDescription</name>
-          <state>Use the legacy C runtime library.</state>
+          <state>Use the normal configuration of the C/C++</state>
+          <state>runtime library. No locale interface,</state>
+          <state>C locale, no file descriptor support,</state>
+          <state>no multibytes in printf and scanf, and</state>
+          <state>no hex floats in strtod.</state>
         </option>
         <option>
           <name>RTConfigPath</name>
-          <state></state>
+          <state>$TOOLKIT_DIR$\LIB\DLIB\dlAVR-1s-ec_mul-n.h</state>
         </option>
         <option>
           <name>RTLibraryPath</name>
-          <state>$TOOLKIT_DIR$\LIB\CLIB\cl0t.r90</state>
+          <state>$TOOLKIT_DIR$\LIB\DLIB\dlAVR-1s-ec_mul-n.r90</state>
         </option>
         <option>
           <name>Input variant</name>
           <version>36</version>
-          <state>3</state>
+          <state>0</state>
         </option>
         <option>
           <name>Input description</name>
-          <state>Full formatting.</state>
+          <state>Automatic choice of formatter.</state>
         </option>
         <option>
           <name>Output variant</name>
           <version>36</version>
-          <state>3</state>
+          <state>0</state>
         </option>
         <option>
           <name>Output description</name>
-          <state>Full formatting.</state>
+          <state>Automatic choice of formatter.</state>
         </option>
         <option>
           <name>GRuntimeLibSelectSlave</name>
           <version>0</version>
-          <state>4</state>
+          <state>1</state>
         </option>
         <option>
           <name>GeneralEnableMisra</name>
@@ -1265,7 +1269,7 @@
         </option>
         <option>
           <name>GenDeviceSelectMenu</name>
-          <state>0	-v0, Max 256 byte data, 8 Kbyte code</state>
+          <state>m8a	ATmega8A</state>
         </option>
         <option>
           <name>OGProductVersion</name>
@@ -1273,7 +1277,7 @@
         </option>
         <option>
           <name>OGLastSavedByProductVersion</name>
-          <state></state>
+          <state>6.80.7.1083</state>
         </option>
       </data>
     </settings>
@@ -1302,7 +1306,7 @@
         </option>
         <option>
           <name>CCDefines</name>
-          <state>NDEBUG</state>
+          <state>F_CPU=8000000UL</state>
         </option>
         <option>
           <name>CCPreprocFile</name>
@@ -1318,7 +1322,7 @@
         </option>
         <option>
           <name>CCListCFile</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>CCListCMnemonics</name>
@@ -1334,7 +1338,7 @@
         </option>
         <option>
           <name>CCListAssSource</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>CCEnableRemarks</name>
@@ -1389,7 +1393,7 @@
         <option>
           <name>CCCrossCallPassesList</name>
           <version>8</version>
-          <state>1</state>
+          <state>5</state>
         </option>
         <option>
           <name>CCObjUseModuleName</name>
@@ -1453,7 +1457,7 @@
         </option>
         <option>
           <name>CCCompilerRuntimeInfo</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>newCCIncludePaths</name>
@@ -1544,7 +1548,7 @@
         <option>
           <name>CCOptStrategy</name>
           <version>0</version>
-          <state>1</state>
+          <state>0</state>
         </option>
         <option>
           <name>CCOptLevel</name>
@@ -1597,7 +1601,7 @@
         </option>
         <option>
           <name>CDebug</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>ADefines</name>
@@ -1718,7 +1722,7 @@
         </option>
         <option>
           <name>ANewIncludes</name>
-          <state>###Uninitialized###</state>
+          <state>$TOOLKIT_DIR$\INC\</state>
         </option>
         <option>
           <name>AsmMultiByteSupport</name>
@@ -1734,7 +1738,7 @@
         </option>
         <option>
           <name>AsmOutputFile</name>
-          <state></state>
+          <state>$FILE_BNAME$.r90</state>
         </option>
         <option>
           <name>AStdIncCheck</name>
@@ -1777,12 +1781,12 @@
         </option>
         <option>
           <name>OutputFile</name>
-          <state>templproj.a90</state>
+          <state>MegaClock.d90</state>
         </option>
         <option>
           <name>OutputFormat</name>
           <version>11</version>
-          <state>25</state>
+          <state>22</state>
         </option>
         <option>
           <name>FormatVariant</name>
@@ -1819,7 +1823,7 @@
         </option>
         <option>
           <name>ListSymbols</name>
-          <state>2</state>
+          <state>1</state>
         </option>
         <option>
           <name>PageLengthCheck</name>
@@ -1843,7 +1847,7 @@
         </option>
         <option>
           <name>XclFile</name>
-          <state>$TOOLKIT_DIR$\src\template\lnk0t.xcl</state>
+          <state>$TOOLKIT_DIR$\src\template\cfgm8a.xcl</state>
         </option>
         <option>
           <name>XclFileSlave</name>
@@ -1915,11 +1919,12 @@
         </option>
         <option>
           <name>XExtraOptionsCheck</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>XExtraOptions</name>
-          <state></state>
+          <state>-Ointel-standard,(CODE)=.hex</state>
+          <state>-Ointel-standard,(XDATA)=.eep</state>
         </option>
         <option>
           <name>IncludeSuppressed</name>
@@ -1935,7 +1940,7 @@
         </option>
         <option>
           <name>DebugInformation</name>
-          <state>1</state>
+          <state>0</state>
         </option>
         <option>
           <name>RuntimeControl</name>
@@ -1947,7 +1952,7 @@
         </option>
         <option>
           <name>AllowExtraOutput</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>GenerateExtraOutput</name>
@@ -1955,16 +1960,16 @@
         </option>
         <option>
           <name>XExtraOutOverride</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>ExtraOutputFile</name>
-          <state>templproj.a90</state>
+          <state>MegaClock.a90</state>
         </option>
         <option>
           <name>ExtraOutputFormat</name>
           <version>11</version>
-          <state>25</state>
+          <state>22</state>
         </option>
         <option>
           <name>ExtraFormatVariant</name>
@@ -1985,7 +1990,7 @@
         </option>
         <option>
           <name>BufferedTermOutput</name>
-          <state>0</state>
+          <state>1</state>
         </option>
         <option>
           <name>XcRTLibraryFile</name>

+ 196 - 79
main.c

@@ -52,51 +52,96 @@ __C_task void main(void)
   // Tasks for sheduler
   RTOS_SetTask(startADC,0,50);
   RTOS_SetTask(getTime,4,500);
-  RTOS_SetTask(ds18b20_StartMeasure,8,10000);
+  RTOS_SetTask(showHHMM,8,500);
+  RTOS_SetTask(ds18b20_StartMeasure,12,10000);
   
-  /* Get time */
+  State = show_HHMM;
 
   while(1) {
-    /*
-    if (1 == Flag.newTime) {
-      Flag.newTime = 0;
-
-      Display[0] = IndctrNums[(0x0F & (Clock.hours>>4))];
-      Display[1] = IndctrNums[(0x0F & Clock.hours)];
-      Display[2] = IndctrNums[(0x0F & (Clock.minutes>>4))];
-      Display[3] = IndctrNums[(0x0F & Clock.minutes)];
-    }
-    */
-
-    if (1 == Flag.newTemp) {
-      Flag.newTemp = 0;
 
-      Display[0] = Sym_blank;
-      Display[1] = IndctrNums[(0x0F & (Temperature>>4))];
-      Display[2] = IndctrNums[(0x0F & Temperature)];
-      Display[3] = Sym_gradus;
+    /* check time */
+    if  (1 == Flag.newTime) {
+      Flag.newTime = 0;
+      
+      if (show_HHMM == State) {
+        // every 25 and 55 seconds shoe temperature for 5 seconds
+        if ((25 == Clock.seconds) || (55 == Clock.seconds)) {
+          RTOS_SetTask(showHHMM,5000,500);
+          RTOS_SetTask(showTEMP,0,1000);
+        }
+      }
     }
 
+    /* check buttons */
     if (1 == Flag.newBTN) {
       Flag.newBTN = 0;
       
       btn_t btn = getBTN();
-      if (btn == btn_1) {
-        LED_ON(LED_1);
-      } else if (btn == btn_2) {
-        LED_ON(LED_2);
-      } else if (btn == btn_3) {
-        LED_OFF(LED_1);
-      } else if (btn == btn_4) {
-        LED_OFF(LED_2);
-      } else if (btn == btn_5) {
-        LED_ON(LED_1);
-        LED_ON(LED_2);
-      } else if (btn == btn_6) {
-        LED_OFF(LED_1);
-        LED_OFF(LED_2);
+      switch (btn) {
+      case btn_1:
+        // show HH:MM
+        State = show_HHMM;
+        RTOS_SetTask(showHHMM,0,500);
+        break;
+
+      case btn_2:
+        // show MM SS
+        State = show_MMSS;
+        Flag.needDot = 0;
+        RTOS_SetTask(showMMSS,0,500);
+        break;
+
+      case btn_3:
+        // show temperature
+        State = show_TEMP;
+        Flag.needDot = 0;
+        RTOS_SetTask(showTEMP,0,1000);
+        break;
+
+      case btn_4:
+        // time --
+        if (Clock.minutes > 0) {
+          Clock.minutes --;
+        } else {
+          Clock.minutes = 59;
+          if (Clock.hours > 0) {
+            Clock.hours --;
+          } else {
+            Clock.hours = 23;
+          }
+        }
+        break;
+
+      case btn_5:
+        // time ++
+        Clock.minutes ++;
+        if (Clock.minutes > 59) {
+          Clock.minutes = 0;
+          Clock.hours ++;
+          if (Clock.hours > 23) {
+            Clock.hours = 0;
+          }
+        }
+        break;
+
+      case btn_6:
+        // save new time
+        Flag.blankIndktr = 0;
+        setTime();
+        RTOS_SetTask(getTime,4,500);
+        break;
+
+      case btn_7:
+        // time set
+        Flag.blankIndktr = 1;
+        RTOS_SetTask(getTime,0,0);
+        break;
+
+      default:
+        break;
       }
-    }
+      
+    } // end of new BTN
 
     tdelay_ms(10); // sleep and rotate dispatcher
   } // end of while(1)
@@ -105,6 +150,40 @@ __C_task void main(void)
 /**
  *  S u b r o u t i n e s
  */
+static void showHHMM(void) {
+  State = show_HHMM;
+  Display[0] = IndctrNums[(0x0F & (Clock.hours>>4))];
+  Display[1] = IndctrNums[(0x0F & Clock.hours)];
+  Display[2] = IndctrNums[(0x0F & (Clock.minutes>>4))];
+  Display[3] = IndctrNums[(0x0F & Clock.minutes)];
+}
+
+static void showMMSS(void) {
+  Display[0] = IndctrNums[(0x0F & (Clock.minutes>>4))];
+  Display[1] = IndctrNums[(0x0F & Clock.minutes)];
+  Display[2] = IndctrNums[(0x0F & (Clock.seconds>>4))];
+  Display[3] = IndctrNums[(0x0F & Clock.seconds)];
+}
+
+static void showTEMP(void) {
+  int8_t t = Temperature;
+  
+  if (t < 0) {
+    Display[0] = Sym_blank;
+    t = -t;
+  } else {
+    Display[0] = Sym_minus;
+  }
+
+  // convert to BCD
+  t = ((t/10)<<4)|(t%10);
+
+  Display[1] = IndctrNums[(0x0F & (t>>4))];
+  Display[2] = IndctrNums[(0x0F & t)];
+  Display[3] = Sym_gradus;
+  
+  Flag.needDot = 0;
+}
 
 static void startADC(void) {
   // enable interrupt and start conversion
@@ -135,6 +214,10 @@ static btn_t getBTN(void) {
 static void getTime(void) {
   static uint8_t old_sec;
 
+  /* Sync with indicators */
+  Flag.waitIndktr = 1;
+  while (Flag.waitIndktr == 1);
+ 
   /*устанавливаем указатель DS1307 на нулевой адрес*/
   twi_buf[0] = (DS1307_ADR<<1)|0; //адресный пакет
   twi_buf[1] = 0;                 //адрес регистра   
@@ -150,25 +233,32 @@ static void getTime(void) {
   Clock.seconds = twi_buf[1];
   Clock.minutes = twi_buf[2];
   Clock.hours = twi_buf[3];
-
-  if (old_sec != Clock.seconds) {
-    Flag.needDot = 1;
-    old_sec = Clock.seconds;
-  } else {
-    Flag.needDot = 0;
+  
+  if (show_HHMM == State) {
+    if (old_sec != Clock.seconds) {
+      Flag.needDot = 1;
+    } else {
+      Flag.needDot = 0;
+    }
   }
+
+  old_sec = Clock.seconds;
   Flag.newTime = 1;
 }
 
 static void setTime(void) {
-   /*подготавливаем сообщение*/
+  /* Sync with indicators */
+  Flag.waitIndktr = 1;
+  while (Flag.waitIndktr == 1);
+ 
+   /* prepare new time */
   twi_buf[0] = (DS1307_ADR<<1)|0; //адресный пакет
   twi_buf[1] = 0;                 //адрес регистра
-  twi_buf[2] = Clock.seconds;     //значение секунд 
+  twi_buf[2] = 0;                 //значение секунд 
   twi_buf[3] = Clock.minutes;     //значение минут 
   twi_buf[4] = Clock.hours;       //значение часов
 
-  /*отправляем его*/
+  /* senr to rtc */
   TWI_SendData(twi_buf, 5);
 }
 
@@ -279,6 +369,10 @@ static void ds18b20_WriteByte(uint8_t byte) {
 }
 
 static void ds18b20_StartMeasure(void) {
+  /* Sync with indicators */
+  Flag.waitIndktr = 1;
+  while (Flag.waitIndktr == 1);
+ 
   /* Reset, skip ROM and start temperature conversion */
   if (ds18b20_Reset() != 0) {
     Temperature = -99;
@@ -292,7 +386,12 @@ static void ds18b20_StartMeasure(void) {
 
 static void ds18b20_ReadTemperature(void) {
   uint8_t temperature[2];
+  uint8_t decimal;
 
+  /* Sync with indicators */
+  Flag.waitIndktr = 1;
+  while (Flag.waitIndktr == 1);
+ 
   /* Reset, skip ROM and send command to read Scratchpad */
   if (ds18b20_Reset() != 0) {
     Temperature = -98;
@@ -309,20 +408,19 @@ static void ds18b20_ReadTemperature(void) {
     /* Store temperature integer digits */
     Temperature = ((temperature[1]&0x7)<<4) | (temperature[0]>>4);
 
-    /* Get only integer part of temperature */
-//    Temperature = (int8_t)digit;
-
-    //выделяем с помощью битовой маски дробную часть
-    tempDecimals = (temperature[0] & 15);
+    // get decimal part
+    decimal = (temperature[0] & 15);
     
-    //преобразуем в целое число
-    tempDecimals *= 10; //(tempDecimals << 1) + (tempDecimals << 3);// Умножаем на 10
-    tempDecimals >>= 4; //(tempDecimals >> 4);//делим на 16 или умножаем на 0.0625
+    // convert to binary
+    //decimal *= 10; //(tempDecimals << 1) + (tempDecimals << 3);// Умножаем на 10
+    //decimal >>= 4; //(tempDecimals >> 4);//делим на 16 или умножаем на 0.0625
     
-    if (tempDecimals > 4) {
+    // round integer part
+    if (decimal > 7) {
       Temperature ++;
     }
   }
+  
   Flag.newTemp = 1;
 }
 
@@ -334,41 +432,60 @@ static void ds18b20_ReadTemperature(void) {
 #pragma vector=TIMER0_OVF_vect
 __interrupt void TIMER0_OVF_ISR(void) {
   static uint8_t indicator = 0;
+  static uint8_t blank_time = 0; // blanking timer
+  uint8_t flag = 1;
 
   TCNT0 = TIM0_CNT; // reload timer
 
   INDCTR_COMMON_PORT &= ~INDCTR_COMMON_ALL; // off all indikators
 
-  switch (indicator) {
-  case 0:
-    INDCTR_SEGMENT_PORT = Display[0];
-    INDCTR_COMMON_PORT |= 0x01;
-    indicator = 1;
-    break;
+  Flag.waitIndktr = 0;
 
-  case 1:
-    if (1 == Flag.needDot) {
-      INDCTR_SEGMENT_PORT = Display[1] | Sym_dot;
+  if (1 == Flag.blankIndktr) {
+    if (blank_time <= 200) {
+      blank_time ++;
+      flag = 1;
+    } else if (blank_time <= 250) {
+      blank_time ++;
+      flag = 0;
     } else {
-      INDCTR_SEGMENT_PORT = Display[1];
+      blank_time = 0;
+      flag = 1;
     }
-    INDCTR_COMMON_PORT |= 0x02;
-    indicator = 2;
-    break;
-
-  case 2:
-    INDCTR_SEGMENT_PORT = Display[2];
-    INDCTR_COMMON_PORT |= 0x04;
-    indicator = 3;
-    break;
-
-  case 3:
-    INDCTR_SEGMENT_PORT = Display[3];
-    INDCTR_COMMON_PORT |= 0x08;
-
-  default:
-    indicator = 0;
-    break;
+  }
+
+  if (1 == flag) {
+    switch (indicator) {
+    case 0:
+      INDCTR_SEGMENT_PORT = Display[0];
+      INDCTR_COMMON_PORT |= 0x01;
+      indicator = 1;
+      break;
+
+    case 1:
+      if (1 == Flag.needDot) {
+        INDCTR_SEGMENT_PORT = Display[1] | Sym_dot;
+      } else {
+        INDCTR_SEGMENT_PORT = Display[1];
+      }
+      INDCTR_COMMON_PORT |= 0x02;
+      indicator = 2;
+      break;
+
+    case 2:
+      INDCTR_SEGMENT_PORT = Display[2];
+      INDCTR_COMMON_PORT |= 0x04;
+      indicator = 3;
+      break;
+
+    case 3:
+      INDCTR_SEGMENT_PORT = Display[3];
+      INDCTR_COMMON_PORT |= 0x08;
+
+    default:
+      indicator = 0;
+      break;
+    } // end of switch
   }
 }
 

+ 15 - 2
main.h

@@ -113,7 +113,7 @@ const indctr_sym_t IndctrNums[16] = {
 #define DS18B20_HIGH            DS18B20_PORT|=(1<<DS18B20_PAD)
 #define DS18B20_VALUE           (DS18B20_PIN & (1<<DS18B20_PAD))
 
-static int8_t Temperature, tempDecimals;
+static int8_t Temperature;
 
 static uint8_t ds18b20_Reset(void);
 static void ds18b20_WriteBit(uint8_t bit);
@@ -156,15 +156,25 @@ typedef struct {
 } time_t;
 
 /* Global varaibles */
+typedef enum {
+  show_None = 0,
+  show_HHMM,
+  show_MMSS,
+  show_TEMP
+} state_t;
+
 volatile struct {
   uint8_t needDot       : 1;
   uint8_t newTime       : 1;
   uint8_t newTemp       : 1;
   uint8_t newBTN        : 1;
   uint8_t T1OC          : 1;
-  uint8_t               : 3; // дополнение до 8 бит (резерв) 
+  uint8_t setTime       : 1;
+  uint8_t blankIndktr   : 1;
+  uint8_t waitIndktr    : 1;
 } Flag;
 
+volatile static state_t State;
 volatile static indctr_sym_t Display[4];
 volatile static uint8_t resultADC;
 static uint8_t twi_buf[8];
@@ -172,6 +182,9 @@ static time_t Clock;
 static void setTime(void);
 
 /* Functions prototypes */
+static void showHHMM(void);
+static void showMMSS(void);
+static void showTEMP(void);
 static void startADC(void);
 static btn_t getBTN(void);
 static void getTime(void);