I wired three 10K ohm resistors in series (because I had lots of 10K resistors!) to form a simple voltage divider. After double checking with a multimeter, I connected porta.0 of a 16f877 to the resistor node that was less than 5 volts, and used the following code to read and display the battery voltage;
/code
'************************ DEFINE ADCIN PARAMATERS
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 0 ' Set clock source (Fosc/2)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
ADCON1 = %10001110 'Right justify, channel 0 is analog
'*************************** END ADCIN SETUP
adcin 0,bat_volts 'read battery voltage
bat_volts = (bat_volts */500)>> 2
lcdout $fe,$80+15,dec(bat_volts/100 *3),".",dec1 bat_volts
/endcode
Please note that the Pic itself is powered via a 7805 regulator as suggested in an earlier post, with appropriate capacitors, and a fuse just to be safe. I didn't use precision resistors, I didn't need great accuracy. You might add a zener diode for extra protection.
I hope this helps get you started.
Jerry.
Bookmarks