ADCIN and PIC18F4331 Arghhhhh !
Hi, I'm puzzled
I had no problem with the following code until I changed PIC from 18F4620 to 18F4331. Seem's like it always read ADC channel 0
Tried many variations of the defines but basically this is what I'm using:
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 50
DEFINE ADC_CLOCK 3
ADCON2.7 = 1 'Right justified A/D
ANSEL0 = %11000011 'ADC pins: AN0,AN1,AN6,AN7
ADCON0 = %00000011
ADCHS = %01000100
then I have the four subroutines called by the program:
GetDure:
adcin 1, duration
duration = (duration*2) + 70
return
GetDpth: 'Return Depth
adcin 7, DPTH
dpth = 1024-dpth
DPTH = (dpth*5)/21
RETURN
GetStrk: 'Return Stroke length
adcin 6, STRK
STRK = (STRK*20)/51 '0 to 401
STRK = STRK + 80 '80 to 480 (1 to 6 inches)
RETURN
GetSPD: 'Return speed
adcin 0, PulseLen
PulseLen = ((1026-PulseLen)*4) + 35 '35 to 4100
return
The program compile fine and worked very well before with PIC18F4620, (using different define to match PIC). Now, It looks like it always read channel 0, for all 4 subroutines. It doesn't care about the value for the channel, after the ADCIN. What I'm missing ???
J-P
Incredible, could someone explain this ?
Well, here is what is working:
Code:
DEFINE OSC 16
TRISB = %00000000
'----- Set ADC ---------------------------------------------
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 2
ADCON1 = %00000111 'ADC on channel AN0 to AN7
ADCON2.7 = 1 'Right justified A/D
'----- Set LCD ---------------------------------------------
DEFINE LCD_LINES 4
define LCD_DREG PORTD
DEFINE LCD_RSREG PORTC
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTC
DEFINE LCD_EBIT 2
line_1 con $80
line_2 con $C0
line_3 con $94
line_4 con $D4
PAUSE 1000
LCDOUT $FE, 1
A0 VAR WORD
A1 VAR WORD
A2 VAR WORD
A3 VAR WORD
A4 VAR WORD
main:
ADCIN 0, A0
ADCIN 1, A1
ADCIN 2, A2
ADCIN 3, A3
lcdOUT $FE,1, LINE_1, #A0
lcdOUT $FE, LINE_2, #A1
lcdOUT $FE, LINE_3, #A2
lcdOUT $FE, LINE_4, #A3
pause 100
goto main
end
BUT !!!!!!!!! this is NOT working
Code:
DEFINE OSC 16
TRISB = %00000000
'----- Set ADC ---------------------------------------------
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 2
ADCON1 = %00000111 'ADC on channel AN0 to AN7
ADCON2.7 = 1 'Right justified A/D
'----- Set LCD ---------------------------------------------
DEFINE LCD_LINES 4
define LCD_DREG PORTD
DEFINE LCD_RSREG PORTC
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTC
DEFINE LCD_EBIT 2
line_1 con $80
line_2 con $C0
line_3 con $94
line_4 con $D4
PAUSE 1000
LCDOUT $FE, 1
A0 VAR WORD
A1 VAR WORD
A2 VAR WORD
A3 VAR WORD
A4 VAR WORD
main:
ADCIN 0, A0
ADCIN 1, A1
ADCIN 6, A2
ADCIN 7, A3
lcdOUT $FE,1, LINE_1, #A0
lcdOUT $FE, LINE_2, #A1
lcdOUT $FE, LINE_3, #A2
lcdOUT $FE, LINE_4, #A3
pause 100
goto main
end
The only difference is that I'm using channel 1,2,6 and 6 instead of 1,2,3,4
?????????????????????