I am writing some code for a PIC16F648A using PicBasic Pro. The idea for the circuit is for the PIC to monitor 6 comparators. The comparators go high based on the angular position of a potentiometer. The comparators are set up in 15 degree increments spanning 45 degrees to each side of center.

The circuit is to activate an LED and flash it when either 45 degree limit is reached. This portion of the code works correctly.

The other function of the code is to drive a motor to return the potentiometer to center. The idea is for the PIC to look at which comparator is active, drive the motor accordingly and stop at the 15 degree from center mark. I was then going to use a timed event to get it close to center. I cannot reach exact center due to potentiometer hysterisis. That is OK.

I believe there is an issue with my If,THEN statements. I have limited experience using them. When I activate the button to command the centering function, I get a latching voltage that will not go low until the other extreme limit is reached, not at 15 degrees as it should.


Here is my code:

TRISA=%11111000 'make A0, A1 & A2 outputs
TRISB=%11101111 'make B0, B1, B2, B3, B5, B6 & B7 inputs

CCWDRV var PortA.0 'change port name
CWDRV var PortA.1 'change port name
LED var PortA.2 'change port name
CNTR var PortB.0 'change port name
PT1 var PortB.7 'change port name
PT2 var PortB.6 'change port name
PT3 var PortB.5 'change port name
PT4 var PortB.3 'change port name
PT5 var PortB.2 'change port name
PT6 var PortB.1 'change port name


cycle:

if (PT1=0) and (PT2=0) and(PT3=0) and(PT4=0) and(PT5=0) and (PT6=0) and (CNTR=0) Then '000 000 right stop
low LED
pause 10
high LED
pause 500
low LED
pause 500
goto cycle
endif

if (PT1=1) and (PT2=1) and(PT3=1) and(PT4=1) and(PT5=1) and(PT6=1) and (CNTR=0) Then '111 111 left stop
low LED
pause 10
high LED
pause 500
low LED
pause 500
goto cycle
endif

if (PT1=0) and (PT2=0) and(PT3=0) and(PT4=0) and(PT5=0) and(PT6=0) and (CNTR=1) Then '000 000 center from right stop
low LED
pause 10
high CCWDRV
gosub CCWCENTER 'go to sub-routine to center from right
goto cycle
endif

if (PT1=1) and (PT2=1) and(PT3=1) and(PT4=1) and(PT5=1) and(PT6=1) and (CNTR=1) Then '111 111 center from left stop
low LED
pause 10
high CWDRV
gosub CWCENTER 'go to sub-routine to center from left
goto cycle
endif

if (PT1=1) and (PT2=0) and(PT3=0) and(PT4=0) and(PT5=0) and(PT6=0) and (CNTR=1) Then '100 000
low LED
pause 10
high CCWDRV
gosub CCWCENTER 'go to sub-routine to center from right
goto cycle
endif

if (PT1=1) and (PT2=1) and(PT3=0) and(PT4=0) and(PT5=0) and(PT6=0) and (CNTR=1) Then '110 000
low LED
pause 10
high CCWDRV
gosub CCWCENTER 'go to sub-routine to center from right
goto cycle
endif

if (PT1=1) and (PT2=1) and(PT3=1) and(PT4=1) and(PT5=0) and(PT6=0) and (CNTR=1) Then '111 100
low LED
pause 10
high CWDRV
gosub CWCENTER 'go to sub-routine to center from left
goto cycle
endif

if (PT1=1) and (PT2=1) and(PT3=1) and(PT4=1) and(PT5=1) and(PT6=0) and (CNTR=1) Then '111 110
low LED
pause 10
high CWDRV
gosub CWCENTER 'go to sub-routine to center from left
goto cycle
endif

GOTO cycle

CCWCENTER
if (PT1=1) and (PT2=1) and(PT3=1) and(PT4=0) and(PT5=0) and(PT6=0) Then
low CCWDRV 'stop motor at 111000 position
high CCWDRV 'start motor for last leg of travel
pause 1000 'run time to go from 15 degrees to center CCW
low CCWDRV
endif
return

CWCENTER
if (PT1=1) and (PT2=1) and(PT3=1) and(PT4=0) and(PT5=0) and(PT6=0) Then
low CWDRV 'stop motor at 111000 position
high CWDRV 'start motor for last leg of travel
pause 1000 'run time to go from 15 degrees to center CW
low CWDRV
endif
return

end


Thank you for the help,
Paul