PDA

View Full Version : 16f688 Adc



PICante
- 22nd August 2008, 16:38
Another issue regarding the 16F688.
Setting up the 10-bit ADC I expected a 0 – 1023 range, what I got was 0 to 65472 !!?
Of cause I made some kind of mistake here but nothing that is obvious to me. Also I have a problem with the right justify and getting the decimal point and scaling (Volts) correct. I have tried a few ways to solve it but I’m having trouble to understand the math. Can somebody explain how to do it correctly?

Thanks guys!







OSCCON = %1110111 'Use Internal OSC at 8MHz
CMCON0 = %0000111 'Comparators off
TRISA = %000100 'PortA 0=output- 1=input
TRISC = %000000 'PortC 0=output- 1=input
ANSEL = %0000100 '(Analog Select) 0=Digital 1=Analog

V VAR WORD
L VAR WORD
T VAR WORD

DEFINE ADC_BITS 10 ' 10-bits
DEFINE ADC_CLOCK 3 ' rc
DEFINE ADC_SAMPLEUS 50 ' Sample time uS

ADCON0 = %00000001 ' ADC in operation
ADCON1 = %11001000 ' rightadjust and Vref+

GOSUB movelcd

PAUSE 500

loop:

GOSUB readv

PAUSE 50

GOTO loop

END

'****************** - SUBS - ************************************
MOVELCD:

DEFINE LCD_DREG PORTC 'Data on PORTC PIN 10,9,8,7
DEFINE LCD_DBIT 0 'Start bit for PORTC
DEFINE LCD_RSREG PORTC 'Register Select (RS)
DEFINE LCD_RSBIT 4 'RS bit PORTC PIN 6
DEFINE LCD_EREG PORTC 'Enable Port PORTC
DEFINE LCD_EBIT 5 'E bit PORTC PIN 5
DEFINE LCD_BITS 4 'bitmode (4-bit)
DEFINE LCD_LINES 2 'number of lines (2)

PAUSE 500

RETURN
'************************************************* ***************
readv:

ADCIN 2,V ' take reading

T = V

LCDOUT $FE,2,"ADC ", DEC T," "

lCDOUT $FE,$C0,"DCV = ",DEC (V/100),".",dec2 V," "

RETURN

Darrel Taylor
- 22nd August 2008, 18:34
Setting up the 10-bit ADC I expected a 0 – 1023 range, what I got was 0 to 65472 !!?
That's from the ADFM bit. For 10-bit A/D, use ADCON0.7 = 1 ; right justify

The math depends on what you're reading, but this might help ..
http://www.picbasic.co.uk/forum/showthread.php?p=2010
<br>

PICante
- 22nd August 2008, 21:18
Thanks Darrel!

I changed the; ADCON0 = %00000001 to ADCON0 = %10000001, and it works!
And yes, the link helped me do some math too, only; how can I get two decimals?

Thanks again!

Darrel Taylor
- 22nd August 2008, 21:52
For 2 decimals, multiply by 500 instead of 50.

Then this will display the value with 2 decimal places ...
LCDout $FE,2,"V= ",DEC Volts/100,".",DEC2 Volts//100," Vdc"

PICante
- 23rd August 2008, 10:34
Thank you very much Darrel!