I'm trying to use an A/D converter for the first time and I'm totally stumped. I'm not doing anything fancy here--just want to convert a voltage and send the digital value to a pc with SEROUT2. The serial communication works fine, but the ADCIN value always prints out as 0. This is despite the fact that I can verify voltages on PORTA.0 between 0.5 and 4.5 V using a multimeter. I've pretty much copied my code from working examples--Here's what I've got:
' PIC16F874A with a 20MHz crystal
' Serial out pin connected to DB9 pin 2 through 100ohm
' Serial in pin connected to DB9 pin 3 through 22Kohm
' Ground connected to DB9 pin 5
' Input voltage for ADC connected to PORTA.0
DEFINE OSC 20
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 50
B0 VAR BYTE 'just used for SERIN input and testing
adcVal VAR WORD 'used for ADC value
W1 VAR WORD 'used to test DEC function in SEROUT2
TRISA = 255 'PORTA is all input
ADCON1 = %10000000 'PORTA analog, no ref voltages, output right justified
PAUSE 500
W1 = 1022 'just to test DEC
SEROUT2 1,16780,["Ready to go ", DEC W1, 10] 'Verify communication
pause 10
loop:
SERIN2 2,16780,[B0] 'Read in a character on PORTB.2
PAUSE 10
Serout2 1,16780, ["Getting ADC value", 10]
pause 10
ADCIN 0, adcVal 'perform ADC and store 10 bits in adcVal
pause 10
SEROUT2 1,16780,["ADC value: ", DEC adcVal, 10] 'Send the decimal value
Goto loop 'Start again
END
I know there is a lot of unnecessary stuff in there. I will parse it all down once the ADC is working. The serial output is always "ADC value: 0" no matter what voltage is going to PORTA.0. I've played around with 8-bit output and right vs. left justification, but no luck. The spec sheet for the PIC16F874A does not indicate anything unusual about the device (there is no ANSEL register, and PORTA.0 should work as an ADC input). Finally, I'm using factory-fresh PICs--same results on multiple ICs. Anybody got any ideas or resources I may not have turned up?
Bookmarks