2015-05-28 16:49:44 -07:00
/*
Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright ( C ) Josh Stewart
A full copy of the license may be found in the projects root directory
*/
2015-02-14 09:04:00 -08:00
# include "scheduler.h"
# include "globals.h"
2013-07-10 04:18:18 -07:00
2013-09-04 17:27:16 -07:00
void initialiseSchedulers ( )
2013-07-10 04:18:18 -07:00
{
2014-01-30 20:02:32 -08:00
// Much help in this from http://arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html
2013-09-04 17:27:16 -07:00
//Fuel Schedules, which uses timer 3
TCCR3B = 0x00 ; //Disbale Timer3 while we set it up
2013-07-10 04:18:18 -07:00
TCNT3 = 0 ; //Reset Timer Count
2013-09-04 17:27:16 -07:00
TIFR3 = 0x00 ; //Timer3 INT Flag Reg: Clear Timer Overflow Flag
TCCR3A = 0x00 ; //Timer3 Control Reg A: Wave Gen Mode normal
TCCR3B = ( 1 < < CS12 ) ; //Timer3 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
2015-02-06 02:19:16 -08:00
//TCCR3B = 0x03; //Timer3 Control Reg B: Timer Prescaler set to 64. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
2015-09-24 18:57:44 -07:00
//Timer 3 compare channel A is reserved for idle control, therefore there are only 2 fuel channels here
2015-09-29 00:21:00 -07:00
fuelSchedule1 . Status = OFF ;
2013-09-04 17:27:16 -07:00
fuelSchedule2 . Status = OFF ;
2013-11-13 22:17:58 -08:00
fuelSchedule3 . Status = OFF ;
2013-09-04 17:27:16 -07:00
//Ignition Schedules, which uses timer 5
TCCR5B = 0x00 ; //Disbale Timer3 while we set it up
TCNT5 = 0 ; //Reset Timer Count
2013-11-13 22:17:58 -08:00
TIFR5 = 0x00 ; //Timer5 INT Flag Reg: Clear Timer Overflow Flag
TCCR5A = 0x00 ; //Timer5 Control Reg A: Wave Gen Mode normal
2015-08-29 04:16:03 -07:00
//TCCR5B = (1 << CS12); //Timer5 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
2015-09-24 13:24:40 -07:00
TCCR5B = 0x03 ; //aka Divisor = 64 = 490.1Hz
2013-09-04 17:27:16 -07:00
ignitionSchedule1 . Status = OFF ;
ignitionSchedule2 . Status = OFF ;
2013-11-13 22:17:58 -08:00
ignitionSchedule3 . Status = OFF ;
//The remaining Schedules (Schedules 4 for fuel and ignition) use Timer4
TCCR4B = 0x00 ; //Disbale Timer4 while we set it up
TCNT4 = 0 ; //Reset Timer Count
TIFR4 = 0x00 ; //Timer4 INT Flag Reg: Clear Timer Overflow Flag
TCCR4A = 0x00 ; //Timer4 Control Reg A: Wave Gen Mode normal
2015-09-24 13:24:40 -07:00
TCCR4B = ( 1 < < CS12 ) ; //Timer4 Control Reg B: aka Divisor = 256 = 122.5HzTimer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
2013-11-13 22:17:58 -08:00
ignitionSchedule4 . Status = OFF ;
fuelSchedule4 . Status = OFF ;
2015-09-29 00:21:00 -07:00
//Note that timer4 compare channel C is used by the idle control
2013-07-10 04:18:18 -07:00
}
/*
2013-11-13 22:17:58 -08:00
These 8 function turn a schedule on , provides the time to start and the duration and gives it callback functions .
All 8 functions operate the same , just on different schedules
2013-07-10 04:18:18 -07:00
Args :
2013-09-04 17:27:16 -07:00
startCallback : The function to be called once the timeout is reached
2013-11-13 22:17:58 -08:00
timeout : The number of uS in the future that the startCallback should be triggered
2013-09-04 17:27:16 -07:00
duration : The number of uS after startCallback is called before endCallback is called
2013-09-03 05:26:08 -07:00
endCallback : This function is called once the duration time has been reached
2013-07-10 04:18:18 -07:00
*/
2013-09-04 17:27:16 -07:00
void setFuelSchedule1 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
if ( fuelSchedule1 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
2013-07-10 04:18:18 -07:00
//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))
2013-09-23 05:23:34 -07:00
//unsigned int absoluteTimeout = TCNT3 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
2015-09-29 00:21:00 -07:00
unsigned int absoluteTimeout = TCNT3 + ( timeout > > 4 ) ; //As above, but with bit shift instead of / 16
2015-09-29 16:27:37 -07:00
OCR3A = absoluteTimeout ;
2013-09-04 17:27:16 -07:00
fuelSchedule1 . duration = duration ;
fuelSchedule1 . StartCallback = startCallback ; //Name the start callback function
2014-01-09 23:54:33 -08:00
fuelSchedule1 . EndCallback = endCallback ; //Name the end callback function
2013-09-04 17:27:16 -07:00
fuelSchedule1 . Status = PENDING ; //Turn this schedule on
2015-02-06 02:19:16 -08:00
2015-09-29 00:21:00 -07:00
TIMSK3 | = ( 1 < < OCIE3A ) ; //Turn on the C compare unit (ie turn on the interrupt)
2013-07-10 04:18:18 -07:00
}
2013-09-04 17:27:16 -07:00
void setFuelSchedule2 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
{
if ( fuelSchedule2 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
2013-09-23 05:23:34 -07:00
//unsigned int absoluteTimeout = TCNT3 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
unsigned int absoluteTimeout = TCNT3 + ( timeout > > 4 ) ; //As above, but with bit shift instead of / 16
2013-09-04 17:27:16 -07:00
OCR3B = absoluteTimeout ; //Use the B copmare unit of timer 3
fuelSchedule2 . duration = duration ;
fuelSchedule2 . StartCallback = startCallback ; //Name the start callback function
2014-01-09 23:54:33 -08:00
fuelSchedule2 . EndCallback = endCallback ; //Name the end callback function
2013-09-04 17:27:16 -07:00
fuelSchedule2 . Status = PENDING ; //Turn this schedule on
TIMSK3 | = ( 1 < < OCIE3B ) ; //Turn on the B compare unit (ie turn on the interrupt)
}
2013-11-13 22:17:58 -08:00
void setFuelSchedule3 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
{
if ( fuelSchedule3 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
//unsigned int absoluteTimeout = TCNT3 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
unsigned int absoluteTimeout = TCNT3 + ( timeout > > 4 ) ; //As above, but with bit shift instead of / 16
OCR3C = absoluteTimeout ; //Use the C compare unit of timer 3
fuelSchedule3 . duration = duration ;
fuelSchedule3 . StartCallback = startCallback ; //Name the start callback function
2014-01-09 23:54:33 -08:00
fuelSchedule3 . EndCallback = endCallback ; //Name the end callback function
2013-11-13 22:17:58 -08:00
fuelSchedule3 . Status = PENDING ; //Turn this schedule on
TIMSK3 | = ( 1 < < OCIE3C ) ; //Turn on the C compare unit (ie turn on the interrupt)
}
void setFuelSchedule4 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) ) //Uses timer 4 compare B
{
if ( fuelSchedule4 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
//unsigned int absoluteTimeout = TCNT3 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
2015-08-29 04:16:03 -07:00
//unsigned int absoluteTimeout = TCNT4 + (timeout >> 4); //As above, but with bit shift instead of / 16
unsigned int absoluteTimeout = TCNT4 + ( timeout > > 2 ) ; //As above, but with bit shift instead of / 16
2013-11-13 22:17:58 -08:00
OCR4B = absoluteTimeout ; //Use the B compare unit of timer 4
fuelSchedule4 . duration = duration ;
fuelSchedule4 . StartCallback = startCallback ; //Name the start callback function
2014-01-09 23:54:33 -08:00
fuelSchedule4 . EndCallback = endCallback ; //Name the end callback function
2013-11-13 22:17:58 -08:00
fuelSchedule4 . Status = PENDING ; //Turn this schedule on
TIMSK4 | = ( 1 < < OCIE4B ) ; //Turn on the B compare unit (ie turn on the interrupt)
}
2013-09-04 17:27:16 -07:00
//Ignition schedulers use Timer 5
void setIgnitionSchedule1 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
{
if ( ignitionSchedule1 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
2013-09-23 05:23:34 -07:00
//unsigned int absoluteTimeout = TCNT5 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
2014-12-08 20:31:48 -08:00
//unsigned char sreg;
//sreg = SREG;
//noInterrupts();
2015-08-29 04:16:03 -07:00
//unsigned int absoluteTimeout = TCNT5 + (timeout >> 4); //As above, but with bit shift instead of / 16
unsigned int absoluteTimeout = TCNT5 + ( timeout > > 2 ) ; //As above, but with bit shift instead of / 16
2013-09-04 17:27:16 -07:00
OCR5A = absoluteTimeout ;
2014-12-08 20:31:48 -08:00
//SREG = sreg;
2013-09-04 17:27:16 -07:00
ignitionSchedule1 . duration = duration ;
ignitionSchedule1 . StartCallback = startCallback ; //Name the start callback function
ignitionSchedule1 . EndCallback = endCallback ; //Name the start callback function
ignitionSchedule1 . Status = PENDING ; //Turn this schedule on
TIMSK5 | = ( 1 < < OCIE5A ) ; //Turn on the A compare unit (ie turn on the interrupt)
}
void setIgnitionSchedule2 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
if ( ignitionSchedule2 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
2015-09-12 00:41:30 -07:00
//unsigned int absoluteTimeout = TCNT5 + (timeout >> 4 ); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
unsigned int absoluteTimeout = TCNT5 + ( timeout > > 2 ) ; //As above, but with bit shift instead of / 16 (high precision)
2013-09-04 17:27:16 -07:00
OCR5B = absoluteTimeout ;
ignitionSchedule2 . duration = duration ;
ignitionSchedule2 . StartCallback = startCallback ; //Name the start callback function
ignitionSchedule2 . EndCallback = endCallback ; //Name the start callback function
ignitionSchedule2 . Status = PENDING ; //Turn this schedule on
2013-09-19 13:41:36 -07:00
TIMSK5 | = ( 1 < < OCIE5B ) ; //Turn on the B compare unit (ie turn on the interrupt)
2013-07-10 04:18:18 -07:00
}
2013-11-13 22:17:58 -08:00
void setIgnitionSchedule3 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
{
if ( ignitionSchedule3 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
//unsigned int absoluteTimeout = TCNT5 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
2015-10-01 22:52:09 -07:00
//unsigned int absoluteTimeout = TCNT5 + (timeout >> 4); //As above, but with bit shift instead of / 16
unsigned int absoluteTimeout = TCNT5 + ( timeout > > 2 ) ; //As above, but with bit shift instead of / 16
2013-11-13 22:17:58 -08:00
OCR5C = absoluteTimeout ;
ignitionSchedule3 . duration = duration ;
ignitionSchedule3 . StartCallback = startCallback ; //Name the start callback function
ignitionSchedule3 . EndCallback = endCallback ; //Name the start callback function
ignitionSchedule3 . Status = PENDING ; //Turn this schedule on
TIMSK5 | = ( 1 < < OCIE5C ) ; //Turn on the C compare unit (ie turn on the interrupt)
}
void setIgnitionSchedule4 ( void ( * startCallback ) ( ) , unsigned long timeout , unsigned long duration , void ( * endCallback ) ( ) )
{
if ( ignitionSchedule4 . Status = = RUNNING ) { return ; } //Check that we're not already part way through a schedule
//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))
//unsigned int absoluteTimeout = TCNT5 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
unsigned int absoluteTimeout = TCNT4 + ( timeout > > 4 ) ; //As above, but with bit shift instead of / 16
OCR4A = absoluteTimeout ;
ignitionSchedule4 . duration = duration ;
ignitionSchedule4 . StartCallback = startCallback ; //Name the start callback function
ignitionSchedule4 . EndCallback = endCallback ; //Name the start callback function
ignitionSchedule4 . Status = PENDING ; //Turn this schedule on
TIMSK4 | = ( 1 < < OCIE4A ) ; //Turn on the C compare unit (ie turn on the interrupt)
}
2013-09-08 03:01:47 -07:00
2013-11-13 22:17:58 -08:00
//This function (All 8 ISR functions that are below) gets called when either the start time or the duration time are reached
2013-09-04 17:27:16 -07:00
//This calls the relevant callback function (startCallback or endCallback) depending on the status of the schedule.
//If the startCallback function is called, we put the scheduler into RUNNING state
2013-09-08 03:01:47 -07:00
//Timer3A (fuel schedule 1) Compare Vector
2015-09-29 00:21:00 -07:00
ISR ( TIMER3_COMPA_vect , ISR_NOBLOCK ) //fuelSchedule1
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
if ( fuelSchedule1 . Status = = PENDING ) //Check to see if this schedule is turn on
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
fuelSchedule1 . StartCallback ( ) ;
fuelSchedule1 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
2013-09-23 05:23:34 -07:00
//unsigned int absoluteTimeout = TCNT3 + (fuelSchedule1.duration / 16);
2015-09-29 00:21:00 -07:00
unsigned int absoluteTimeout = TCNT3 + ( fuelSchedule1 . duration > > 4 ) ; //Divide by 16
OCR3A = absoluteTimeout ;
2013-07-10 04:18:18 -07:00
}
2013-09-04 17:27:16 -07:00
else if ( fuelSchedule1 . Status = = RUNNING )
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
fuelSchedule1 . EndCallback ( ) ;
fuelSchedule1 . Status = OFF ; //Turn off the schedule
2015-09-29 00:21:00 -07:00
TIMSK3 & = ~ ( 1 < < OCIE3A ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
2013-07-10 04:18:18 -07:00
}
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER3_COMPB_vect , ISR_NOBLOCK ) //fuelSchedule2
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
if ( fuelSchedule2 . Status = = PENDING ) //Check to see if this schedule is turn on
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
fuelSchedule2 . StartCallback ( ) ;
fuelSchedule2 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
2013-09-23 05:23:34 -07:00
//unsigned int absoluteTimeout = TCNT3 + (fuelSchedule2.duration / 16);
unsigned int absoluteTimeout = TCNT3 + ( fuelSchedule2 . duration > > 4 ) ; //Divide by 16
2013-09-04 17:27:16 -07:00
OCR3B = absoluteTimeout ;
2013-07-10 04:18:18 -07:00
}
2013-09-04 17:27:16 -07:00
else if ( fuelSchedule2 . Status = = RUNNING )
2013-07-10 04:18:18 -07:00
{
2013-09-04 17:27:16 -07:00
fuelSchedule2 . EndCallback ( ) ;
fuelSchedule2 . Status = OFF ; //Turn off the schedule
TIMSK3 & = ~ ( 1 < < OCIE3B ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
2013-07-10 04:18:18 -07:00
}
2013-11-13 22:17:58 -08:00
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER3_COMPC_vect , ISR_NOBLOCK ) //fuelSchedule3
2013-11-13 22:17:58 -08:00
{
if ( fuelSchedule3 . Status = = PENDING ) //Check to see if this schedule is turn on
{
fuelSchedule3 . StartCallback ( ) ;
fuelSchedule3 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
//unsigned int absoluteTimeout = TCNT3 + (fuelSchedule2.duration / 16);
unsigned int absoluteTimeout = TCNT3 + ( fuelSchedule3 . duration > > 4 ) ; //Divide by 16
OCR3C = absoluteTimeout ;
}
else if ( fuelSchedule3 . Status = = RUNNING )
{
fuelSchedule3 . EndCallback ( ) ;
fuelSchedule3 . Status = OFF ; //Turn off the schedule
TIMSK3 & = ~ ( 1 < < OCIE3C ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
}
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER4_COMPB_vect , ISR_NOBLOCK ) //fuelSchedule4
2013-11-13 22:17:58 -08:00
{
if ( fuelSchedule4 . Status = = PENDING ) //Check to see if this schedule is turn on
{
fuelSchedule4 . StartCallback ( ) ;
fuelSchedule4 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
//unsigned int absoluteTimeout = TCNT3 + (fuelSchedule2.duration / 16);
unsigned int absoluteTimeout = TCNT4 + ( fuelSchedule4 . duration > > 4 ) ; //Divide by 16
OCR4B = absoluteTimeout ;
}
else if ( fuelSchedule4 . Status = = RUNNING )
{
fuelSchedule4 . EndCallback ( ) ;
fuelSchedule4 . Status = OFF ; //Turn off the schedule
TIMSK4 & = ~ ( 1 < < OCIE4B ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
}
2013-09-04 17:27:16 -07:00
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER5_COMPA_vect , ISR_NOBLOCK ) //ignitionSchedule1
2013-09-04 17:27:16 -07:00
{
if ( ignitionSchedule1 . Status = = PENDING ) //Check to see if this schedule is turn on
{
2015-08-22 18:36:18 -07:00
if ( ign1LastRev = = startRevolutions ) { return ; }
2013-09-04 17:27:16 -07:00
ignitionSchedule1 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
2015-02-09 05:04:03 -08:00
ignitionSchedule1 . startTime = currentLoopTime ;
2014-12-08 20:31:48 -08:00
ignitionSchedule1 . StartCallback ( ) ;
2015-08-29 04:16:03 -07:00
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule1.duration >> 4);
2015-08-22 18:36:18 -07:00
ign1LastRev = startRevolutions ;
2015-08-29 04:16:03 -07:00
unsigned int absoluteTimeout = TCNT5 + ( ignitionSchedule1 . duration > > 2 ) ; //Divide by 4
2013-09-04 17:27:16 -07:00
OCR5A = absoluteTimeout ;
}
else if ( ignitionSchedule1 . Status = = RUNNING )
{
2014-12-08 20:31:48 -08:00
ignitionSchedule1 . Status = OFF ; //Turn off the schedule
ignitionSchedule1 . EndCallback ( ) ;
2015-02-05 13:11:33 -08:00
ignitionCount + = 1 ; //Increment the igintion counter
2014-12-08 20:31:48 -08:00
TIMSK5 & = ~ ( 1 < < OCIE5A ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
2013-09-04 17:27:16 -07:00
}
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER5_COMPB_vect , ISR_NOBLOCK ) //ignitionSchedule2
2013-09-04 17:27:16 -07:00
{
if ( ignitionSchedule2 . Status = = PENDING ) //Check to see if this schedule is turn on
{
2015-08-22 18:36:18 -07:00
if ( ign2LastRev = = startRevolutions ) { return ; }
2013-09-04 17:27:16 -07:00
ignitionSchedule2 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
2015-02-09 05:04:03 -08:00
ignitionSchedule2 . startTime = currentLoopTime ;
2014-12-18 16:25:53 -08:00
ignitionSchedule2 . StartCallback ( ) ;
2015-08-22 18:36:18 -07:00
ign2LastRev = startRevolutions ;
2015-09-12 00:41:30 -07:00
unsigned int absoluteTimeout = TCNT5 + ( ignitionSchedule2 . duration > > 2 ) ;
2013-09-04 17:27:16 -07:00
OCR5B = absoluteTimeout ;
}
else if ( ignitionSchedule2 . Status = = RUNNING )
{
2014-12-18 16:25:53 -08:00
ignitionSchedule2 . Status = OFF ; //Turn off the schedule
ignitionSchedule2 . EndCallback ( ) ;
2015-02-05 13:11:33 -08:00
ignitionCount + = 1 ; //Increment the igintion counter
2014-12-18 16:25:53 -08:00
TIMSK5 & = ~ ( 1 < < OCIE5B ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
2013-09-04 17:27:16 -07:00
}
2013-11-13 22:17:58 -08:00
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER5_COMPC_vect , ISR_NOBLOCK ) //ignitionSchedule3
2013-11-13 22:17:58 -08:00
{
if ( ignitionSchedule3 . Status = = PENDING ) //Check to see if this schedule is turn on
{
2015-08-22 18:36:18 -07:00
if ( ign3LastRev = = startRevolutions ) { return ; }
2013-11-13 22:17:58 -08:00
ignitionSchedule3 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
2015-02-09 05:04:03 -08:00
ignitionSchedule3 . startTime = currentLoopTime ;
ignitionSchedule3 . StartCallback ( ) ;
2015-10-01 22:52:09 -07:00
unsigned int absoluteTimeout = TCNT5 + ( ignitionSchedule3 . duration > > 2 ) ;
2015-08-22 18:36:18 -07:00
ign3LastRev = startRevolutions ;
2013-11-13 22:17:58 -08:00
OCR5C = absoluteTimeout ;
}
else if ( ignitionSchedule3 . Status = = RUNNING )
{
ignitionSchedule3 . Status = OFF ; //Turn off the schedule
2015-02-05 13:11:33 -08:00
ignitionSchedule3 . EndCallback ( ) ;
ignitionCount + = 1 ; //Increment the igintion counter
2013-11-13 22:17:58 -08:00
TIMSK5 & = ~ ( 1 < < OCIE5C ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
}
}
2015-06-05 06:30:37 -07:00
ISR ( TIMER4_COMPA_vect , ISR_NOBLOCK ) //ignitionSchedule4
2013-11-13 22:17:58 -08:00
{
if ( ignitionSchedule4 . Status = = PENDING ) //Check to see if this schedule is turn on
{
2015-08-22 18:36:18 -07:00
if ( ign4LastRev = = startRevolutions ) { return ; }
2013-11-13 22:17:58 -08:00
ignitionSchedule4 . Status = RUNNING ; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
2015-02-09 05:04:03 -08:00
ignitionSchedule4 . startTime = currentLoopTime ;
ignitionSchedule4 . StartCallback ( ) ;
2013-11-13 22:17:58 -08:00
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration / 16);
unsigned int absoluteTimeout = TCNT4 + ( ignitionSchedule4 . duration > > 4 ) ; //Divide by 16
2015-08-22 18:36:18 -07:00
ign4LastRev = startRevolutions ;
2013-11-13 22:17:58 -08:00
OCR4A = absoluteTimeout ;
}
else if ( ignitionSchedule4 . Status = = RUNNING )
{
ignitionSchedule4 . Status = OFF ; //Turn off the schedule
2015-02-05 13:11:33 -08:00
ignitionSchedule4 . EndCallback ( ) ;
ignitionCount + = 1 ; //Increment the igintion counter
2015-08-22 18:36:18 -07:00
TIMSK4 & = ~ ( 1 < < OCIE4A ) ; //Turn off this output compare unit (This simply writes 0 to the OCIE4A bit of TIMSK4)
2013-11-13 22:17:58 -08:00
}
2013-07-10 04:18:18 -07:00
}