You are on page 1of 11

#include <avr/pgmspace.

h>

//////////////

volatile byte half_revolutions;

volatile unsigned int rpm;

volatile unsigned long timeold;

////////////////////////

const byte pullup = 3;

const byte injectPIN = 9;

volatile unsigned int injectDelayTime;

volatile unsigned long injectOnTime = 30;

volatile boolean injectOn;

volatile int alternate = 0;

int VEval ( float MAP, int rpm);

//RPM///////////////////////////////////////////////////////////////////////////////////////////////

volatile int rps ;

volatile unsigned long periodz;

//Airflow///////////////////////////////////////////////////////////////////////////////////////////

volatile double AF;

volatile int VE;

const double Ka = 0.55;

const double Kt = 1;

const int Kd = 50;//<---------------delay ke injection


const double InjSpec = 14.3625; // (4.5/1.2)*3.83

//startingfuel//

volatile int count = 1;

volatile int countr = 0;

const float sfueltime = 1.5;//fuel injection when start in ms

//vetable//

const PROGMEM uint16_t vetable [8][12] =

// RPM 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000

{ //MAP

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //40

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //50

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //60

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //70

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //80

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //90

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} , //100

{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80} //110

};

volatile int indexm,indexr,incrementm,incrementr;

//MAP///////////////////////////////////////////////////////////////////////////////////////////////

volatile int RAWMAP;

volatile double VMAP;

volatile double MAP;

//allign map//

volatile int initialInj = 1;


volatile int initialMAP = 1;

volatile double highMAP;

volatile double delMAP;

//TPS///////////////////////////////////////////////////////////////////////////////////////////////

const int TPSpin = 5;

const int minTPS = 178;

const int maxTPS = 332;

volatile double TPS;

volatile double RAWinTPS;

volatile double inTPS;

volatile double inOldTPS = 0;

volatile double outTPS = 0;

//Oil pump//////////////////////////////////////////////////////////////////////////////////////////

const byte oilPUMP = 5;

//Serialmon/////////////////////////////////////////////////////////////////////////////////////////

volatile int injPulVal;

volatile int pmpPulVal;

/////////////////////////////////////////////NILRTOS////////////////////////////////////////////////

//------------------------------------------------------------------------------

void setup()

Serial1.begin(9600);

pinMode(oilPUMP, OUTPUT);
TCCR3A = 0; // normal mode

TCCR3B = 0; // stop timer

TIMSK3 = 0; // cancel timer interrupt

TCNT3 = 0;

pinMode (injectPIN, OUTPUT);

digitalWrite (pullup, HIGH);

attachInterrupt (0, reinject, RISING);

//------------------------------------------------------------------------------

void loop()

rps = (rpm/60);//change RPM to frequency

periodz = (1000/rps);//ms

injectDelayTime = 3;

VE = VEval (MAP, rpm);

if (countr < 120)

AF = sfueltime*62.5;

else

AF=(1.25*VE*MAP*rpm*Ka)/(InjSpec*157476.7782);

injectOnTime = (AF+TPS)*62.5;

count = 0;
}

//MAP//////////////////////////////////////////////////////////////////////////////////////////

RAWMAP = analogRead(A0);

VMAP = ((RAWMAP*5)/1023);

if(VMAP >= highMAP || initialMAP == 1)

highMAP = VMAP;

initialMAP = 0;

delMAP = highMAP - VMAP;

MAP = (1/0.045)*(VMAP+0.425);

//TPS//////////////////////////////////////////////////////////////////////////////////////////////

RAWinTPS = analogRead(TPSpin);

if(RAWinTPS < minTPS)

RAWinTPS = minTPS;

if(RAWinTPS > maxTPS)

RAWinTPS = maxTPS;

inTPS = map(RAWinTPS, minTPS, maxTPS, 0, 90);

outTPS = (0.01*inTPS) - (0.01*inOldTPS) + (0.8*outTPS);


inOldTPS = inTPS;

TPS = ((outTPS*Kt*1000)/(14.7*InjSpec));

//TPS = 0;

Serial1.println("AF");

Serial1.println( rpm );

Serial1.println("map");

Serial1.println(MAP);

Serial1.println("injectOnTime");

Serial1.println(injectOnTime);

ISR (TIMER3_COMPA_vect)

// if currently on, turn off

if (injectOn)

digitalWrite (injectPIN, LOW);// inject off

injPulVal = 0;

TCCR3B = 0; // stop timer

TIMSK3 = 0; // cancel timer interrupt

EIFR = bit (INTF0); // delete any pending interrupt on D2

attachInterrupt (0, reinject, RISING); // re-instate interrupts for firing time

}
else

// hold-off time must be up

pmpPulVal = 1;

if(alternate == 0)

digitalWrite (injectPIN, HIGH); // inject on

injPulVal = 1;

if (count == 1)

countr++;

TCCR3B = 0; // stop timer

TCCR3B |= (1 << WGM32); // Turn on CTC mode (so it will start again) automatically

TCCR3B |= (CS32); // CTC, scale to clock / 256

OCR3A = injectOnTime;

alternate = 1;

if(delMAP >= 2 && initialInj == 1)

digitalWrite (injectPIN, HIGH); // inject on

TCCR3B = 0; // stop timer

TCCR3B |= (1 << WGM32); // Turn on CTC mode (so it will start again) automatically

TCCR3B |= (CS32); // CTC, scale to clock / 256

alternate = 1;
initialInj = 0;

injPulVal = 1;

OCR3A = injectOnTime; // time before timer fires

else

digitalWrite (injectPIN, LOW);

injPulVal = 0;

alternate = 0;

injectOn = !injectOn; // toggle

/////////////////////////ngat

void reinject ()

Serial.println(rpm);

injectOn = false; // make sure flag off just in case

TCCR3A = 0; // normal mode

TCCR3B |= (1 << WGM32); // Turn on CTC mode (so it will start again) automatically

TIMSK3 |= (1 << OCIE3A); // Set interrupt on compare match.

TCCR3B |= (CS32); // CTC, scale to clock / 256

OCR3A = injectDelayTime; // time before timer fires


detachInterrupt (0); // cancel any existing falling interrupt (interrupt 0)

half_revolutions++; //For the RPM calculations

if (half_revolutions >= 1) {

rpm = 60 * 1000000 / (micros() - timeold) * half_revolutions;

timeold = micros();

half_revolutions = 0;

//realcranktime = (cranktimeStop - cranktimeStart);

void fInject()

if (false) // if we need to change the time, insert condition here ...

noInterrupts (); // atomic change of the time amount

interrupts ();

int decode_rpm(int rpm)

/* initialize increment to be the difference in rpm between each array element an

the next */

incrementr = 6000/12;

indexr = 0;
/* find which array element corresponds the the rpm value */

while(indexr * incrementr < rpm)

indexr++;

return indexr;

int decode_MAP(int MAP)

/* you can also just do the division explicitly */

incrementm = 110/11;

indexm = 0;

// int MAPcompare = ;

while(((indexm*incrementm)+40) < MAP)

indexm++;

return indexm;

int VEval ( float MAP, int rpm)

volatile int rpm_index = decode_rpm(rpm);

volatile int MAP_index = decode_MAP(MAP);

return pgm_read_word(&vetable[MAP_index][rpm_index - 1]);

You might also like