Преглед на файлове

Changed way of reading value.

Thomas Chef преди 2 години
родител
ревизия
a7f8abc5ba
променени са 1 файла, в които са добавени 9 реда и са изтрити 4 реда
  1. 9 4
      main/pcnt_functions.c

+ 9 - 4
main/pcnt_functions.c

@@ -26,10 +26,15 @@ const int pcntUnit = PCNT_UNIT_0;
 
 uint32_t getkWh() {
 
-    pcnt_intr_disable(pcntUnit);
-    volatile int32_t count = DPORT_REG_READ(0x3FF57060);
-    volatile uint32_t bigCounter = Counter_32K_Pulses;
-    pcnt_intr_enable(pcntUnit);
+    volatile uint32_t firstRead, count, bigCounter;
+
+    // Loop until we have two identical reads of the interrupt-updated 32K-counter
+    // The 32K-counter is only updated maybe once per 24h, so should not be a problem
+    do {
+        firstRead = Counter_32K_Pulses;
+        count = DPORT_REG_READ(0x3FF57060);
+        bigCounter = Counter_32K_Pulses;
+    } while( firstRead != bigCounter );
 
     return (bigCounter * 32000) + count;
 }