ADC reading and configuration problem


Closed Thread
Results 1 to 5 of 5
  1. #1

    Question ADC reading and configuration problem

    Hi everyone,

    I can't get my ADCs to working properly. I've tried to play with the registers, with no results.

    I have to sample 4 analog inputs.

    Only one reading is correct, it's the "tsense" refered to the ADCIN5. The others values displayed are completely crazy.

    I'm using the PIC18F4431 ( datasheet : http://ww1.microchip.com/downloads/e...Doc/39616d.pdf )

    The simple code used for the test :
    Code:
    DEFINE OSC 32      
    DEFINE LCD_DREG PORTC
    DEFINE LCD_EREG PORTD
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_EBIT 0
    DEFINE LCD_RSBIT 1
    DEFINE ADC_BITS 10
    
    TRISA=%11111111
    TRISB=%11000000   
    TRISC=%110000     
    TRISD=%11100      
    TRISE=%11111111
    
    ANSEL0=%11111111
    ADCON0=%00011111
    ADCHS=%01010101
    
    potsense var word
    tsense var word
    usense var word
    isense var word
    
    pause 2000
    LCDOUT $fe,1 
    
    lp:
    
    ADCIN 4,potsense
    ADCIN 5,tsense
    ADCIN 6,usense
    ADCIN 7,isense
    
    lcdout $fe,$2,DEC5 potsense," ",DEC5 tsense
    lcdout $fe,$c0,DEC5 usense," ",DEC5 isense
    
    pause 10
    
    goto lp
    Last edited by pxidr84; - 25th March 2011 at 18:17.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: ADC reading and configuration problem

    Hello,

    I had the same problem a while back. The adcin command does not work with the
    pic18fxx31s. You have to read them manually. See this thread it may help.

    http://www.picbasic.co.uk/forum/show...ght=PIC18F2331

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: ADC reading and configuration problem

    Quote Originally Posted by mark_s View Post
    Hello,

    I had the same problem a while back. The adcin command does not work with the
    pic18fxx31s. You have to read them manually. See this thread it may help.

    http://www.picbasic.co.uk/forum/show...ght=PIC18F2331
    Hi,

    I've tried this (from Bruce's code) :

    Code:
    DEFINE OSC 32      
    DEFINE LCD_DREG PORTC
    DEFINE LCD_EREG PORTD
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_EBIT 0
    DEFINE LCD_RSBIT 1
    DEFINE ADC_BITS 10
       
      CH1 VAR WORD
        CH2 VAR WORD
        CH3 VAR WORD
        CH4 VAR WORD
        
        ' Analog setup
        ' Single shot, multi-channel, simultaneous Mode2, Groups A+B, then C+D
        ADCON0 = %00011101
        
        ' Set +/- Vref to AVdd/AVss, FIFO buffer enabled
        ADCON1 = %00010000
        
        ADCON2 = %11001111   ' Right justified, 24 Tad, Frc
        ADCON3 = 0           ' Disable all triggers
        ADCHS = 0            ' Channel select for AN0,1,2,3
        ANSEL0 = %00001111   ' AN0,1,2,3 analog input, rest digital
     
    Main:
        ADCON0.1 = 1         ' Start the conversion
        WHILE ADCON0.1=1     ' Wait for it to complete (all 4 channels)
        wend
        
        ' FIFO buffer holds all 4 channels. Reading ADRESH & ADRESL automatically
        ' increments the buffer pointer (on read of ADRESL) to the next channels
        ' storage location.
        CH1.HighByte = ADRESH ' get result from AN0
        CH1.LowByte = ADRESL  ' Increment buffer pointer
        
        CH2.HighByte = ADRESH ' get result from AN1
        CH2.LowByte = ADRESL  ' Increment buffer pointer
        
        CH3.HighByte = ADRESH ' get result fron AN2
        CH3.LowByte = ADRESL  ' Increment buffer pointer
        
        CH4.HighByte = ADRESH ' get result from AN3
        CH4.LowByte = ADRESL  ' clears buffer pointer
      
       
        ' show 4 A/D channel results
        lcdout $fe,$2,DEC5 CH1," ",dec5 CH2
        lcdout $fe,$c0,DEC5 CH3," ",dec5 CH4
       
        pause 100
     
        GOTO Main
    The readings works, but the lower bits of the reading are unstable, and I don't know why.
    Last edited by pxidr84; - 27th March 2011 at 13:24.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: ADC reading and configuration problem

    What is the source of the analog? Do you have smoothing capacitors from the ADC pins to VSS? Sometimes that helps.
    Dave
    Always wear safety glasses while programming.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: ADC reading and configuration problem

    Quote Originally Posted by mackrackit View Post
    What is the source of the analog? Do you have smoothing capacitors from the ADC pins to VSS? Sometimes that helps.
    In fact I've resolved my problem. The result of the ADC was configured to be right-justified (ADCON2.7=1), I've set it to be left-justified (ADCON2.7=0), and I haven't problems anymore (the readings are perfectly stable now).

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts