I'm having some trouble with the analog inputs on a 16F886. Code is shown below. Most of it is commented out right now, so it's just an initial LED flash, analog in, heartbeat routine. I want to use AN0(RA0), AN1(RA1) and AN4(RA5) as analog inputs. If I comment out all of the ADCIN instructions, the LED flashes away as it should. If I uncomment any of the ADCINs, the heartbeat LED just stays on. It seems I'm doing something wrong with either the ADC configuration or the actual ADCIN command, but what? I had something very similar to this working fine on a 16f690, but changing over to the 16F886 has caused me some problems. This is new for me, so any comments/suggestions on programming style, etc. would be welcome. Thanks.
BrianB
define OSC 4
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
'CM1CON0 = 0 ' Comparators off (Do I need these?)
'CM2CON0 = 0 ' Comparators off
ANSEL = %00010011' Set analog pins
ANSELH = %00000000
TRISA = %00100011' Set port A
TRISB = %11000000 ' Set port B
TRISC = %00000000 ' Set port C
ANIN0 var WORD 'Cylinder 1 Feedback Pot
ANIN1 var WORD 'Cylinder 2 Feedback Pot
SW1EXT var bit 'Extend Switch
SW2RET var Bit 'Retract Switch
EXT1FLAG VAR Bit
RET1FLAG VAR Bit
EXT2FLAG var bit
RET2FLAG var bit
J var WORD
'Initialize Variables and Ports
PORTA=0
PORTB=0
PORTC=0
EXt1flag=0
ext2flag=0
ret1flag=0
ret2flag=0
for j=1 to 25 'Startup indicator
toggle PortA.2
pause 50
next j
MAIN:
FOR j=1 TO 500
Adcin 4, ANIN0
'Poll Switches
'If PortB.7=0 then
' pause 20
' if PORTB.7=0 then
' SW1EXT=1
' endif
'Else
' SW1EXT=0
'ENDIF
'If PortB.6=0 then
' pause 20
' if PORTB.6=0 then
' SW2RET=1
' endif
'else
' SW2RET=0
'endif
'adcin 0, ANIN0 'Cylinder 1 Pot
'IF ANIN0<245 then
' EXT1FLAG=0 'OK to Extend
'Else
' EXT1FLAG=1
'endif
'IF ANIN0>10 then
' RET1FLAG=0 'OK to Retract
'else
' RET1FLAG=1
'ENdif
'ADCIN 1, ANIN1 'Cylinder 2 Pot
'IF ANIN1<245 then
' EXT2FLAG=0 'OK to Extend
'Else
' EXT2FLAG=1
'endif
'IF ANIN1>10 then
' RET2FLAG=0 'OK to Retract
'else
' RET2FLAG=1
'ENdif
'IF (SW1EXT=1) and (EXT1FLAG=0) then 'Extend Cylinder 1
' PORTB.3=1
' PORTB.4=0
' PORTB.5=1
'endif
'IF (SW1EXT=1) and (EXT1FLAG=1) then 'Stop Cylinder 1
' PORTB.3=0
' PORTB.4=0
' PORTB.5=0
'ENDIF
'IF (SW2RET=1) and (RET1FLAG=0) then 'Retract Cylinder 1
' PORTB.3=0
' PORTB.4=1
' PORTB.5=1
'endif
'IF (SW2RET=1) AND (RET1FLAG=1) then 'Stop Cylinder 1
' PORTB.3=0
' PORTB.4=0
' PORTB.5=0
'endif
'IF (SW1EXT=1) and (EXT2FLAG=0) then 'Extend Cylinder 2
' PORTB.0=1
' PORTB.1=0
' PORTB.2=1
'endif
'IF (SW1EXT=1) and (EXT2FLAG=1) then 'Stop Cylinder 2
' PORTB.0=0
' PORTB.1=0
' PORTB.2=0
'ENDIF
'IF (SW2RET=1) and (RET2FLAG=0) then 'Retract Cylinder 2
' PORTB.0=0
' PORTB.1=1
' PORTB.2=1
'endif
'IF (SW2RET=1) AND (RET2FLAG=1) then 'Stop Cylinder 2
' PORTB.0=0
' PORTB.1=0
' PORTB.2=0
'endif
'if (SW1EXT=0) and (SW2RET=0) then 'Stop Cylinders
' PORTB.3=0
' PORTB.4=0
' PORTB.5=0
' PORTB.0=0
' PORTB.1=0
' PORTB.2=0
'endif
'if SW1EXT=1 and SW2RET=1 then 'Stop Cylinders
' PORTB.3=0
' PORTB.4=0
' PORTB.5=0
' PORTB.0=0
' PORTB.1=0
' PORTB.2=0
'endif
'if (ANIN0>ANIN1) AND (SW1EXT=0) AND (SW2RET=0) THEN
' GOSUB SLAVE1
'ENDIF
'IF (ANIN1>ANIN0) AND (SW1EXT=0) AND (SW2RET=0) THEN
' GOSUB SLAVE2
'ENDIF
NExt J
toggle PORTA.2 'Heartbeat LED
pause 500
goto main
'SLAVE1:
'IF (ANin0-ANin1)>=5 THEN
' PORTB.0=1
' PORTB.1=0
' PORTB.2=1
'ENDIF
'if (ANin0-ANin1)<5 THEN
'PORTB.0=0
'PORTB.1=0
'PORTB.2=0
'ENDIF
'RETURN
'SLAVE2:
'IF (ANin1-ANin0)>=5 THEN
' PORTB.0=0
' PORTB.1=1
' PORTB.2=1
'ENDIF
'IF (ANin1-ANin0)<5 THEN
'PORTB.0=0
'PORTB.1=0
'PORTB.2=0
'ENDIF
'RETURN
Bookmarks