PDA

View Full Version : ADC value with 2 decimals on an LCD



Squibcakes
- 1st December 2005, 03:32
OK, I need some answers to my ADC questions. I’m a ADC first timer here so bear with me.

I have a16F870 and I want to display an analog voltage (0.00-5.00V) on an LCD display. Presently I can:

1. Display the ADC voltage as a number between 0 – 255.
2. Display the ADC voltage as a number between 0 - 5 (1 volt resolution)

Here is an example of my code:

RESULT VAR BYTE

ADCIN 0, RESULT
LCDOUT $FE, 1
LCDOUT "ADC VALUE:"
LCDOUT $FE, $C0
LCDOUT DEC RESULT ‘ displays number 0 - 255
PAUSE 2000

RESULT = RESULT / 51
LCDOUT $FE, 1
LCDOUT "VOLTAGE VALUE:"
LCDOUT $FE, $C0
LCDOUT DEC RESULT ‘ displays number 0 – 5

What I really want to do is display the ADC voltage as a number between 0.00 – 5.00 with TWO decimal digits.

To display RESULT with two decimals, can I just multiply RESULT by 100, and use the DIG command to display three discreet digits, and the decimal point????????????

I tried this, but the display shows values between 0 – 2.55V. Strangely, it gives these readings twice when adjusting my 5k pot from off to full scale!
i.e. (off) 0 to 2.55 (half way) then 0 to 2.55 (full scale)

Maybe I need to use trickier math or higher ADC resolution????????????

This brings me to my next question.

The above example uses DEFINE ADC_BITS 8. I am guessing this is why my RESULT value is sampled over 255 steps.

I have tried using ADC_BITS 10, to display RESULT in the range of 0 to 1023, but this doesn’t work using the above code. Any ideas????????????

Sorry for the long winded post, (I know I dread reading long posts too), but I hope I have given you brainiacs enough clues to solve my dilemma.

Cheers

Squibcakes
- 1st December 2005, 03:47
Ok, I would be the first to admit, I'm a bit of a goose!

For those playing at home, my RESULT var should be WORD size, not Byte Size! This fixes up the problem!

Still learning!

Doh!

CocaColaKid
- 2nd December 2005, 12:50
I use:


DEFINE ADC_BITS 10 ' Set resolution of conversion
DEFINE ADC_CLOCK 8 ' Set clock source (x/FOSC or FRC)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time (in uS)


However as I recall you have to switch the ADRESH/ADRESL to right justification for 10-bit and left justification for 8-bit.

CocaColaKid
- 2nd December 2005, 15:54
You can use the formula:



volts = 50000 / 255 * A/D Value
if volts DIG 1 => 5 then
volts = volts + 100
endif
volts = volts / 100
debug cmd,line3,dec1 volts/100,".",dec2 volts//100


At say 2.5V the display will read 2.49 and the variable "volts" will hold 249.