Author Topic: 12F615 project going wrong, is my logic right ?  (Read 7535 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
12F615 project going wrong, is my logic right ?
« on: July 23, 2011, 09:21:51 pm »
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: [Select]
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.
 

Offline gregariz

  • Frequent Contributor
  • **
  • Posts: 545
  • Country: us
Re: 12F615 project going wrong, is my logic right ?
« Reply #1 on: July 23, 2011, 09:48:57 pm »
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
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 12F615 project going wrong, is my logic right ?
« Reply #2 on: July 23, 2011, 09:57:20 pm »
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
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11648
  • Country: my
  • reassessing directives...
Re: 12F615 project going wrong, is my logic right ?
« Reply #3 on: July 24, 2011, 01:59:17 am »
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: [Select]
dim tmp as word
dim i as byte
tmp=con
for i=0 to 11 '(12bit ADC)
    if tmp and 1 then
        gpio.3=1
    else
        gpio.3=0
    endif
    tmp=tmp > 1
next i

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.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 12F615 project going wrong, is my logic right ?
« Reply #4 on: July 24, 2011, 06:30:35 am »
ok, usual damn problem with stupid mikroe basic, the clock source went and changed itself ! now for some code tinkering
 

Offline gregariz

  • Frequent Contributor
  • **
  • Posts: 545
  • Country: us
Re: 12F615 project going wrong, is my logic right ?
« Reply #5 on: July 25, 2011, 01:07:28 am »
ok, usual damn problem with stupid mikroe basic, the clock source went and changed itself ! now for some code tinkering

I love mikroe basic but I find that it does crazy stuff if you have a problem in your program somewhere. For example I was using I2C on port C as well as another device. I hadn't initialized port C properly (my fault) but I was going nuts because commenting and uncommenting a line that did nothing in the program made it work! go figure..
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 12F615 project going wrong, is my logic right ?
« Reply #6 on: July 25, 2011, 06:02:02 am »
I have already told them that they need to do something about setting up the registers, for example the ADC registers, i mean if you put an ADC read command in your program you obviously want to use the ADC so why not detect that and set the appropriate register values. But no I have to get the stupid microchip datasheet that has the information spread all over the place and figutre it out and have to tell the pic that I want the pin to be an input AND an analogue one at that, which is pretty obvious if i have done and ADC read command on it.

Another helpful thing would be if the compiler would check the register settings and that they are compatible with the program.

Of course it would be even more helpful if the clock source did not keep changing on it own !
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11648
  • Country: my
  • reassessing directives...
Re: 12F615 project going wrong, is my logic right ?
« Reply #7 on: July 25, 2011, 07:36:02 am »
Of course it would be even more helpful if the clock source did not keep changing on it own !
this should not be happening. you should find the source of bug. maybe in your hardware/circuit?
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline gregariz

  • Frequent Contributor
  • **
  • Posts: 545
  • Country: us
Re: 12F615 project going wrong, is my logic right ?
« Reply #8 on: July 25, 2011, 06:03:29 pm »
Of course it would be even more helpful if the clock source did not keep changing on it own !
this should not be happening. you should find the source of bug. maybe in your hardware/circuit?

Are you using their PicFlash download cable? The only time I find that keeps changing is when it detects a different device.

I love the idea of a register check. Thats the main problem I keep running into, and yes I too needed to get into Microchip's data sheets to track down the ADC register settings - but some of the settings were a bit weird IIRC so I blame Microchip for that.
 

Offline bruce273

  • Contributor
  • Posts: 39
  • Country: gb
Re: 12F615 project going wrong, is my logic right ?
« Reply #9 on: July 26, 2011, 11:45:11 am »
Have you disabled the watchdog timer?
 

Offline bruce273

  • Contributor
  • Posts: 39
  • Country: gb
Re: 12F615 project going wrong, is my logic right ?
« Reply #10 on: July 26, 2011, 07:50:09 pm »
You could define your variables as integers instead of words so long your using the MSB of the ADC otherwise you could use a longint at the expense of some processing time. This will allow you to have negative numbers.

Also if i'm reading your code correctly ADCON0 will need to be 10001101 for the settings in your comments.

To disable the watchdog you will need to write xxxxx00 xxxx0101 to the CONFIG register
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 12F615 project going wrong, is my logic right ?
« Reply #11 on: July 26, 2011, 09:09:28 pm »
well it's all working fine now, I did completely redo the registers. I'm just fiddling with timings to work around the physical specs of the valve to get around oscillation.

I'm using the pickit2
 

Offline bruce273

  • Contributor
  • Posts: 39
  • Country: gb
Re: 12F615 project going wrong, is my logic right ?
« Reply #12 on: July 27, 2011, 08:34:54 pm »
thats most likely a control problem. you can have a look for PID controller (or the subtypes P, PI) on google, I've also found a website that explains it nicely without going into the Electronic Engineering Degree theory

http://www.societyofrobots.com/programming_PID.shtml

Hope it helps
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 12F615 project going wrong, is my logic right ?
« Reply #13 on: July 28, 2011, 05:46:57 am »
thanks, i'll take a look
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf