Measuring the PicAxe Supply Voltage

crowland

Member
Here's a code fragment I use for monitoring the supply voltage:
Code:
symbol ADCON0 = $1f

readBattery:
	; enable the ADC
	peek ADCON0, b2
	b2 = b2 or 1		; set bit 0 to enable the ADC
	poke ADCON0, b2
	; get the supply voltage by reading the 1.024V
	; adc calibration voltage using the supply voltage
	; as the DAC reference.
	calibadc10 w0
	; disable the ADC to save power
	peek ADCON0, b2
	b2 = b2 and $FE		; clear bit 0 to disable the ADC
	poke ADCON0, b2
	; convert to 1/10V
	; calib adc is 1.024V
	; V = 1.024 * 10 * 1024/adc
	w0 = 10486/w0	; voltage in 1/10 V in low byte
	battery = b0
	return
The ADCON0 stuff is because I originally used this in a low power circuit, it isn't needed unless you want to obsess about the power.
The battery voltage can be used to prompt the user to replace the batteries.

Chris
 
Top