Re: Inconsistent results measuring pulses
Posted: Sat Aug 24, 2019 5:17 am
Easily achieved. The interrupt handler updates a global. Before you clear down the global, disable interrupts. Read the global, clear it down, and re-enable interrupts. Ensure that the period for which interrupts are disabled is shorter than the shortest interval between pulses:
The reason for clearing down the global is to avoid it exceeding 30 bits in size, which would lead to it becoming a long integer which cannot be handled in an ISR. If your consumption won't exceed 268MWH between reboots you could just let the ISR update the running total and skip this bit of code.
Code: Select all
state = machine.disable_irq() running_total += gpulse_count gpulse_count = 0 machine.enable_irq(state)