I'm a newbie when it comes to programming...with some previous experience, but getting old and remembering less...
I'm creating a depth meter using a MAXSONAR ranger - using the PW output. I've made the code work previously with a Basic Stamp BS2, but want to use the 12F683 with PBP3. The problem seems to be in the math.
My input readings "inputpulse" reads anywhere from 92 to 466 after taking the average of 10 readings and dividing by 10 (just to move back the decimal). 92 (is tank full) and 364 is tank empty. using these values I am changing the tap setting on an AD5220 (digital Potentiometer). the Digipot values range from 0 to 127.
So, I need to convert the 92 and 364 to 1% or 100% respectively and apply to 127 to get a new tap setting.
I get my average readings fine, but somewhere in the math I can't get a multiplier to properly vary the 127 to a new tap setting...it gives me 0's and 1's or 11,21,31,41,51...
I'm stumped and out of scotch!
here's the code.
Help!Code:'@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF oSCCON = %01100111 ' Ocs set to 4 MHz TRISIO = %00000000 ' Set all ports to outputs, in this example CMCON0 = 7 ' Analog comparators off ANSEL = 0 ' Analog select set to digital, pg 69 data ADCON0 = 0 ' A/D turned OFF, pg 68 of data INCLUDE "modedefs.bas" define char_pacing 1000 define osc 4 'Define Variables ************************* inputpulse var word dist var word avgdist var word x var word counter VAR byte oldtapsetting VAR word newtapsetting VAR word 'Pin Assignments ************************** ' GPIO.0 = Pin 7 - Serout LCD ' GPIO.1 = Pin 6 - Output clk to AD5220 ' GPIO.2 = Pin 5 - Output U/D Select to AD5220 ' GPIO.3 = Pin 4 - Switch Input ' GPIO.4 = Pin 3 - Input from sensor ' GPIO.5 = Not Used 'Set Variables **************************** dist = 0 oldtapsetting = 0 newtapsetting = 0 'initialize outputs ********************** low gpio.0 low gpio.1 low gpio.2 low gpio.3 low gpio.4 low gpio.5 ' Initialize AD5220 LOW gpio.2 FOR counter=0 TO 127 PULSOUT 6,5 PAUSE 1 NEXT 'Main Program **************************** 'get avg depth pause 1000 for x = 1 to 10 pulsin gpio.4,1,inputpulse dist= dist + inputpulse pause 100 next x avgdist=dist/10 select case avgdist case is < 92 newtapsetting = 1 case is > 364 newtapsetting = 127 case else avgdist = avgdist - 92 'avgdist = avgdist newtapsetting = 127 * (avgdist / 272) newtapsetting = newtapsetting end select serout gpio.0,t9600,[12,"avg :", #avgdist,13] serout gpio.0,t9600,["tap :", #newtapsetting] pause 500 end
John




Bookmarks