Option PortA "TTTTT0TP" Option PortC "0T0TT1T0" Public Miles as persistent single 'always have this as the first Dim Public MPGAA(1 TO 100) AS PERSISTENT SINGLE 'Array to hold MPGA Public OutQueSize As Integer = 40 Public OutQue(1 To 40) As Byte 'Used for sending text or byte to the display PUBLIC spaces (8) as string Public tx as string public lcd as byte public c as integer 'c for counter dim MPHC as byte Dim MPHA (1 to 5) as single Dim MPHAA as single Dim MPH as SINGLE Dim Distance as integer Dim TenthMile as integer = 50 'Set this to the distance count for .1 mile PUBLIC MPGI AS SINGLE PUBLIC MPGA AS SINGLE Public MPGAT AS SINGLE 'HOLDS TOTAL OF ARRAY Public MPGFLAG AS BYTE = 0 ' USED TO INDICATE COUNT OF 100 REACHED Public MPGC AS BYTE =1 Public MPGA1 as single Public KB as byte = 1 Public KBPin as byte = 20 Public KBreset as byte Dim MixtureTotal as Single = 0.0145 Dim MixtureLow as single Public Mixture as Single Public MAF1 as integer public MAFpin as byte = 19 Public MAF as single ' Oil Pressure Sub Public Oil1 as integer public Oilpin as byte = 17 Public Oil as single Public Temp1 as integer public Temppin as byte = 16 Public Temp as single Public Volt1 as integer public Voltpin as byte = 15 Public Volt as single Public O21 as integer public O2pin as byte = 13 Public O2 as single Public Fuel1 as integer public Fuelpin as byte = 14 Public Fuel as single Public RPM as single public RPMpin as byte = 11 Public CEL as single public CELpin as byte = 12 '---------------------------------------------------------- Public sub main() 'Start of main program '---------------------------------------------------------- if firsttime()=true then miles=0.0 ' use to clear miles in persistent memory call InilizeMPGA() end if Call OpenQueue(OutQue, OutQueSize) 'Open the output queue Call DefineCom(3,0,7, bx0000_1000) 'non-inverted, no parity, 8 bits. Call OpenCom(3, 19200, 0, OutQue) 'Open Com3 at 19,200 Baud call putpin(7,1) 'Resets the LCD interface Call delay(0.5) Call PutQueueByte(outque,&h0C) 'Reset and clear the screen Call delay(1.2)'This is normally 0.2. This way I can see the reset Call PutQueueByte(outque,&h11) 'Turn on Backlight Call PutQueueByte(outque,&h16) ' Turn on the LCD, no cursor call startmpga() c=0 spaces(c) = "" c=1 spaces(c) = " " c=2 spaces(c) = " " c=3 spaces(c) = " " c=4 spaces(c) = " " c=5 spaces(c) = " " c=6 spaces(c) = " " c=7 spaces(c) = " " c=8 spaces(c) = " " Start: Call GetKB 'Call GetMPH() 'Call DistanceCheck() 'Call GetMixture() 'Call DistanceCheck() 'Call GetMAF() Call GetTemp() 'Call GetOil() Call GetVolts() 'Call DistanceCheck() 'Call GetMPGI() 'Call GetMPGA() 'Call DistanceCheck() Call GetFuel() Call GetO2Volts If KB=1 then Call DisplayTempOil() end if Call DistanceCheck() if KB=2 then call DisplayVoltFuel() end if Call DistanceCheck() If KB=3 Then call DIsplayO2Maf() end if Call DistanceCheck() if KB=4 then call DisplayMPGIMPGA() End if Call DistanceCheck() if KB=5 then call DisplayMilesFuel() End if If KB=6 then Call DisplayMixRPM() end if Call DistanceCheck() If KB=7 then Call ResetMiles() End If goto Start: end sub '---------------------------------------------------------- Public Sub GetMPGI() '---------------------------------------------------------- If MPH=0.0 then mpgi=0.0 exit Sub end if MPGI=MPH/((((0.0114*MAF^4)+(0.0286*MAF^3)+(0.1163*maf^2)+0.2052*maf)-0.0686)/14.7)*60.0*60.0/100.0 End Sub '---------------------------------------------------------- Public Sub GetMPGA() '---------------------------------------------------------- dim Temp2 as single If MPH=0.0 then exit Sub end if If mpgc<99 and mpgflag=0 then mpgc=mpgc+1 mpgaa(mpgc)=mpgi Mpgat=mpgat+mpgi mpga=mpgat/csng(mpgC) exit sub end if If mpgc >= 100 then mpgflag=1 mpgc=1 end if Temp2=mpgaa(mpgc) mpgaa(mpgc)=MPGI 'Get old value out MPGAT=MPGAT-TEMP2 'Subtract it from the total MPGAT=MPGAT+MPGI 'Add the current MPGI MPGC=MPGC+1 'Incriment for nest time MPGA=MPGAT/100.0 'put the average in MPGA END SUB '--------------------------------------------------------------- Public Sub InilizeMPGA() '--------------------------------------------------------------- dim count as byte=1 for count = 1 to 100 mpgaa(count)=0.0 next COUNT MPGFlag=0 MPGC=1 end sub '--------------------------------------------------------------- Public Sub StartMPGA() '--------------------------------------------------------------- Dim count as byte = 1 for count = 1 to 100 mpgat=mpgat+mpgaa(count) Next count mpga=mpgat/100.0 mpgflag=1 mpgc=1 end sub '------------------------------------------------------------------------------ Public Sub Displaytempoil() 'KB=1 '------------------------------------------------------------------------------ Call PutQueueByte(OutQue,&H94) tx="Oil " Call PutQueueStr(OutQue,tx) c=len (cstr(oil1)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(oil1) Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H80) tx="Temp " Call PutQueueStr(OutQue,tx) c=len (cstr(Temp1)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(temp1) Call PutQueueStr(OutQue,tx) Call Delay (0.01) end sub '------------------------------------------------------------------------------ Public Sub DisplayVoltFuel()' KB=2 '------------------------------------------------------------------------------ Call PutQueueByte(OutQue,&H80) tx="Volt " Call PutQueueStr(OutQue,tx) c=len (cstr(Volt)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(Volt) Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H94) tx="Fuel " Call PutQueueStr(OutQue,tx) c=len (cstr(Fuel)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(Fuel) Call PutQueueStr(OutQue,tx) Call Delay (0.01) End sub '------------------------------------------------------------------------------ Public Sub DisplayO2Maf() 'KB=3 '------------------------------------------------------------------------------ Call PutQueueByte(OutQue,&H80) tx="O2Volt " Call PutQueueStr(OutQue,tx) O2 = (fix(O2*100.0))/100.0 'fixes if more than two dec points. c=len (cstr(O2)) 'This is the fill-in code if c>8 then c=8 end if c=8-c tx = spaces(c) & cstr(O2) Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H94) tx="MAF " Call PutQueueStr(OutQue,tx) MAF = (fix(MAF*10.0))/10.0 'fixes if more than one dec point. c=len (cstr(MAF)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(MAF) Call PutQueueStr(OutQue,tx) Call Delay (0.01) End sub '------------------------------------------------------------------------------ Public Sub DisplayMPGIMPGA()'KB=4 '------------------------------------------------------------------------------ Call PutQueueByte(OutQue,&H80) tx="MPGI " Call PutQueueStr(OutQue,tx) MPGI = (fix(MPGI)) c=len (cstr(MPGI)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(MPGI) Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H94) tx="MPGA " Call PutQueueStr(OutQue,tx) MPGA = (fix(MPGA)) c=len (cstr(MPGA)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(MPGA) Call PutQueueStr(OutQue,tx) Call Delay (0.01) end sub '------------------------------------------------------------------------------ Public Sub DisplayMilesFuel()' KB=5 '------------------------------------------------------------------------------ Call PutQueueByte(OutQue,&H80) tx="Miles " Call PutQueueStr(OutQue,tx) Miles= (fix(Miles*10.0))/10.0 'fixes if more than one dec points. c=len (cstr(Miles)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(Miles) Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H94) tx="Fuel " Call PutQueueStr(OutQue,tx) 'Fuel = (fix(Fuel*100.0))/100.0 'fixes if more than one dec point. c=len (cstr(Fuel)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(Fuel) Call PutQueueStr(OutQue,tx) Call Delay (0.01) end sub '------------------------------------------------------------------------------ Public Sub DisplayMIXRPM()' KB=6 '------------------------------------------------------------------------------ Call PutQueueByte(OutQue,&H80) tx="MIXTURE " Call PutQueueStr(OutQue,tx) Mixture = (fix(MIXTURE)) c=len (cstr(MIXTURE)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(MIXTURE) Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H94) tx="RPM " Call PutQueueStr(OutQue,tx) RPM = (fix(RPM)) c=len (cstr(RPM)) 'This is the fill-in code c=8-c tx = spaces(c) & cstr(RPM) Call PutQueueStr(OutQue,tx) Call Delay (0.01) end sub '--------------------------------------------------------- Public Sub ResetMiles()'KB=7 '--------------------------------------------------------- Call PutQueueByte(OutQue,&H80) tx="Reset Fuel&Miles" Call PutQueueStr(OutQue,tx) Call Delay (0.01) Call PutQueueByte(OutQue,&H94) tx="NO YES" Call PutQueueStr(OutQue,tx) Call Delay (0.01) iF KBRESET=1 and KB=6 THEN Call getkb() If KBReset=1 then Fuel=0.0 MILES=0.0 CALL InilizeMPGA() KBRESET=0 END IF END IF end sub '------------------------------------------------------------------------------ Public Sub GetVolts() '------------------------------------------------------------------------------- Volt1 = GetADC(Voltpin) Volt=csng(Volt1) Volt=Volt*4.0 Volt=(fix(((Volt*0.0044)+0.05)*10.0))/10.0 End Sub 'Returns with the Voltage in Volt as single to 1 dec place '----------------------------------------------------------------------------- Public Sub GetTemp() '----------------------------------------------------------------------------- Temp1 = GetADC(Temppin) temp=csng(temp1) Temp=Temp*2.0 Temp=(fix(temp*0.0044)) Temp=fix(((5.0-Temp)*30.0)+110.0) Temp1=cint(temp) End Sub 'Returns with the temperture in Temp as single '----------------------------------------------------------------------------- Public Sub GetOil() '----------------------------------------------------------------------------- Oil1 = GetADC(Oilpin) Oil=csng(Oil1) Oil=Oil*2.0 Oil=(fix(Oil*0.0044)+0.5) Oil=(Oil-2.3)*16.111 Oil1=cint(oil) End Sub 'Returns with the Oil Pressure in Oil as single ' to 0 dec place '----------------------------------------------------------------------------- Public Sub GetFuel() '----------------------------------------------------------------------------- Fuel1 = GetADC(FuelPin) Fuel=csng(Fuel1) Fuel=Fuel*2.0 Fuel=5.037*Fuel/1024.0 Fuel = (fix(Fuel*100.0))/100.0 'fixes if more than one dec point. 'Fuel=(Fuel-2.3)*16.111 End Sub 'Returns with the Fuel Voltage in Fuel as single '---------------------------------------------------------------- Public Sub GetMPH() '--------------------------------------------------------------- Dim speed1 as single Dim MPH1 as single Dim Speed as integer if getpin(6)=0 then Speed=pulsein(6,1) else speed=pulsein(6,0) end if speed1 = csng(speed) MPH1=416667.0*(Speed1^-1.0) If mphc < 1 or mphc >5 then mphc=1 end if mpha(mphc)=mph1 mphc=mphc+1 mphaa=(mpha(1)+mpha(2)+mpha(3)+mpha(4)+mpha(5))/5.0 MPH = mphaa End Sub ' Returns MPH as SINGLE '--------------------------------------------- Public Sub DistanceCheck() '--------------------------------------------- Static flag as byte Dim Dist as byte Dist = getpin(9) if dist = 1 and flag = 0 then Distance = distance + 1 Flag = 1 If Distance > TenthMile then Miles = miles + 0.1 Distance = distance - TenthMile end if elseif dist = 0 and flag = 1 then Flag = 0 end if end sub 'Returns Miles as Single '-------------------------------------------------- Public Sub GetKB() '-------------------------------------------------- dim KBValue as integer Dim KBValue1 as single static KBFlag as byte KBValue = GetADC(KBPin) If KBValue < 1000 then Call Delay (0.01) KBValue = GetADC(KBPin) End If KBValue1= Csng(KBValue) KBValue1=5.0*kbvalue1/1024.0 KBValue = cint(KBValue1) If KBValue = 5 then KBFlag=0 exit sub end if If KBValue =2 and KBFlag = 0 then KB = KB + 1 KBFlag=1 end if If KBValue = 4 and KBFlag = 0 then KBFlag = 1 KBReset=1 end if If KB >7 or KB <= 0 then KB = 1 end if END SUB '------------------------------------------------------------------------------- Public Sub GetMixture() '------------------------------------------------------------------------------- Dim Mixture1 as single Call Pulsein(8,0,MixtureLow) Mixture1=MixtureLow / (MixtureTotal) 'finds % of On time Mixture1=(Mixture1*100.0) + 0.5 'Converts to a % Mixture=fix(mixture1) 'Removes dec portion of Mixture End Sub '-------------------------------------------------------------------------------- Public Sub GetMAF() '-------------------------------------------------------------------------------- MAF1 = GetADC(MAFpin) MAF=csng(MAF1) MAF=5.037*MAF/1024.0 MAF=0.0114*MAF^4+0.0286*MAF^3+0.1163*MAF^2+0.2050*MAF-0.0686 'MAF=fix(maf*100.0)/100.0 ' converts to 2 dec places if needed End Sub 'Returns with the Mas Air Flow in MAF in Oz/Sec '-------------------------------------------------------------------------------- Public Sub GetO2Volts() '-------------------------------------------------------------------------------- O21 = GetADC(O2Pin) O2=csng(O21) O2=5.0*O2/1024.0 'O2=fix(O2*100.0)/100.0 ' converts to 2 dec places if needed End Sub 'Returns with the Mas Air Flow in MAF in Oz/Sec