Forráskód Böngészése

Версия с float. Компилиться...

Vladimir N. Shilov 8 éve
szülő
commit
6f1f29415a
2 módosított fájl, 59 hozzáadás és 29 törlés
  1. 7 0
      ReadMe.txt
  2. 52 29
      src/main.c

+ 7 - 0
ReadMe.txt

@@ -123,3 +123,10 @@ RTOS выкинул. на очереди SPL? :-)
 автокоррекция смещения "0" на входе АЦП -- будет получено и сохранено в EEPROM
 значение АЦП при нулевом токе на входе, которое затем будет использовано при
 расчётах.
+
+---
+2016.11.23
+
+Попробую float.
+Прошивка сразу на 896 байт больше, и теперь 3235 байт. на ОЗУ не повлияло.
+Перепроверю в IAR-е -- минимум 3349 байт. странно. проверить бы в железе...

+ 52 - 29
src/main.c

@@ -109,11 +109,11 @@ static void PrepareForLed(void);
 __IO uint16_t ConversionBuffer[ADC_SMPLS];
 __IO uint8_t BufferIndex = 0;
 __IO uint8_t LedCnt = 0;
-static uint16_t Current = 0;
-static uint32_t Capacity = 0;
+static float Current = 0.0;
+static float Capacity = 0.0;
 static uint8_t LedDigits[3] = {0, 0, 0};
 static uint8_t LedPoint = 3;
-static uint16_t SubSecondBfr = 0;
+static float SubSecondBfr = 0.0;
 static uint8_t SubSecondCnt = SUB_SECOND_CNT;
 static uint16_t ADC_ZeroFix = 0;
 
@@ -175,7 +175,7 @@ void main(void)
         BufferIndex = 0;
 
         /* Oversampling */
-        uint32_t tbuf = 0;
+        uint16_t tbuf = 0;
         uint8_t i = 0;
         for(i=0; i<ADC_SMPLS; i++){
           tbuf += ConversionBuffer[i];
@@ -194,26 +194,21 @@ void main(void)
           tbuf -= ADC_ZeroFix;
 
           tbuf >>= 3; // pre div
-          tbuf *= 3335; // Vref = Vcc
-          tbuf = (tbuf + 4096) / 8191; // get ADC input voltage in mV
-          tbuf *= 1000; // -- for OU divider //get voltage in uV
-          tbuf = (tbuf + 3147) / 6294; // get voltage from shunt
+          Current = tbuf * 3.335; // Vref = Vcc
+          Current /= 8191; // get ADC input voltage in mV
+          Current /= 6.29412; // get voltage from shunt
           /* â Current òîê â ìèëèàìïåðàõ */
-          tbuf *= 1000; // get voltage in uV
-          Current = (tbuf + 25) / 50; // shunt resistance in mOhms
+          Current /= 0.05; // shunt resistance in mOhms
 
           SubSecondBfr += Current;
 
-        } else {
-          tbuf = 0;
-          Current = 0;
         }
 
         SubSecondCnt --;
 
         if(SubSecondCnt == 0){
           Capacity += ( (SubSecondBfr + (SUB_SECOND_CNT/2)) / SUB_SECOND_CNT );
-          SubSecondBfr = 0;
+          SubSecondBfr = 0.0;
           SubSecondCnt = SUB_SECOND_CNT;
         }
 
@@ -262,7 +257,7 @@ static void ADC_CorrectZero(void) {
 
 /** Prepare current value for output ot led */
 static void PrepareForLed(void) {
-  uint16_t value;
+  float value;
 
   if((BTN_PORT->IDR & BTN_PINS) == 0){
     value = Capacity / 3600;
@@ -270,27 +265,55 @@ static void PrepareForLed(void) {
     value = Current;
   }
 
-  if(value > 9999){
+  LedDigits[0] = 0;
+  LedDigits[1] = 0;
+  LedDigits[2] = 0;
+
+  if(value >= 10.0){
     /* XX.X Amper */
     LedPoint = 1;
-    LedDigits[0] = value / 10000;
-    value = value % 10000;
-    LedDigits[1] = value / 1000;
-    LedDigits[2] = (value % 1000) / 100;
-  } else if(value > 999){
+    while(value >= 10.0){
+      LedDigits[0] ++;
+      value -= 10.0;
+    }
+    while(value >= 1.0){
+      LedDigits[1] ++;
+      value -= 1.0;
+    }
+    while(value >= 0.1){
+      LedDigits[2] ++;
+      value -= 0.1;
+    }
+  } else if(value >= 1.0){
     /* X.XX Amper */
     LedPoint = 0;
-    LedDigits[0] = value / 1000;
-    value = value % 1000;
-    LedDigits[1] = value / 100;
-    LedDigits[2] = (value % 100) / 10;
+    while(value >= 1.0){
+      LedDigits[0] ++;
+      value -= 1.0;
+    }
+    while(value >= 0.1){
+      LedDigits[1] ++;
+      value -= 0.1;
+    }
+    while(value >= 0.01){
+      LedDigits[2] ++;
+      value -= 0.01;
+    }
   } else {
     /* XXX mili Amper */
     LedPoint = 3;
-    LedDigits[0] = value / 100;
-    value = value % 100;
-    LedDigits[1] = value / 10;
-    LedDigits[2] = value % 10;
+    while(value >= 0.1){
+      LedDigits[0] ++;
+      value -= 0.1;
+    }
+    while(value >= 0.01){
+      LedDigits[1] ++;
+      value -= 0.01;
+    }
+    while(value >= 0.001){
+      LedDigits[2] ++;
+      value -= 0.001;
+    }
   }
 }