PDA

View Full Version : ADCIN failure



brid0030
- 5th November 2007, 20:05
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?

brid0030
- 5th November 2007, 21:25
I just noticed that TRISA should be a six-bit register for my PIC. But using "TRISA = %111111" did not fix the problem.

brid0030
- 5th November 2007, 22:12
OK. So I went back to the breadboard and started trying different pins. By a fluke (actually by mistake) I got it working when I used "ADCIN 1, adcVal" with the voltage input attached to PORTA.0 (NOT PORTA.1!!!). So what's going on here? Almost all of the examples of ADCIN I have seen use channel 0 and connect input to PORTA.0. Also, the first use of ADCIN always gives a 0. After that it seems to work fine. Does this make sense to anybody?

Bruce
- 5th November 2007, 22:29
ADCIN 0 should for sure read AN0. Do you have your analog input connected to pin #2 on
your 16F874A?

If so, which version of PBP are you using?

brid0030
- 6th November 2007, 14:50
The voltage input is indeed attached to pin 2--right next to MCLR. All the other pins on PORTA are floating and read out at about .9 volts.

I'm using PBP 2.47 issued by Reynolds Electronics on 11-9-06 (Cheers Bruce).

Bruce
- 6th November 2007, 15:54
Strange problem. I can run pretty much the same thing you have here on an 877A and it
works fine.


DEFINE OSC 20
@ DEVICE HS_OSC

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 PORTC.6,84,["Ready to go ", DEC W1, 10] 'Verify communication
pause 10

loop:
SERIN2 PORTC.7,84,[B0] 'Read in a character on PORTB.2
PAUSE 10
Serout2 PORTC.6,84, ["Getting ADC value", 10]
pause 10
ADCIN 0, adcVal 'perform ADC and store 10 bits in adcVal
pause 10
SEROUT2 PORTC.6,84,["ADC value: ", DEC adcVal, 10] 'Send the decimal value
Goto loop 'Start again

END
On a LAB-X1 board it reads the POT on RA0. Changing to ADCIN 1 it reads the POT on RA1.

What happens if you disable A/D and try blinking an LED on RA0?

brid0030
- 6th November 2007, 16:20
Yeah, I can blink an LED no problem. Of course that involves setting PORTA to output etc...

Well, I'll just go with what I have for now. This is a prototyping project, and it is clear to me now that the PIC16F874A is overkill--I will use a smaller, simpler PIC for the final version, and hopefully this problem will go away.

Thanks for the help.