So I have written the below program for a 12F615. The idea is that Con and Pos are the values of two potentiometers. One (Pos) is in a water valve and tells the pic the position of the valve and the other (Con) is the potentiomter used in the circuit to determine where we want the valve. So the idea is that if the circuit detects that the control pot is significantly higher or lower than the feedback pot it will operate the valve motor via a H bridge to move the valve in the desired direction.
At the moment the valve will not work and i have a zero volts output with some oscillation which seems to indicate that the circuit can't decide what direction to send the valve in. I always seem to mess up register settings so can someone confirm these are correct. Is my "logic" for the control program otherwise correct ?
what happens if I subtract two word variables and the answer is negative, can this cause an issue ?
program valve_driver
dim Con, Pos, D as word ' Declarations section
main: ' Main program
TRISIO = %00000011
ANSEL = %01010011 'set GPIO.0 & 1 as analogue input, conversion clock to /16
ADCON0 = %10000000 'ADON0 ADC register setup
'R justify result
'use VDD as Vreff
'select channel 3
'enable ADC
GPIO = 0 ' reset all outputs
start:
Con = ADC_read(0) ' read control pot
delay_us(5)
Pos = ADC_read(1) ' read position pot
if Con-Pos > 2
then
GPIO.5 = 0
GPIO.4 = 1
else GPIO.4 = 0
end if
delay_us(100)
if Pos-Con > 2
then
GPIO.4 = 0
GPIO.5 = 1
else GPIO.5 = 0
end if
GOTO start
end.