18f2321 AD faulty pic perhaps?

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,395
Got this AD routine working ok, but i have to select AN1 i/p when i am using AN0, and it wont select any other input on the AN0-12 channels.

I have a pot wired to AN0 (pin2) but i have to set the ADCON0 to select AN1,(00000001) otherwise it wont work,

yet if i put the pot onto AN1 (pin3) and select AN1 (00000001) or AN0 (00000000) it wont work? is it possible the pic is at fault?


I can get it to work on another pic (different model number) on all An0-10 no problem, never experienced this fault before!!!

Here is my set up if its any help, i have ordered a new pic in the meantime,
Rich (BB code):
Main: setup ports
        movlw  b'01110111'     ; osc ferequency 8mhz
        movwf  OSCCON
    	movlw  b'00000001'	  ;set digital i/o portA 
        movwf  ADCON1         ;set all to digital I/O
        clrf   PORTA
        clrf   LATA
		movlw b'00001111'  ; set porta,0,1 to input, rest outputs
        movwf   TRISA				     
		MOVLW  07h 		
		MOVWF  CMCON        ; comparators off
		clrf   PORTB
        clrf   LATB 
        clrf   TRISB         ;  ALL B/C PORTS TO OUTPUTS .
     	clrf   PORTC	     ; PORTA,0 INPUT. A1-7 OUTPUTS.
        clrf   LATC
        clrf   TRISC	

;************************************
; speed routine

	MOVLW b'00000001' ; AN0 source input =1(bit 0)  
	MOVWF ADCON0      ; set file configs 
	MOVLW b'00001100' ; set up vref internal, ( Vref bits 4,5,)  AN0-AN2 i/ps 
	MOVWF ADCON1      ; set up reg 
	MOVLW B'00101011' ; SET CLOCK FREQ FRC, TAD 12bits,LEFT justified bit7=0,
	MOVWF ADCON2      ; do it
	Call Delay                ; wait 50ms                      
	BSF ADCON0,GO    ;Start conversion
wait
	BTFSC ADCON0,GO  ;Is conversion done?
	GOTO wait         ;No, test again
	MOVFF ADRESH,RESULTHI   ; store upper 2 bits in resulthi  
	MOVFF ADRESL,RESULTLO   ; store lower 8 bits in resultlo
	
	return
 
Last edited:
@Dodgydave,

In the PIC18fxx31 device, you have picked one of the most advanced, or I'll call it bizarre, a-d modules in the PIC universe. Here is just some of the parsing code for GCBasic (ADREADPORT is synonymous with AN0 etc. I beleive, it's been quite awhile so forgive me).

Rich (BB code):
        'Set Auto or Single Convert Mode
        #IFDEF Bit(ACONV)
            SET ACONV OFF  'Single shot mode 
            SET ACSCH OFF  'Single channel CONVERSION
            'GroupA
            IF ADReadPort = 0 OR ADReadPort = 4 OR ADReadPort = 8 Then
                SET ACMOD1 OFF
                SET ACMOD0 OFF
            END IF
            'GroupB
            IF ADReadPort = 1 OR ADReadPort = 5 Then
                SET ACMOD1 OFF
                SET ACMOD0 ON
            END IF
            'GroupC
            IF ADReadPort = 2 OR ADReadPort = 6 Then
                SET ACMOD1 ON
                SET ACMOD0 OFF
            END IF
            'GroupD
            IF ADReadPort = 3 OR ADReadPort = 7 Then
                SET ACMOD1 ON
                SET ACMOD0 ON
            END IF
            
        #ENDIF


        #IFDEF BIT(GASEL0)
            'GROUP A SELECT BITS
            IF ADReadPort = 0 THEN
                SET GASEL1 OFF
                SET GASEL0 OFF
            END IF
            IF ADReadPort = 4 THEN
                SET GASEL1 OFF
                SET GASEL0 ON
            END IF
            IF ADReadPort = 8 THEN
                SET GASEL1 ON
                SET GASEL0 OFF
            END IF
            'GROUP C SELECT BITS        
            IF ADReadPort = 2 THEN
                SET GCSEL1 OFF
                SET GCSEL0 OFF
            END IF
            IF ADReadPort = 6 THEN
                SET GCSEL1 OFF
                SET GCSEL0 ON
            END IF
            'GROUP B SELECT BITS
            IF ADReadPort = 1 THEN
                SET GBSEL1 OFF
                SET GBSEL0 OFF
            END IF
            IF ADReadPort = 5 THEN
                SET GBSEL1 OFF
                SET GBSEL0 ON
            END IF
            'GROUP D SELECT BITS
            IF ADReadPort = 3 THEN
                SET GDSEL1 OFF
                SET GDSEL0 OFF
            END IF
            IF ADReadPort = 7 THEN
                SET GDSEL1 OFF
                SET GDSEL0 ON
            END IF
 

joeyd999

Joined Jun 6, 2011
6,331
@Dodgydave,

In the PIC18fxx31 device, you have picked one of the most advanced, or I'll call it bizarre, a-d modules in the PIC universe. Here is just some of the parsing code for GCBasic (ADREADPORT is synonymous with AN0 etc. I beleive, it's been quite awhile so forgive me).
Ummm....there's little difference between A/D converters with respect to almost any PIC part. Just because something *seems* difficult in someone's arcane high-level language doesn't mean that it is at the hardware level.
 

joeyd999

Joined Jun 6, 2011
6,331
Got this AD routine working ok, but i have to select AN1 i/p when i am using AN0, and it wont select any other input on the AN0-12 channels.

I have a pot wired to AN0 (pin2) but i have to set the ADCON1 to select AN1,(00000001) otherwise it wont work,

yet if i put the pot onto AN1 (pin3) and select AN1 (00000001) or AN0 (00000000) it wont work? is it possible the pic is at fault?

I can get it to work on another pic (different model number) on all An0-10 no problem, never experienced this fault before!!!

Here is my set up if its any help, i have ordered a new pic in the meantime,
ADCON1 does not select the active channel, it tells the chip which group of inputs are analog (vs. digital).

ADCON0, bits 2-5 select the channel.

I didn't look closely at your code, but hope this helps. My guess is the chip is fine (and I use this chip in current production).
 
Ummm....there's little difference between A/D converters with respect to almost any PIC part. Just because something *seems* difficult in someone's arcane high-level language doesn't mean that it is at the hardware level.
All I'm saying is that there's more to than just setting up say TRIS, ANSEL, ADRESX, and ADCON registers with the 18fxx31 devices. If I could do it, than it is not difficult. Just alerting the OP to the fact that more is involved. Nine registers to look at, including four ADCON registers, and don't forget the ADCHS, or a-d channel select register.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,395
ADCON1 does not select the active channel, it tells the chip which group of inputs are analog (vs. digital).

ADCON0, bits 2-5 select the channel.

I didn't look closely at your code, but hope this helps. My guess is the chip is fine (and I use this chip in current production).
Yeh i know i put the wrong register in the title, but it still wont work.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,395
@dodgydave,

in the pic18fxx31 device, you have picked one of the most advanced, or i'll call it bizarre, a-d modules in the pic universe. Here is just some of the parsing code for gcbasic (adreadport is synonymous with an0 etc. I beleive, it's been quite awhile so forgive me).

Rich (BB code):
        'set auto or single convert mode
        #ifdef bit(aconv)
            set aconv off  'single shot mode 
            set acsch off  'single channel conversion
            'groupa
            if adreadport = 0 or adreadport = 4 or adreadport = 8 then
                set acmod1 off
                set acmod0 off
            end if
            'groupb
            if adreadport = 1 or adreadport = 5 then
                set acmod1 off
                set acmod0 on
            end if
            'groupc
            if adreadport = 2 or adreadport = 6 then
                set acmod1 on
                set acmod0 off
            end if
            'groupd
            if adreadport = 3 or adreadport = 7 then
                set acmod1 on
                set acmod0 on
            end if
            
        #endif


        #ifdef bit(gasel0)
            'group a select bits
            if adreadport = 0 then
                set gasel1 off
                set gasel0 off
            end if
            if adreadport = 4 then
                set gasel1 off
                set gasel0 on
            end if
            if adreadport = 8 then
                set gasel1 on
                set gasel0 off
            end if
            'group c select bits        
            if adreadport = 2 then
                set gcsel1 off
                set gcsel0 off
            end if
            if adreadport = 6 then
                set gcsel1 off
                set gcsel0 on
            end if
            'group b select bits
            if adreadport = 1 then
                set gbsel1 off
                set gbsel0 off
            end if
            if adreadport = 5 then
                set gbsel1 off
                set gbsel0 on
            end if
            'group d select bits
            if adreadport = 3 then
                set gdsel1 off
                set gdsel0 off
            end if
            if adreadport = 7 then
                set gdsel1 off
                set gdsel0 on
            end if

its an 18f2321 !! Not 18f**31, and further more if the new one is the same its getting swapped for a 18f24k22
 

bance

Joined Aug 11, 2012
315
I think you're brave trying to programme 18F**** PICs in ASM, I'm sorry I can't help, but hope I'm encouraging you to get it done....

Best of luck, Steve.
 

tshuck

Joined Oct 18, 2012
3,534
I would suggest first doing what the datasheet says on page 237 in order to perform a conversion. There have been times where I've tried doing it out of the specified order, only to realize doing it in the order listed solves the problem...

Next, ensure your acquisition time is being met properly, my bet is that it isn't. Read page 238 for more information...Table 20-1 would suggest you are outside the operating parameters.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,395
I would suggest first doing what the datasheet says on page 237 in order to perform a conversion. There have been times where I've tried doing it out of the specified order, only to realize doing it in the order listed solves the problem...

Next, ensure your acquisition time is being met properly, my bet is that it isn't. Read page 238 for more information...Table 20-1 would suggest you are outside the operating parameters.
No its not, tried all osc frequencies, different Tad times all with same result, not working,

As for page 238 there is no table...
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,395
FIXED IT LADS !!!

i was setting the wrong bits in ADCON0, its bits 5-2, and not bits 0-3


DOUBLE Doh!!! to me.......
 

tshuck

Joined Oct 18, 2012
3,534
No its not, tried all osc frequencies, different Tad times all with same result, not working,

As for page 238 there is no table...
I didn't say there was a table on page 238, just that you should read that. The table I referred to, table 20-1, is on page 239.
FIXED IT LADS !!!

i was setting the wrong bits in ADCON0, its bits 5-2, and not bits 0-3


DOUBLE Doh!!! to me.......
I wasn't sure why you had listed so many bits to indicate the channel, I didn't realize you were assigning that to the register directly, essentially turning the ADC on and off...it happens...:)
 
Top