PDA

View Full Version : 16F819 config for A/D



enigma
- 27th January 2016, 12:12
Hi All
Not used this PIC before, trying to read a voltage on AN0 pin17 but get the same result on my serial out for any input, 5235. I suspect I`ve either miss configured a register or missed one altogether. My test code is as follows, anything obvious?. Running 8mHz, a LED flashes OK at correct intervals as far as I can tell.
Cheers Pete

CODE:

#CONFIG
__config _INTRC_IO & _WDT_OFF & _PWRTE_OFF & _MCLR_ON & _BODEN_ON & _LVP_OFF & _WRT_ENABLE_1536 & _DEBUG_OFF & _CP_ALL

#ENDCONFIG

'Set PIC register defaults

OPTION_REG.7=0 ' enable pull-ups on port B

OSCCON = %01110000 ' Internal 8 MHz osc

TRISA = %00000011 'set RA0/1 to inputs, all others to outputs

TRISB = %00001111 'out,out,out,out,in,in,in,in

ADCON1 = %11001110 'Use RA0 for Analogue input Right Justified

DEFINE OSC 8


'Define ADCIN Parameters
Define ADC_BITS 10
Define ADC_CLOCK 1
Define ADC_SAMPLEUS 50

'PortA.2 is serial debug
extdataOut var porta.2


'Define variables
InputVoltage var word




test:
ADCIN 0,InputVoltage 'Get value

serout2 extdataOut,16468,["Debug ",dec4 InputVoltage,13]

pause 1000

goto test

peterdeco1
- 27th January 2016, 18:52
Hi. I've used this routine many times without a problem Try it.

OSCCON = $70 '8mhz
adcon1 = 7 'set inputs to all digital - adcin command will convert individual input to analog
@ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON
'declare bit, byte, word variables here
TRISA = %00000011 'set RA0/1 to inputs, all others to outputs
TRISB = %00001111 'out,out,out,out,in,in,in,in
Clear 'RESET ALL VARIABLES UPON POWERUP
Pause 100 'SETTLE DOWN
'ADCON1.7 = 1 'RIGHT JUSTIFY RESULT on word variables - not needed for bytes

start:
ADCIN 0,(your variable)

enigma
- 29th January 2016, 10:00
Thanks! That's got it up and running, Cheers Pete