瀏覽代碼

Changed way of reading value.

Thomas Chef 2 年之前
父節點
當前提交
f6a4204867
共有 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;
 }