Electronics > Microcontrollers

12F615 project going wrong, is my logic right ?

(1/3) > >>

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 ?

--- Code: ---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


Con = ADC_read(0)                      ' read control pot
Pos = ADC_read(1)                      ' read position pot

if Con-Pos > 2
GPIO.5 = 0
GPIO.4 = 1
else GPIO.4 = 0
end if


if Pos-Con > 2
GPIO.4 = 0
GPIO.5 = 1
else GPIO.5 = 0
end if

GOTO start
--- End code ---

I dont have anything specific to suggest .. code looks ok to me but I have had trouble in the past with the ADC register setups, so thats where I would look first.

As a tip I always wire the tx/rx pins out to a max232 or ftdi chip so that I can pump any variables to a terminal prog on a PC to help with debugging

err, RS232, now you have lost me  ;)

I'm going to try a new variant where I'll not do the subtraction to avoid negative numbers in case the > operation cannot distinguish + and - numbers, I'll try tinkering with the logic algorithm as something must be up there

this is one simple example i'll use to debug one variable by outputting in serial data like value (LSB first) in one of pin output and checked/probed with oscilloscope

--- Code: ---dim tmp as word
dim i as byte
for i=0 to 11 '(12bit ADC)
    if tmp and 1 then
    tmp=tmp > 1
next i

--- End code ---

you can as well put LED in place of h-bridge controller to see if they light up correctly.
they maybe issue with negative number, and you have to carefully do the right shift in the above code, vb dont provide > operator, you can substitute with / 2 (divide by 2) but then further analysis on negative number on you compiler is needed. one way is to do several operation to intentionally create negative number and/or do division and output the value using code above. if the output is not what you expected, then the compiler is not doing what you expected. hope it help.

ok, usual damn problem with stupid mikroe basic, the clock source went and changed itself ! now for some code tinkering


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version