소스 검색

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;
 }