PDA

View Full Version : ADCIN on 18F8722



nobner
- 26th May 2011, 20:45
Hello together,
i want to use the AD-Converter on the 18F8722 for the first time.
I want to use the AN0 and the AN1 channel.
So I looked in the Data-Sheet and wrote this out:

DEFINE ADC_BITS 10 'trying with 10 bits resolution
DEFINE ADC_CLOCK 3 'Is this clock the same as my system clock (external
'8MHz crystal)?
DEFINE ADC_SAMPLEUS 50 'Took it from the manual, is it correct?

TRISA=%00001111 'Setting RA0 to RA3 to inputs
ADCON0=%00000111 'using AN0 and AN1
ADCON1=%00001101 'using supply voltage as vref, setting AN0 and AN1 to
'analog inputs
ADCON2.7=1 'result is right justified

...and then the ADCIN 0, variable....?

Do I have to do other settings for ADCON2, when i'm using ADCIN?
Are these settings above correct, or is something missing?

Thanks in advance!!

Charles Linquis
- 27th May 2011, 02:39
This is "lifted" from one of my programs.
Note that I use a high-accuracy reference voltage on RA3 and I run at 40Mhz.
If you are running at 20Mhz, you can set the clock divisor to /32


ADCON0 = %00000001
ADCON1 = %00010011 ;Set Vref on RA3, 11 readable chan A/D (AN0-AN11)
ADCON2 = %10011110 ; Right justify clk/64

;-----------------------------------------------------------------------
Chan = 1 ; or whatever channel you want to read
AVGVAR = 12 ; The number of samples you want to take (for averaging and scaling)

Gosub DoADC

;Result in ADCStor

;----------------------------------------------------------------------


DOADC:
ADCSTOR = 0
For X = 1 to AVGVAR
ADCIN Chan,ADCVAR
ADCSTOR = ADCSTOR + ADCVAR
PAUSEUS 20
Next X
Return

HankMcSpank
- 27th May 2011, 21:09
Hello together,
i want to use the AD-Converter on the 18F8722 for the first time.
I want to use the AN0 and the AN1 channel.
So I looked in the Data-Sheet and wrote this out:

DEFINE ADC_BITS 10 'trying with 10 bits resolution
DEFINE ADC_CLOCK 3 'Is this clock the same as my system clock (external
'8MHz crystal)?
DEFINE ADC_SAMPLEUS 50 'Took it from the manual, is it correct?

TRISA=%00001111 'Setting RA0 to RA3 to inputs
ADCON0=%00000111 'using AN0 and AN1
ADCON1=%00001101 'using supply voltage as vref, setting AN0 and AN1 to
'analog inputs
ADCON2.7=1 'result is right justified

...and then the ADCIN 0, variable....?

Do I have to do other settings for ADCON2, when i'm using ADCIN?
Are these settings above correct, or is something missing?

Thanks in advance!!

I could be wrong (if so, someone please correct me!), but the adcon0 register can be disregarded if using the picbasic adcin command...it sets the adcon0 register 'behind the scenes' for you (this area confused the hell outta me when I came to try & suss it). FWIW, your extract here...

ADCON0=%00000111 'using AN0 and AN1

wouldn't be as you've outlined, bit 0 turns the ADC module on, bit 1 is the ADC conversion status bit, bit 3 selects AN0. So to select AN0 would be

ADCON0=%00000101

selecting AN1 would be...

ADCON0=%000001001


...and so on - ie you can only have on ADC channel selected at any one time....but the ADCIN command sorts the channel selection for you.

Charles Linquis
- 28th May 2011, 14:12
For all that are using an 8722 -

I found a few strange characteristics of that device that I could never explain. I switched to 8723's and all is well. The 8723's have a 12 bit A/D as well.

nobner
- 29th May 2011, 17:35
Thanks to you all!
So I try my first AD-prog this week,
and i think i can get it work thanks to your help!
Best regards
Norbert