18f2321 AD faulty pic perhaps?

Discussion in 'Embedded Systems and Microcontrollers' started by Dodgydave, Jul 26, 2013.

  1. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,974
    744
    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,
    Code ( (Unknown Language)):
    1.  
    2. Main: setup ports
    3.         movlw  b'01110111'     ; osc ferequency 8mhz
    4.         movwf  OSCCON
    5.         movlw  b'00000001'    ;set digital i/o portA
    6.         movwf  ADCON1         ;set all to digital I/O
    7.         clrf   PORTA
    8.         clrf   LATA
    9.         movlw b'00001111'  ; set porta,0,1 to input, rest outputs
    10.         movwf   TRISA                    
    11.         MOVLW  07h     
    12.         MOVWF  CMCON        ; comparators off
    13.         clrf   PORTB
    14.         clrf   LATB
    15.         clrf   TRISB         ;  ALL B/C PORTS TO OUTPUTS .
    16.         clrf   PORTC         ; PORTA,0 INPUT. A1-7 OUTPUTS.
    17.         clrf   LATC
    18.         clrf   TRISC   
    19.  
    20. ;************************************
    21. ; speed routine
    22.  
    23.     MOVLW b'00000001' ; AN0 source input =1(bit 0)  
    24.     MOVWF ADCON0      ; set file configs
    25.     MOVLW b'00001100' ; set up vref internal, ( Vref bits 4,5,)  AN0-AN2 i/ps
    26.     MOVWF ADCON1      ; set up reg
    27.     MOVLW B'00101011' ; SET CLOCK FREQ FRC, TAD 12bits,LEFT justified bit7=0,
    28.     MOVWF ADCON2      ; do it
    29.     Call Delay                ; wait 50ms                      
    30.     BSF ADCON0,GO    ;Start conversion
    31. wait
    32.     BTFSC ADCON0,GO  ;Is conversion done?
    33.     GOTO wait         ;No, test again
    34.     MOVFF ADRESH,RESULTHI   ; store upper 2 bits in resulthi  
    35.     MOVFF ADRESL,RESULTLO   ; store lower 8 bits in resultlo
    36.    
    37.     return 
    38.  
     
    Last edited: Jul 26, 2013
  2. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    @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).

    Code ( (Unknown Language)):
    1.         'Set Auto or Single Convert Mode
    2.         #IFDEF Bit(ACONV)
    3.             SET ACONV OFF  'Single shot mode
    4.             SET ACSCH OFF  'Single channel CONVERSION
    5.             'GroupA
    6.             IF ADReadPort = 0 OR ADReadPort = 4 OR ADReadPort = 8 Then
    7.                 SET ACMOD1 OFF
    8.                 SET ACMOD0 OFF
    9.             END IF
    10.             'GroupB
    11.             IF ADReadPort = 1 OR ADReadPort = 5 Then
    12.                 SET ACMOD1 OFF
    13.                 SET ACMOD0 ON
    14.             END IF
    15.             'GroupC
    16.             IF ADReadPort = 2 OR ADReadPort = 6 Then
    17.                 SET ACMOD1 ON
    18.                 SET ACMOD0 OFF
    19.             END IF
    20.             'GroupD
    21.             IF ADReadPort = 3 OR ADReadPort = 7 Then
    22.                 SET ACMOD1 ON
    23.                 SET ACMOD0 ON
    24.             END IF
    25.            
    26.         #ENDIF
    27.  
    28.  
    29.         #IFDEF BIT(GASEL0)
    30.             'GROUP A SELECT BITS
    31.             IF ADReadPort = 0 THEN
    32.                 SET GASEL1 OFF
    33.                 SET GASEL0 OFF
    34.             END IF
    35.             IF ADReadPort = 4 THEN
    36.                 SET GASEL1 OFF
    37.                 SET GASEL0 ON
    38.             END IF
    39.             IF ADReadPort = 8 THEN
    40.                 SET GASEL1 ON
    41.                 SET GASEL0 OFF
    42.             END IF
    43.             'GROUP C SELECT BITS        
    44.             IF ADReadPort = 2 THEN
    45.                 SET GCSEL1 OFF
    46.                 SET GCSEL0 OFF
    47.             END IF
    48.             IF ADReadPort = 6 THEN
    49.                 SET GCSEL1 OFF
    50.                 SET GCSEL0 ON
    51.             END IF
    52.             'GROUP B SELECT BITS
    53.             IF ADReadPort = 1 THEN
    54.                 SET GBSEL1 OFF
    55.                 SET GBSEL0 OFF
    56.             END IF
    57.             IF ADReadPort = 5 THEN
    58.                 SET GBSEL1 OFF
    59.                 SET GBSEL0 ON
    60.             END IF
    61.             'GROUP D SELECT BITS
    62.             IF ADReadPort = 3 THEN
    63.                 SET GDSEL1 OFF
    64.                 SET GDSEL0 OFF
    65.             END IF
    66.             IF ADReadPort = 7 THEN
    67.                 SET GDSEL1 OFF
    68.                 SET GDSEL0 ON
    69.             END IF
     
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,724
    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.
     
  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,724
    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).
     
    Dodgydave likes this.
  5. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    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.
     
  6. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,974
    744
    Yeh i know i put the wrong register in the title, but it still wont work.
     
  7. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,974
    744

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

    Active Member

    Jun 2, 2010
    280
    35
    My sincere apologies, I guess I woke up cross eyed this morning:rolleyes:, happens more and more as time goes on.
     
    Dodgydave likes this.
  9. bance

    Member

    Aug 11, 2012
    315
    34
    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.
     
    Dodgydave likes this.
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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.
     
  11. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,974
    744
    No its not, tried all osc frequencies, different Tad times all with same result, not working,

    As for page 238 there is no table...
     
  12. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,974
    744
    FIXED IT LADS !!!

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


    DOUBLE Doh!!! to me.......
     
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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.
    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...:)
     
Loading...