I'm using the picdem 18F4550 with microchip v8.63 with the C18 compiler.
I will enable PortA to set as input, I will connect a LDR on port RA0. Which is as following (I think):
TRISAbits.TRISA0 = 1; //<= set RA0 as input.
Now I want the value of the LDR (voltage/value if a led is on), can I say:
int colorLed = PortAbits.RA0;
And now in the variable of type int there is the value/voltage of my Led.
Correct me if I'm wrong.
Best Answer
Firstly, lets check that you have connected your LDR up correctly, it should be something like this...
To read the value of PIN RA0/AN0, you need to do some initialisation to make sure the port is setup correctly. The datasheet explains how all this works, but these values should work:
Now the port should be set up, you can now read the LDR value:
You can now read the result of the LDR as a 10-bit value in
ADRESH:ADRESL
. If you only need 8-bit resolution, then setADCON2.ADFM = 0
for left justification of the result, then you only need to read theADRESH
to get your result.