read two input pins

Discussion in 'Embedded Systems and Microcontrollers' started by chassler53852, Jan 22, 2013.

  1. chassler53852

    Thread Starter New Member

    Jan 22, 2013
    10
    0
    I need help I want to read input pontimeter on pin RA0
    and pin RA1 which a temp. sensor is attached

    from reading the book the code is as follows to read RA0
    TRISA = 0xFF // all port a i/o inputs

    ANSEL =1
    ADCON0 = 0b00000001 //channel an0/ra0 selected

    int x=0;
    GODONE = 1;
    while (GODONE ==1)
    {
    }
    x=ADRESH

    so this is basic for reading value of port ra0 to value X

    so How do I read value of RA1 ??
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    1.) use CODE tags( button looking like '#')
    2.) It would seem you are using a PIC microcontroller, state that
    3.) What PIC microcontroller are you using? (16f877A, 18F4321, etc.)
    4.) Look in the datasheet for the section on ADC you need to set the ADC input to the corresponding pin(ADCON0), then run the ADC(GO = 1) and wait for DONE/ ADC interrupt, then read ADRES.
    5.) Reading ADRESH only gives you the most significant 8 bits, provided it is left justified...
     
  3. chassler53852

    Thread Starter New Member

    Jan 22, 2013
    10
    0
    but whats the syntax to read port RA1
    and does the ADRESH read all ports?
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    No, ADRES only contains the result of the last analog-to-digital conversion. A PIC only has one ADC, and as such, the inputs are multiplexed together so as to allow the microcontroller to read multiple analog lines. If you look at the datasheet under the ADC section, you'll find that you need to modify ADCON0 in order to read AN1.

    Basically, you'll start the conversion on AN0, read ADRES for AN0 results, store the data, change ADCON0 to read AN1, perform the conversion, then read ADRES in order to get the results for AN1.
     
    absf likes this.
  5. chassler53852

    Thread Starter New Member

    Jan 22, 2013
    10
    0
    youve been a great help this ansers my questions then
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I'm glad that helped!

    As a bit of advice, you can't program a microcontroller without the datasheet. Always keep it handy and consult it first.
     
Loading...