' =========================================================================
'   Fuel Injection program
' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSCHS3, TURBO, STACKX, OPTIONX, BOR42 'OSCHS3
FREQ            75_000_000
ID		"V0.82"

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
twov    var byte(3)
fivev   var byte(3)
idx     var byte
idx2    var word
idx3    var byte
temp    var word
temp2   var word
ratio   var byte
'fire    var byte
ve      var word
ve2     var word
tpsinc  var word
tpsold  var byte
rpm     var byte
'cou var word
ox var byte
' =========================================================================
  PROGRAM Start
' =========================================================================


' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------
I2CSND       SUB     1
I2CST        SUB     0
I2CSP        SUB     0

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------


Start:
' allow fuel presure to build up for 50 milli seconds
pause 75 '50

'initialize 5 volt a/d
I2CST
I2CSND %10010010
I2CSND %00000100
I2CRECV RC.6, fivev(0), 1
I2CSP

'initialize 2.5 volt a/d
I2CST
I2CSND %10010000
I2CSND %00000100
I2CRECV RC.6, twov(0), 1
I2CSP

low ra.0
low ra.1
low ra.2
low ra.3

idx = 0
ratio = 147 ' air fuel ratio
'fire = 1
idx2 = 0
idx3 = 0
've = 34 ' volmumetric effieceny 34.453125
tpsold = 163
'''
rpm = 0
ox = 100


low rc.1
high rb.7
low rb.5
high rb.4
low rb.2
low rb.1
low rb.0

'--------------- Intial fuel system prime
if twov(2) > 148 Then ' while engine is less then ~35C prime it. 15 milli seconds
pulsout ra.1, 15, 100 'left side
pulsout ra.0, 15, 100 'right side
endif


Main:


if rc.5 = 1 THEN
pulsin rc.5, 0, rpm, 10 ' get engine rpm
if rpm = 0 THEN 
' engin not running
ELSE
'if rc.5 = 0 THEN
'fire = 0

for idx = 0 to 3
  ' recive 5v
  I2CST
  I2CSND %10010011
  I2CRECV RC.6, fivev(idx), 1
  I2CSP
' recive 2.5v
  I2CST
  I2CSND %10010001
  I2CRECV RC.6, twov(idx), 1
  I2CSP

next

'if idx3 < 50 THEN ' initial 50 revolutions extra gas ratio of 1:6
'  inc idx3
'  ratio = 90
'ELSE
if twov(2) < 148 THEN 'wait till engin is about 35C 
  ratio = 162 '160 '156
  high rc.0 '  engin warm
ELSE
  low rc.0 ' cold engine 50 at -6C(196) and 160 at 35C(148) 
  temp = 23 * twov(2)
  temp = 5000 - temp
  temp = temp / 10
  ratio = temp_LSB
ENDIF


'------------------- VE CALC
''if idx3 < 40 THEN
''ve = 45 '45
''ELSE
ve = fivev(1) * 2
ve = ve / 3
ve2 = fivev(1) * rpm
ve2 = ve2 / 220
ve = ve - ve2
''ENDIF
'---------------------- O2 sensor adjusting ----------------
'low rb.0
'low rb.1
if idx3 < 205 then
inc idx3
endif

if rb.6 = 1 then 'if enabled run o2 adjusting
if idx3 > 200 then
idx3 = 100
'if rpm > 65 THEN 'only adjust at closed throttle (disabled)
'if idx3 > 495 then ' wait for end
if twov(2) < 155 then 'enging warm enuf
if fivev(2) > 140 then ' throttle closed

if twov(3) > 70 then ' runnin rich make leaner
ox = ox - 1 
endif
if twov(3) < 40 then ' running lean make richer
ox = ox + 1
endif

 ENDIF 
 ENDIF
endif
ve = ve * ox
ve = ve / 100
'endif
endif  
'    IF twov(3) < 45 THEN 'running lean make richer
'if rb.3 = 1 THEN
'      ox = ox + 1
've = ve * ox
've = ve / 100
'ENDIF
'      high rb.1  
'    ENDIF'

'ELSE
'   if twov(3) > 75 THEN 'running rich make leaner
'      high rb.0
'    ENDIF
'  
'    IF twov(3) < 45 THEN 'running lean make richer
'      high rb.1  
'    ENDIF
'ENDIF
'----------------------TPS sensor enriching
tpsinc = 0
if tpsold > fivev(2) THEN
 tpsinc =  tpsold - fivev(2)
if fivev(2) > 155 THEN
tpsinc = tpsinc * 24' 25 
ELSE
tpsinc = tpsinc * 6 '5
ENDIF   ' accerloatior enrich
 ENDIF

tpsold = fivev(2)
'---------------------- low rpm idle increase to prevent stall( Expermental, not sure if i want to keep this or not)
if idx3 > 10 THEN '10 '45
if rpm > 120 THEN
  tpsinc = tpsinc + 10
ENDIF 
ENDIF

'----------------------calculate fuel

temp = 243 * fivev(1) 'map sensor
temp2 = 514 - twov(1) 'temp sensor
temp = temp / temp2
temp = temp * 237  '237


temp2 = temp / 50
temp2 = temp2 * ve
temp2 = temp2 / ratio ' ratio
temp2 = temp2 + tpsinc
temp2 = temp2 + 25 ' injector open time 25 on 4,  100 on 1
pulsout ra.1, temp2, 4  ' left side of engine

'-------------------- second cyclnder
temp2 = temp / 49
temp2 = temp2 * ve
temp2 = temp2 / ratio ' ratio
temp2 = temp2 + tpsinc
temp2 = temp2 + 25 ' injector open time 25 on 4,  100 on 1
pulsout ra.0, temp2, 4 'right side of engine


'ENDIF


endif
ENDIF


'watch temp
'watch ve
'watch fivev(0)
'watch fivev(1)
'watch fivev(2)
'watch fivev(3)
'watch twov(0)
'watch twov(1)
'watch twov(2)
'watch twov(3)

'break

GOTO Main


' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------

I2CSND:
I2Csend RC.6, __PARAM1
RETURN

I2CST:
I2CSTART RC.6
RETURN

I2CSP:
I2CSTOP RC.6
RETURN
' -------------------------------------------------------------------------

