12F683 ADCIN - both channels the same


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

    Default 12F683 ADCIN - both channels the same

    I'm having a problem with reading two analog inputs. The first channel (GPIO.0) works well but the issue is that the next channel reads identically even though the voltage going into the pin is clearly different.

    I must be missing something simple, I think that what I am trying to do is quite basic. I've looked at the PicBasic Pro manual, the datasheet and searched the forum. I did see that someone had a similar problem but the solution was not effective for me.

    The wiring has been checked over many times, I am confident that the error is in the code.

    The intention is to read the two axis and the button of a mini joystick and send the data serially to another uC.

    Code:
    @ DEVICE PIC12F683, MCLR_OFF
    @ DEVICE PIC12F683, INTRC_OSC_NOCLKOUT
    @ DEVICE PIC12F683, WDT_OFF
    @ DEVICE PIC12F683, BOD_ON
    @ DEVICE PIC12F683, PWRT_ON
    '-------------------------------------------------------------------------------
    DEFINE  OSC 8
    Define	ADC_BITS	8	' Set number of bits in result
    Define	ADC_CLOCK	3	' Set clock source (3=rc)
    Define	ADC_SAMPLEUS	50	' Set sampling time in uS
    define  CHAR_PACING 1000
    
    OSCCON = %01110001     ' internal oscillator at 4 MHZ
                 
    CMCON0 = 0             ' Comparators off
    ANSEL  = %00000011	   ' Set 0, 1, analog
    WPU    = 0             ' Internal pull-ups = off
    GPIO   = %00000000     ' All outputs = 0 on boot
    TRISIO = %00011111     ' GPIO.0,1,2,3,4 input, GPIO.5 output
    
    joyY         var   GPIO.0
    joyX         var   GPIO.1
    joyBtn       var   GPIO.2
    Sout         var   GPIO.5
    
    AD      var     byte
    X       var     byte
    Y       var     byte
    Btn     var     byte
    B       var     bit
    
    Main: 
    
    adcin joyX, AD
    
    X = AD
    AD = 0
    
    pause 1
    
    adcin joyY, AD
    
    Y = AD
    AD = 0 
    
    B = joyBtn
    if B  then 
    Btn = Btn + 1  
    else
    Btn = 0
    endif
    
    if Btn > 100 then Btn = 100   
    
    serout Sout, 2, ["cmd", X, Y, Btn, 128, 128]
    goto Main     
         
    end
    Rich H

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yet another one where the PBP manual explain it all, but no one seems to bother reading Just kidding

    ADCIN ADCHannel, Var

    ADCHannel must be a decimal value, 0,1,2,3,4,5 etc... you use the ADC channel number, NOT the port PIN name.

    ADCIN 0, Var
    ADCIN 1, Var2
    plah plah plah
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Ahh, I just knew it was going to be something simple.

    Thanks!

    Rich H

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,617


    Did you find this post helpful? Yes | No

    Default

    Hi, Rich

    The way to give a decent name to ADC inputs ... :

    .
    .
    .
    GPIO = %00000000 ' All outputs = 0 on boot
    TRISIO = %00011111 ' GPIO.0,1,2,3,4 input, GPIO.5 output

    joyY CON 0 ' ADC pin number must be set as a CONstant ...
    joyX CON 1

    joyBtn var GPIO.2
    Sout var GPIO.5

    AD var byte
    X var byte

    .
    .
    .

    Main:

    adcin joyX, AD

    X = AD
    AD = 0

    pause 1

    adcin joyY, AD

    Y = AD

    .
    .
    .

    Alain
    Last edited by Acetronics2; - 7th January 2011 at 08:07.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Members who have read this thread : 0

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

Tags for this Thread

Posting Permissions

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