User Tools

Site Tools


Proof of concept: battery level measurement


Demo program showing that it is possible to measure the battery voltage with the onboard brownout detector.


Boot calc into test mode (to allow stock firmware to configure and init the chip), connect a debugger, halt the chip, download the image, and let 'er rip. It'll probably corrupt anything you had in ram before.


Another sample program for the HP20B. This is a proof of concept to illustrate that the brownout detector can be used to probe for the battery voltage.

The idea is that by ramping down the brownout threshold, we can find the point at which the unit goes out of brownout mode. When the threshold is above battery voltage, the brownout bit will be high. As the threshold drops, eventually it will be less than the battery. The last theshold at which the brownout bit was set can give us an approximate measure of the battery voltage. While it seems to work ok, it's not exactly linear. Not that it needs to be, for this application.

The first few hex digits on the display are the last value of the power supply controller status register. Mine shows 0x1d00 when not in brownout (threshold under battery), and 0x1d40 when in brownout (threshold above battery). The next hex digit is the current threshold voltage setting (refer to the book for conversion to volts), and the rightmost digit is the edge of brownout to no brownout.

Finally, the measured battery voltage is converted to decimal, scaled by 10, and placed in the exponent area of the display.

The probe loop is intentionally slow, so that you can see the probe process and brownout detector in action. Hook your calc up to a variable power supply and see the voltages change. (Note, the exponent display is only updated at the end of the probe loop, so it'll be several seconds before you see it change. Adjust your supply *slowly*.)

The keys are not scanned.

Runs from ram, Thumb mode, trashes memory, and requires some sort of JTAG debugger to load and start the program. OCD Commander reports a starting address of 0x002FF000, in case your debugger can't figure it out.

Under OpenOCD, I do a “soft_reset_halt”, then a “load_image battery_level.hex”, followed by a “resume 0x2ff000”. The startup code wants to run in ARM mode, so be sure the core is set accordingly. Once it jumps to main, it should switch cleanly into Thumb mode. (Or at least it seems to for me, single-stepping through it.)

Disclaimer: Yes, the code is ugly. Fix it at your own risk. Consider it nothing more than a toy to demonstrate that we know a little about the hardware.

Happy HP Hacking!

20b/software-battery_level.txt · Last modified: 2008/07/28 19:49 by newell