Mostly working scheduler

This commit is contained in:
Josh Stewart 2013-02-14 08:51:29 +11:00
parent 25253f0beb
commit feff0b244d
2 changed files with 25 additions and 14 deletions

View File

@ -51,6 +51,7 @@ struct table fuelTable;
unsigned long injectTime[engineCylinders]; //The system time in uS that each injector needs to next fire at
boolean intjectorNeedsFire[engineCylinders]; //Whether each injector needs to fire or not
unsigned long counter;
void setup() {
@ -80,12 +81,12 @@ void setup() {
dummyFuelTable(&fuelTable);
initialiseScheduler();
counter = 0;
}
void loop()
{
delay(2500);
//delay(2500);
//Always check for sync
//Main loop runs within this clause
if (hasSync)
@ -108,18 +109,22 @@ void loop()
int pulseWidth = PW(req_fuel, VE, MAP, 100, engineInjectorDeadTime); //The 100 here is just a placeholder for any enrichment factors (Cold start, acceleration etc). To add 10% extra fuel, this would be 110
//Serial.println(VE);
Serial.print("VE: ");
Serial.println(VE);
//Serial.print("VE: ");
//Serial.println(VE);
Serial.print("Injector pulsewidth: ");
Serial.println(pulseWidth);
//Serial.print("Injector pulsewidth: ");
//Serial.println(pulseWidth);
//Serial.println(req_fuel * (float)(VE/100.0) * (float)(MAP/100.0) * (float)(100/100.0) + engineInjectorDeadTime);
//Serial.println( (float)(req_fuel * (float)(VE/100)) );
//Serial.println( (float)(VE/100.0));
//920 out
if (counter > 100000) {
Serial.print("Calling schedule at: ");
Serial.println(micros());
setSchedule1(openInjector2, 1000);
setSchedule1(openInjector2, 1000000);
counter = 0;
}
counter++;
}
else
@ -129,7 +134,7 @@ void loop()
//Serial.println(toothLastToothTime);
//Serial.println(toothLastMinusOneToothTime);
//Serial.println(rpm);
delay(100);
//delay(100);
}

View File

@ -14,8 +14,13 @@ This differs from most other schedulers in that its calls are non-recurring (IE
Each timer can have only 1 callback associated with it at any given time. If you call the setCallback function a 2nd time, the original schedule will be overwritten and not occur
Timer identification
The Adrduino timer2 is used for schedule 1
The Arduino timer3 is used for schedule 2
The Arduino timer3 is used for schedule 1
The Arduino timer4 is used for schedule 2
Both of these are 16-bit timers (ie count to 65536)
See page 136 of the processors datasheet: http://www.atmel.com/Images/doc2549.pdf
256 prescale gives tick every 16uS
256 prescale gives overflow every 1048576uS (This means maximum wait time is 1.0485 seconds)
*/
@ -40,6 +45,7 @@ void initialiseScheduler()
TIMSK3 = 0x01; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
TCCR3A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
TCCR3B = (1 << CS12); //Timer2 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
//TCCR3B = (1 << CS11) | (1 << CS10); //Timer2 Control Reg B: Timer Prescaler set to 64
}
@ -54,9 +60,9 @@ void setSchedule1(void (*callback)(), unsigned long timeout)
{
//We need to calculate the value to reset the timer to (preload) in order to achieve the desired overflow time
//As the timer is ticking every 16uS (Time per Tick = (Prescale)*(1/Frequency))
///TCNT2 = ((255 - (timeout>>4)) * 256) / clockspeed;
//TCNT2 = (timeout / 8192);
TCNT3 = 1;
//TODO: Need to add check for timeout > 1048576 ????
TCNT3 = 65536 - (timeout / 16); //Each tick occurs every 16uS with a 256 prescaler so divide the timeout by 16 to get ther required number of ticks. Subtract this from the total number of tick (65536 for 16-bit timer)
//TCNT3 = 0;
schedule1Callback = callback; //Name the callback function
schedule1Active = 1; //Turn this schedule on
}