Răsfoiți Sursa

tweak getBattMilliVolts to report battery more accurately

Tomas P 9 luni în urmă
părinte
comite
429f82106b
1 a modificat fișierele cu 14 adăugiri și 7 ștergeri
  1. 14 7
      variants/thinknode_m2/ThinknodeM2Board.cpp

+ 14 - 7
variants/thinknode_m2/ThinknodeM2Board.cpp

@@ -19,14 +19,21 @@ void ThinknodeM2Board::begin() {
     enterDeepSleep(0);
   }
 
-  uint16_t ThinknodeM2Board::getBattMilliVolts()  {
+ uint16_t ThinknodeM2Board::getBattMilliVolts() {
     analogReadResolution(12);
-    delay(10);
-    float volts = (analogRead(PIN_VBAT_READ) * ADC_MULTIPLIER * AREF_VOLTAGE) / 4096;
-    analogReadResolution(10);
-    return volts * 1000;
-  }
+    analogSetPinAttenuation(PIN_VBAT_READ, ADC_11db);
+
+    uint32_t mv = 0;
+      for (int i = 0; i < 8; ++i) {
+        mv += analogReadMilliVolts(PIN_VBAT_READ);
+        delayMicroseconds(200);
+      }
+    mv /= 8;
+
+    analogReadResolution(10);  
+    return static_cast<uint16_t>(mv * ADC_MULTIPLIER );
+}
 
   const char* ThinknodeM2Board::getManufacturerName() const {
     return "Elecrow ThinkNode M2";
-  }
+  }