read two input pins

Thread Starter

chassler53852

Joined Jan 22, 2013
10
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 ??
 

tshuck

Joined Oct 18, 2012
3,534
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 ??
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...
 

tshuck

Joined Oct 18, 2012
3,534
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...
but whats the syntax to read port RA1
and does the ADRESH read all ports?
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.
 
Top