PDA

View Full Version : 16F684 ADC problem



markedwards
- 24th August 2006, 18:54
I am having a problem reading the RA4 (AN3) pin 3 on a 16F684.
I have several boards that perform the same.
I can read RA0 (AN0) OK. Am I missing a setting?

TRISA = %11011111 ' Config I/O s
CMCON0 = 7 '%00000111 ' Comparators OFF
CCP1CON = %00000000
ANSEL = %00001001

Loop
ADCIN 3,AD4 'read channel 4...AN3
AD4 = AD4 >> 6
SerOut PORTA.5, n2400,[I,192,#AD4," "]
goto loop

markedwards
- 24th August 2006, 21:56
I can read AN0, AN1, AN2 but not AN3, AN4 data appears to change with AN2.

AM I missing a configuration setting?

INCLUDE "modedefs.bas"
DEFINE CHAR_PACING 500 '
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
WPUA=%00000000
OSCCON = $60 ' WDT_OFF,
@ DEVICE PIC16F684, INTRC_OSC_NOCLKOUT, PWRT_Off, MCLR_OFF, BOD_OFF,PROTECT_OFF
AD0 VAR WORD ' AD0 word (10 bits)
AD1 VAR WORD ' AD1 word (10 bits)
AD2 VAR WORD ' AD2 word (10 bits)
AD3 VAR WORD ' AD3 word (10 bits)
AD4 VAR WORD ' AD4 word (10 bits)
TRISA = %11011111
TRISC = %11000000
CMCON0 = 7 '%00000111 ' Comparators OFF
CCP1CON = %00000000
adcon0=%00000001
ANSEL = %00000001
LOW PORTC.1

loop:

ADCIN 0,AD0 'read channel 0...AN0
AD0 = AD0 >> 6
ADCIN 2,AD2 'read channel 2...AN2
AD2 = AD2 >> 6
ADCIN 3,AD3 'read channel 3...AN3
AD3 = AD3 >> 6
ADCIN 4,AD4 'read channel 4...AN4
AD4 = AD4 >> 6
SerOut PORTA.5, n2400,[I,192,#AD0," ",#ad2," ",#AD4," "]
SerOut PORTA.5, n2400,[I,128,#AD3," "]

goto loop

Archangel
- 25th August 2006, 00:42
http://ww1.microchip.com/downloads/en/DeviceDoc/41202C.pdf#search=%2216f684%20data%22

the link above will get you the data sheet, the code below is copied from the data sheet page 33 I believe it is what you need, I would add that input pin appears to require a pullup resistor too.
ASM
BCF STATUS,RP0 ; BANK 0
CLRF PORTA ;Init PORTA
MOVLW 07H ; Set RA >2:0> to
MOVWF CMCON0 ;Digital I/O
BSF STATUS, RP0 ;Bank 1
CLRF ANSEL ;Digital I/O
MOVLW 0Ch ;Set RA<3:2> as inputs
MOVWF TRISA ; and set RA<5:4,1:0>

BCF STATUS,RP0 ;Bank 0
ENDASM

if this turns out to be wrong, keep this in mind,
I did all the typing for you ;)

JS
More lost than many . . too dumb to know it, too darn old to care.

mister_e
- 25th August 2006, 03:24
@MarkEdwards
Your ANSEL setting is wrong. Try
ANSEL %00011111

markedwards
- 25th August 2006, 06:51
Thanks MR E for the reply!

pin 3 RA4, AN3 seems like it is an output because it is low impeadance.
I am trying to measure 0 - 4 vdc and have a 10k series
resistor and a .1uf capacitor. The voltage is loaded down
on some boards, a few boards pin 3 seems to work.
I figure I am missing some configuration setting or don't understand
something. 3 other adc inputs work as expected.

Mark