PDA

View Full Version : Need some help with ADC readings on 16F72



Megahertz
- 28th August 2011, 10:00
My code is as follows:

Include "modedefs.bas"
DEFINE OSC 20 ' OSCCON defaults to 4MHz on reset
DEFINE ADC_BITS 8 ' Set number of bits in result (8, 10 or 12)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

;-------------CONFIGURATION FUSES SET HERE-------------------------
@ __Config _RC_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON ; & _CP_ALL

DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

PAUSE 100
;------------REGISTERS SET HERE------------------------
ADCON1=%00000100
ADCON0=%1100000
PORTA=0
PORTB=0
PORTC=0
TRISA = %000001
TRISC = 0
TRISB = 0
OPTION_REG = %10001111 ' RAPU = off, PS 1:256 to TMR0
PORTA=0
PORTC=0
PORTB=0

B0 Var Word
gosub CLER
start var Adcon0.0
done var Adcon0.2

Main:
Start=1
while done=1 : pause 1 : wend
b0=ADRES
LCDOUT $FE,2," B0=",dec3 B0 : pause 500
GOSUB CLER
goto Main

cler:
LCDOUT $FE,1
pause 10
LCDOUT $FE,2
PAUSE 10
LCDOUT $FE,$0C
pause 100

return
I am trying to take adc reading of a POT, but the result is constantly "B0=010" on my LCD. Could someone help me identify the problem please?

Bruce
- 28th August 2011, 10:57
Change ADCON0=%1100000 to ADCON0=%11000000, and _RC_OSC to _HS_OSC.

Also, you don't need a word variable for an 8-bit result.

Megahertz
- 28th August 2011, 20:15
Thanks Bruce. Please clear my 1 doubt which I always had but I never asked, but now since I cannot figure out the difference myself, here it goes:

Q) What it the difference between ADCON0 registers BIT 5-3 & ADCON1 BIT 2-0?

mister_e
- 28th August 2011, 20:28
Datasheet is quite self explanatory

ADCON0 bit 5-3 CHS<2:0> Analog Channel Select Bits
This will select the Analog Channel (I/O) to read (ADCIN 0, Var , ADCIN 1, Var)

ADCON1 Bit 2-0 PCFG<2:0> A/D Port Configuration Control Bits
This configure the I/O... Digital OR Analog