1
votes

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.

2
Cross-posting is discouraged. (electronics.stackexchange.com/questions/12176/…)Andy

2 Answers

6
votes

It sounds like you want to measure an analogue voltage, in which case you will need to use the pin as AN0, rather than RA0. You should read section 21 of the datasheet, but in summary, you will need to configure the A-D converter using registers ADCON0, ADCON1 and ADCON2, and read the result that's present on ADRESH:ADRESL.

Note that you could potentially read the LDR using a digital input, but you will be unable to calibrate it. Using the analogue input is slightly more complicated, but much more flexible.

0
votes

PORTA can function as a digital input or analog input, by default.

To use an LDR you most likely need to to configure the PIN as analog input.

HOwever it still depends on what you want to do. Maybe you should give a bit mroe details as to your project.

check this link for the PIC18F4550 datasheet

You can only find more detailed resources on the PIC184550 here

again, you might want to give more details on your application so we can help you better.