'Program Battery Tester 20V
main:						  
	let w6 = 80				'Starting point for variable w6
calibrate:
	readadc10 2, w5		      'Read the reference diode

	pause 50
	w2=w5*60/w6			   'Calculate reference voltage
	if w2 = 70 then VM		    'If wrong goto next step, otherwise measure input
	inc w6					 'Increase the reference variable by 1
	goto calibrate			     'Try again
VM:
	let b6 = 0				   'Reset number of negative readings
	let b7 = 0				   'Reset number of positive readings
	let w2 = 0				  'Reset positive reading sum
	let w5 = 0				  'Reset negative reading sum
VM1:
	readadc10 4, w0		      'Get zero volt reference (approx. 1/2 b+)
	pause 10
	readadc10 1, w1		       'Read the input voltage
	pause 10
	if w1 < w0 then VNeg		'If input voltage is less than zero go to negative readout
	inc b7
	w2=w1-w0*67/w6+w2	    'Calculate for decimal readout
	if b7 = 10 then
	b7=w2/100				 'Get the digit before the decimal point
	b6=w2//100				'Get the decimal portion of the voltage
	b6=b6/10				    'Drop changing .0x digit for more stable output
	sertxd (#b7,".",#b6, " Volts",13,10)   'Display positive reading
	else 
	goto VM1
	endif

nopush:
	pause 50
	goto VM                                    

VNeg:
	inc b6
	w5=w0-w1*67/w6 +w5	    'Calculate for decimal readout
	if b6 = 10 then
	b7=w5/100				 'Get the digit before the decimal point
	b6=w5//100
	b6=b6/10				    'Get the decimal portion of the voltage
	sertxd ("- ",#b7,".",#b6, " Volts",13,10)   'Display negative reading
	else 
	goto VM1
	endif	
	goto nopush
 