• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC ADC analogue voltage reading

Status
Not open for further replies.

richacm

New Member
Hi just realised I should have posted this in the Micro controller forums.

Anyway here it is...

I am just writing some PIC code that will read a 0V-5V analogue value from its AN0 pin. The question I have is what value does the reading relate to in the code? E.g. 0V = 0 but is 5V = 255 and 1V = 51 (255/5)? Or is there some other values for each of these?

Thanks in advance,

Craig
 

futz

Active Member
I am just writing some PIC code that will read a 0V-5V analogue value from its AN0 pin. The question I have is what value does the reading relate to in the code? E.g. 0V = 0 but is 5V = 255 and 1V = 51 (255/5)? Or is there some other values for each of these?
For an 8-bit A/D value, 5V would be 255 and 0V would be 0.
For a 10-bit A/D value, 5V would be 1023 and 0V would be 0.

Here's an easy calculation for it:
For 8-bit: ADRESH Value = (MeasuredVoltage/VREF)*256
For 8-bit: ADRESH/ADRESL Value = (MeasuredVoltage/VREF)*1024

VREF is your voltage reference, which is typically the PIC's internal 5V taken from VDD if you're not supplying your own.
 

futz

Active Member
Here's an easy calculation for it:
For 8-bit: ADRESH Value = (MeasuredVoltage/VREF)*256
For 8-bit: ADRESH/ADRESL Value = (MeasuredVoltage/VREF)*1024
Oops! Second line should be 10-bit.

If you're using a 10-bit A/D such as what many PICs have, but you want an 8-bit result, just set it to left justify, read ADRESH and ignore ADRESL.

If you want a 10-bit result, set it to right justify and read both ADRESH and ADRESL.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top