PDA

View Full Version : ADC setup on 12F1822 ??



swr999
- 25th October 2014, 03:24
Hello!

Has anyone been successful on getting data from the ADC on a 12F1822? While I can get 8-bit conversions, I cannot get 10-bit results. I have puzzled through the related registers from reading the data sheet. Briefly, I find that setting ADCON1.7 stops the ADC from working. Clearing ADCON1.7 lets it work in 8-bit mode.

Reading the errata sheet they show problems with the ADC, and a possible workaround, but I'm not convinced that is my problem since the problem also occurs on a 12F1840.

I can post example code, but initially I just wanted to know if someone on the forum is able to get 10-bit data from a 12F1822, or a 12F1840.

Thanks!

richard
- 25th October 2014, 05:45
works for me

#CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_OFF
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
__CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
__CONFIG _CONFIG2, cfg2
#ENDCONFIG
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
ADCON1=$f0 'rc clk vdd ref right justify
osccon=$6A '4 mhz
anselA=1 'dig i/o
' CMCON0=7 ' compare off
trisA=001

adval var word
main:
pause 5
ADCIN 0, adval
lata=0

if adval > 500 then lata.2= 1 'led1
if adval> 200 then lata.1=1' led2

goto main

swr999
- 25th October 2014, 07:14
Thanks ^^^
I am doing the same register setups as you show, except that I'm using AN2 (ANSELA=%0100, TRISA=%01100). In addition I have ADCON0.0=1 (enable ADC). I'll try removing this. I'll work on this and post my results.
Thanks again!

richard
- 25th October 2014, 07:29
ADCON0.0=1 serves no purpose the ADCIN code will do that for you

HenrikOlsson
- 25th October 2014, 12:17
This may be obvious but since you didn't post your code I'll say it anyway.
The variable you're putting the result in is declared as a WORD, right?

/Henrik.

richard
- 25th October 2014, 23:59
looks like "Occam's Razor" may have struck again

swr999
- 26th October 2014, 05:35
... The variable you're putting the result in is declared as a WORD, right? /Henrik.
Yes, it's a WORD. But thanks!

tekart
- 30th October 2014, 23:51
Not sure if you figured this out, but 10bit AD need to be read out as RIGHT justified, and 8 bit reads should be LEFT justified. Here's a config that works for 8-bit:
ADCON0.0 = 1 ' AD ON
ANSELA = %00000001 ' select analog vs digital (1=analog)
ADCON1.7 = 1 ' bit7 = 1 = LEFT justified
define adc_bits 8 ' set ADC as 8 bit

swr999
- 31st October 2014, 00:06
Thank you^^^. Yes, I got it sorted out. I think my initial confusion resulted from some interaction between PBP3 DEFINEs for the ADC module and some direct settings I was also doing on some ADC-related registers. Everythings good now. Thanks again!