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
fuelSchedule1.Status=OFF;
fuelSchedule2.Status=OFF;
//Ignition Schedules, which uses timer 5
TCCR5B=0x00;//Disbale Timer3 while we set it up
TCNT5=0;//Reset Timer Count
TIFR5=0x00;//Timer3 INT Flag Reg: Clear Timer Overflow Flag
TCCR5A=0x00;//Timer3 Control Reg A: Wave Gen Mode normal
TCCR5B=(1<<CS12);//Timer3 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
unsignedintabsoluteTimeout=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
OCR3A=absoluteTimeout;
fuelSchedule1.duration=duration;
fuelSchedule1.StartCallback=startCallback;//Name the start callback function
fuelSchedule1.EndCallback=endCallback;//Name the start callback function
fuelSchedule1.Status=PENDING;//Turn this schedule on
TIMSK3|=(1<<OCIE3A);//Turn on the A compare unit (ie turn on the interrupt)
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))
unsignedintabsoluteTimeout=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
OCR3B=absoluteTimeout;//Use the B copmare unit of timer 3
fuelSchedule2.duration=duration;
fuelSchedule2.StartCallback=startCallback;//Name the start callback function
fuelSchedule2.EndCallback=endCallback;//Name the start callback function
fuelSchedule2.Status=PENDING;//Turn this schedule on
TIMSK3|=(1<<OCIE3B);//Turn on the B compare unit (ie turn on the interrupt)
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))
unsignedintabsoluteTimeout=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
OCR5A=absoluteTimeout;
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)
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))
unsignedintabsoluteTimeout=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
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
TIMSK5|=(1<<OCIE5B);//Turn on the A compare unit (ie turn on the interrupt)