I can not get the 10-bit A/D working correctly. The program below reads AN0 and turns an LED on if the voltage drops below half of VDD. At least that is what I want it to do. I am expecting to see a result of 1024 at 5 volts and 512 at 2.5 volts. The problem I am having is I don’t get 1024 or even close. If I DEFINE ADC_BITS 10 then the result is over 64000 at 5 volts. If I leave the DEFINE ADC out of the program I get 256 at 5 volts. I don’t know what is going on. Why don’t I get 1024 at 5V? I have been reading the data sheet for over an hour and have not been able to figure this out. The program is below.

Thanks,
Mark

DEFINE OSC 20

' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
' Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

ADCON0 = %1000101 '
ADCON1 = %1000010 ' AN4-AN0 A,A,A,A,A right justify
TRISA.0 = 1
TRISA.1 = 1

' TRISB.4 = 0 ' Set PORTB.4 to output
' TRISB.5 = 0 ' Set PORTB.5 to output
' TRISB.3 = 0 ' Set PORTB.3 to output

adval1 var word
adval2 var word

clear
High PORTB.5 ' Turn on run LED
low PORTB.4

loop:
ADCIN 0, adval1 ' Read AN0
' adcin 1, adval2 ' Read AN1

if adval1 < 512 then
high PORTB.4
else
low PORTB.4
endif

serout2 PORTB.3,16572,["ADVAL_1 = ",dec adval1,13,10]
pause 100
goto loop
End