PDA

View Full Version : 16F1826 ADC setup help!



SOTASOTA
- 3rd May 2016, 21:31
Help! I have this code:


;----[16F1826 Hardware Configuration]-------------------------------------------
#IF __PROCESSOR__ = "16F1826"
#DEFINE MCU_FOUND 1
#CONFIG
cfg1 = _FOSC_INTOSC ;INTOSC oscillator: I/O function on CLKIN pin
cfg1&= _WDTE_OFF ; WDT disabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLRE_OFF ; MCLR/VPP pin function is digital input
cfg1&= _CP_OFF ; Program memory code protection is disabled
cfg1&= _CPD_OFF ; Data memory code protection is disabled
cfg1&= _BOREN_OFF ; Brown-out Reset disabled
cfg1&= _CLKOUTEN_OFF ; CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
cfg1&= _IESO_OFF ; Internal/External Switchover mode is disabled
cfg1&= _FCMEN_OFF ; Fail-Safe Clock Monitor is disabled
__CONFIG _CONFIG1, cfg1

cfg2 = _WRT_OFF ; Write protection off
cfg2&= _PLLEN_OFF ; 4x PLL disabled
cfg2&= _STVREN_OFF ; Stack Overflow or Underflow will not cause a Reset
cfg2&= _BORV_HI ; Brown-out Reset Voltage (Vbor), high trip point selected.
cfg2&= _LVP_OFF ; High-voltage on MCLR/VPP must be used for programming
__CONFIG _CONFIG2, cfg2

#ENDCONFIG

#ENDIF

;----[Verify Configs have been specified for Selected Processor]----------------
; Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
#ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF

OSCCON = %01101010 '4 Mhz internal clock, PLL off
DEFINE OSC 4

DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 4
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time

PORTA = 0
PORTB = 0
TRISA = %00000101 'Set A.0 and A.2 as input, all else Outputs
TRISB = %00000000 'All Outputs
ADCON0 =%00001001 'Turn ON ADC
ADCON1 =%11110000 'Right justified, Internal R/C

ANSELA =%00000100 ' AN2 on PortA.2 Analog
ANSELB =%00000000 ' Digital I/O

adval var word

adcin 2, adval

I have not shown the mainloop, but it is essentially reading the ADC with


adcin 2, adval

I cannot get this ADC to work. I need it to read a 0-5V on RA.2.

I am wanting to use the INTERNAL oscillator @ 4Mhz.

I have tried for hours. What am I missing please?
Thanks!

Scampy
- 3rd May 2016, 23:39
What version of PicBasicPro are you using ?

Also, by not including all the code, it's like saying you have all the ingredients but not telling us the recipe and then asking people why you cake won't rise !

I'm no expert, but you seem to be setting up ANSEL and ADCON so it's not as if all ports are digital or something like that is the cause, so it could possibly be something in the main part of the code which ou haven't shown.

SOTASOTA
- 4th May 2016, 21:35
Thanks for your response Scampy!
I was just wanting to use simple code to get the ADC to work. With the help of Richard's code (from the MELABS forum) I was able to get the ADC to work. Thanks to your help I re-looked at the rest of my code, and I found the issue. I had set PortA.2 to an output later on (by mistake!).

So, all is working PERFECTLY now!!!!

Thanks again!!
Russ :-)