Sam,
Here is some code that have working on a 18F4620. You might have to change things abit to make it work on your PIC.
Not familiar with the battery packs you are using. Are they really +/- 12V or are they 24 volts? Either way I suspect that your PIC would be very unhappy connected across 10 volts ( +/- 5 V ).
I hope this gets you started. Good luck.
Terry
; PIC18F4620
define OSC 40 ; 40 MHz Oscillator"
TRISA = %00000011 ; portA.0 and A.1 are inputs
TRISD = %00000000 ; Port D all outputs
ADCON1 = %0001101
; Comparator Control
CMCON = %00000110
;CMCON.7 = 0 ; C2OUT Comparator Output 2 (Read Only)
;CMCON.6 = 0 ; C1OUT Comparator Output 1 (Read Only)
;CMCON.5 = 0 ; C2INV Comparator 2 Output Inversion
;CMCON.4 = 0 ; C1INV Comparator 1 Output Inversion
;CMCON.3 = 0 ; Comparator Input Switch
;CMCON.2 = 1 ; )
;CMCON.1 = 1 ; )Mode 110
;CMCON.0 = 0 ; )
; ;
; Comparator Voltage Reference Control
CVRCON = %10101010
;CVRCON.7 = 1 ; CVREN Voltage Reference Enable (1 = on, 0 = OFF)
;CVRCON.6 = 0 ; CVROE VREF Output Enable
;CVRCON.5 = 1 ; CVRR Comparator Range Selection bit
;CVRCON.4 = 0 ; CVRSS Comparator Reference Source
;CVRCON.3 = 1 ; )
;CVRCON.2 = 0 ; ) Set for Reference Voltage
;CVRCON.1 = 1 ; ) 1010 sets 2.083v in Low Range
;CVRCON.0 = 0 ; ) 9.2 Volts from Battery
; Start Main Program Loop
loop:
if CMCON.6 = 1 then
CVRCON = %10101101 ; 2.083V
PortD.7 = 1
else
CVRCON = %10101010 ; 2.708V
PortD.7 = 0
endif
Goto loop ; Do it forever
Bookmarks