Many of the 18F-series micros have an HIGH/LOW-VOLTAGE DETECT (HLVD) module that can be used to monitor VDD and warn if it has decreased below a specified voltage or increased above a specified voltage. This is a great technique for monitoring the supply voltage of a micro that is directly battery powered. It has several advantages over using an ADC channel for the purpose:
No port pins are needed to implement.
No external components are required and no power is consumed by an external voltage divider.
The HLVD module uses an internal band-gap reference and draws a maximum of 45 µA when enabled. It can be disabled except when needed to reduce even this tiny current draw.
It can generate an interrupt when Vdd is below or above (depending on how it's configured) the selected set point.
I did forget to mention one important point. You must keep in mind the allowable operating voltage of the micro in determining what low-level threshold to use.
For my tests, I used an 18F2520, and it was operating below 3.85 volts. What is the allowable supply voltage range for this part? According to the graph from the data sheet, it's 4.2 volts. The chip may work below this voltage but it's not guaranteed. It may fail in strange ways below this voltage.
For a battery-powered application, the 18LF2520 would be a better choice. But look carefully at the graph below. The maximum allowed clock speed is a function of supply voltage. The LF version will operate down to 2 volts...at a maximum clock speed of 4 MHz.
A better choice for a battery-powered application would be the 18F25K22. It will operate down to 1.8 volts at a clock speed of 20 MHz.
There was an off-line question about my battery life estimation. I realize this may be unclear. Consulting the illustration below, the battery discharge curve shows voltage vs some unit of time for a constant current load. At t = 0, the battery is fresh and the voltage is about 1.6 volts per cell. At t = 8, the voltage has dropped to 1.1 volts per cell and by t = 9.5, the voltage is less than 1 volt per cell and falling quickly. At t = 10, the cell voltage is 0.8 volts and there's little juice left in the battery.
This was a convenient curve to pick. The red numbers at the top of the graph show percentage of life used. At t = 10, the battery is essentially dead - 100% of life used.
So we can look at any point along the curve and estimate the life remaining based on the cell voltage. This is an estimate that depends on the cell type, the load current and the nature of the load. The best estimates will be based on a curve at approximately the same load as your circuit.
Congratullations and thanks for this wonderful article.
At the begining you say: "This is a great technique for monitoring the supply voltage of a micro that is directly battery powered."
Before to read your article I was planning to use the 'Ultra Low-Power Wake-up (ULPWU)' input pin to detect the low voltage wall power supply and the HLDV input to monitor the low voltage battery but after to read your article I've some troubles as if both signals are or not compatibles and they can do the functions as I had thought.
Can you write any comment about that?. (I use the 18F87K22)