12F615 project going wrong, is my logic right ? - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: July 23, 2011, 09:21:51 pm
content: 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_driverdim Con, Pos, D as word ' Declarations sectionmain: ' Main programTRISIO = %00000011ANSEL = %01010011 'set GPIO.0 & 1 as analogue input, conversion clock to /16ADCON0 = %10000000 'ADON0 ADC register setup'R justify result'use VDD as Vreff'select channel 3'enable ADCGPIO = 0 ' reset all outputsstart:Con = ADC_read(0) ' read control potdelay_us(5)Pos = ADC_read(1) ' read position potif Con-Pos > 2thenGPIO.5 = 0GPIO.4 = 1else GPIO.4 = 0end ifdelay_us(100)if Pos-Con > 2thenGPIO.4 = 0GPIO.5 = 1else GPIO.5 = 0end ifGOTO startend.

author: gregariz
author_rank: Frequent Contributor
timestamp: July 23, 2011, 09:48:57 pm
content: 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

author: Simon
author_rank: Unknown
timestamp: July 23, 2011, 09:57:20 pm
content: err, RS232, now you have lost meI'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

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 24, 2011, 01:59:17 am
content: 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 oscilloscopeCode:dim tmp as worddim i as bytetmp=confor i=0 to 11 '(12bit ADC)if tmp and 1 thengpio.3=1elsegpio.3=0endiftmp=tmp > 1next iyou 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.

author: Simon
author_rank: Unknown
timestamp: July 24, 2011, 06:30:35 am
content: ok, usual damn problem with stupid mikroe basic, the clock source went and changed itself ! now for some code tinkering

author: gregariz
author_rank: Frequent Contributor
timestamp: July 25, 2011, 01:07:28 am
content: Quote from: Simon on July 24, 2011, 06:30:35 amI 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..

author: Simon
author_rank: Unknown
timestamp: July 25, 2011, 06:02:02 am
content: 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 !

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 25, 2011, 07:36:02 am
content: Quote from: Simon on July 25, 2011, 06:02:02 amthis should not be happening. you should find the source of bug. maybe in your hardware/circuit?

author: gregariz
author_rank: Frequent Contributor
timestamp: July 25, 2011, 06:03:29 pm
content: Quote from: Mechatrommer on July 25, 2011, 07:36:02 amAre 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.

author: bruce273
author_rank: Contributor
timestamp: July 26, 2011, 11:45:11 am
content: Have you disabled the watchdog timer?

author: bruce273
author_rank: Contributor
timestamp: July 26, 2011, 07:50:09 pm
content: 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

author: Simon
author_rank: Unknown
timestamp: July 26, 2011, 09:09:28 pm
content: 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

author: bruce273
author_rank: Contributor
timestamp: July 27, 2011, 08:34:54 pm
content: 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 theoryhttp://www.societyofrobots.com/programming_PID.shtmlHope it helps

author: Simon
author_rank: Unknown
timestamp: July 28, 2011, 05:46:57 am
content: thanks, i'll take a look