PDA

View Full Version : Help with multiple ADC inputs



wdmagic
- 28th December 2012, 05:46
I'm having some issues turning on more than a single ADC input, Ive been using the code below.

DEFINE ADC_BITS 10 ' A/D number of bits
DEFINE ADC_CLOCK 1 ' Use A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us
RES1 Var Word ' A/D converter result
RES2 Var Word ' A/D converter result
RES3 Var Word ' A/D converter result
AGAIN:
ADCIN 0, Res1 ' Read Channel 0 data
ADCIN 1, Res2 ' Read Channel 1 data
ADCIN 2, Res3 ' Read Channel 2 data
res1 = res1 /256
res2 = res2 /256
res3 = res3 /256
'Display all 3 ADC results on LCD
Pause 100
goto again
end


Now I know im missing some ansel, adcon or something to enable the extra inputs, right now the extra inputs are eratic/off, they dont take readings right.
as long as i stay with ADCIN 0 thers no problem, but I need to take readings on more pins. and am confused on these extra settings, any ideas would be great. also i havent learned to do the fuse settins for freq, brounout, mclre ect, so if possible leave that stuff out of code for right now.

BTW using a 18F4550

mackrackit
- 28th December 2012, 06:38
Try adding

ADCON2.7 = 1
ADCON1 = %00001100
TRISA = %00000111

wdmagic
- 28th December 2012, 21:53
after adding that none of the adc works. let me post the actual code im using not the base code, i will include the code you posted


DEFINE ADC_BITS 10 ' A/D number of bits
DEFINE ADC_CLOCK 1 ' Use A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us
ADCON2.7 = 1
ADCON1 = %00001100
TRISA = %00000111
TRISD = %00000000 ' PORTD is output

LCDOUT $FE, 1 ' Clear LCD
PAUSE 500 ' Wait 0.5sec for LCD to initialize
SETT Var Word ' A/D converter result in 10bit
READT var word
AGAIN:
ADCIN 0, READT ' Read Channel 0 data
ADCIN 1, SETT ' Read Channel 0 data
ReadT = ReadT / 13 'this may change depending on thermostat
SETT = SETT / 1024 'Output = 0 - 63
readT = (((readT / 10) * 9) / 5) + 33 'Convert ANA0 to Farent.
setT = sett + 40 ' Temp Range will be 40 - 103
LCDOUT $FE, 2
LCDOUT $FE, $80
LCDOUT "Set Temp = ", DEC setT , $DF , "F "
LCDOUT $FE, $C0
LCDOUT "Temp = ", DEC READT , $DF , "F "
pause 100
GOTO AGAIN ' Repeat

mackrackit
- 30th December 2012, 04:29
Not sure what is going on with your setup, I do not see the problem.
I dug out a board with an 18F2550 and ran the below code to make sure it works. I only have the ADC on channels 1 and 2 as I have a LED soldered on channel 2. Other than that it should all be the same.



'<FL_PIC18F2550>'
'<FL_PBPL>'
DEFINE OSC 48
#CONFIG
__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _VREGEN_ON_2L
__CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
#ENDCONFIG
CNT VAR BYTE
ADCON2.7 = 1
ADCON1 = %00001101
TRISA = %00000011
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 50
ADC_0 VAR WORD
ADC_1 VAR WORD

START:
CNT = CNT + 1
Serout2 PORTC.6, 16572, ["TEST ",DEC3 CNT, $d, $a]
Serout2 PORTC.6, 16572, ["ADC_0 ",DEC4 ADC_0, $d, $a]
Serout2 PORTC.6, 16572, ["ADC_1 ",DEC4 ADC_1, $d, $a]
TOGGLE PORTA.2
PAUSE 1000
GOSUB GET_ADC
GOTO START

GET_ADC:
ADCIN 0, ADC_0
ADCIN 1, ADC_1
RETURN

wdmagic
- 3rd January 2013, 04:59
I got it to work by leaving off the adcon.7 code, heres what I am using for 2 ADC, it works great, if I change the adcon.7 it wont even compile


DEFINE ADC_BITS 10 ' A/D number of bits
DEFINE ADC_CLOCK 1 ' Use A/D internal RC clock
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us

ADCON1 = %10000000
TRISA = %00000011 'PORTA 0&1 is input

SETT Var Word ' A/D converter result in 10bit
READT var word ' A/D converter result in 10bit

AGAIN:
ADCIN 0, READT ' Read Channel 0 data
ADCIN 1, SETT ' Read Channel 1 data

mackrackit
- 3rd January 2013, 10:59
ADCON.7 will not compile because it is a wrong command. You need to use ADCON2.7=1

wdmagic
- 4th January 2013, 00:27
sorry I mistyped, I did use adcon2.7, ill try to produce the exact error again and post it.