PICers, There was a thread a while back about how to use a PIC port to test a battery voltage. the best idea was to connect the battery to a port pin thru a resistor & capacitor, make the pin briefly an o/p to discharge the battery, then make the pin an i/p and count how long it takes for the voltage to reach logic 1. Too slow = low volts. I'd probably go about it like so. First of all ... do you reckon it'd work? (Haven't tested it so far.) Secondly, in the btfss loop, if the RA0 voltage never makes it to logic 1, say if the pin is disconnected for some unknown reason, you get an infinite loop. That's a Bad Thing. Any thoughts, ideas etc? Ian ;******************************************************************* ;TSTBAT .. battery test routine. Looks at TERMINAL pin on PORTA ; and counts the time needed to charge a cap from the battery supply. ;If the count exceeds MAX, the battey is taking too long and it's ;probably kaput. Flag battery good = 1 so as to skip on true. ; ;Definitions needed :- ;MAX equ ??? .. to be determined ;MIN equ ??? .. to be determined ;TERMINAL equ PORTA,0 ;BATTERY equ flags,???? ; ;Registers needed :- ;count ;flags ;CCT Configuration :- ; | to PORTA ; Battery Vcc ----^^^^^----||----GND ;******************************************************************** tstbat bsf flags,BATTERY ;BATTERY.0 => battery volts LOW ;BATTERY.1 => battery volts OK BANK_1 bcf TRISA,TERMINAL ;PORTA.0 => OUTPUT BANK_0 bcf PORTA,TERMINAL ;PORTA.0 - LOW, Discharge the capacitor movlw MAX movwf count decfsz count,1 ;Make sure the capacitor is discharged goto $-1 BANK_1 br1 bsf TRISA,TERMINAL ;Then make PORTA.0 an INPUT BANK_0 movlw MIN ;Load the starting value of the count movwf count loop incf count,1 btfss PORTA,TERMINAL ;Voltage risen to logic 1 yet? goto loop ;No - loop'n'count till it does ;Yes - see if the count has exceeded the MAX allowable. ;If it has, the battery's taking too long to charge the cap. movf count,w ;Is count >= MAX? tony nixon's >= test from PIC Archive. addlw 255-MAX+1 ;If MAX >= 5 ... addlw d'251' btfsc STATUS,C ;Use btfss & get rid of the GOTO? goto exit br2 bcf flags,BATTERY ;Flag battery = "bad" exit retlw 0x00 -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body