3.5 Coulomb Counting

One of the challenges with integrating the current flow is capturing a good average, without spending all available bandwidth managing the ADC. Charging is reasonably easy, with a constant current charge and a ramp down during the constant voltage. Discharge is more of a challenge, although having an ADCC helps, as it has an accumulated post conversion function. In this example, we have a regular ADC, so the firmware has to accumulate the current flow through multiple conversions. To provide a consistent sample rate, the ADC is triggered by TMR0.
Note: The accuracy of the discharge coulomb counting will be affected by fast transients in the load current. The micro can either sample faster, or put a low-pass on the output of the current mirror to average out the ADC current draw.