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
TIFR5=0x00;//Timer5 INT Flag Reg: Clear Timer Overflow Flag
TCCR5A=0x00;//Timer5 Control Reg A: Wave Gen Mode normal
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
//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
TCCR4B=(1<<CS12);//Timer4 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
//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
unsignedintabsoluteTimeout=TCNT3+(timeout>>4);//As above, but with bit shift instead of / 16
//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
unsignedintabsoluteTimeout=TCNT3+(timeout>>4);//As above, but with bit shift instead of / 16
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
unsignedintabsoluteTimeout=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
fuelSchedule3.Status=PENDING;//Turn this schedule on
TIMSK3|=(1<<OCIE3C);//Turn on the C compare unit (ie turn on the interrupt)
}
voidsetFuelSchedule4(void(*startCallback)(),unsignedlongtimeout,unsignedlongduration,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
unsignedintabsoluteTimeout=TCNT4+(timeout>>4);//As above, but with bit shift instead of / 16
OCR4B=absoluteTimeout;//Use the B compare unit of timer 4
fuelSchedule4.duration=duration;
fuelSchedule4.StartCallback=startCallback;//Name the start callback function
//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 = 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
unsignedintabsoluteTimeout=TCNT5+(timeout>>4);//As above, but with bit shift instead of / 16
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
unsignedintabsoluteTimeout=TCNT5+(timeout>>4);//As above, but with bit shift instead of / 16
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)
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
unsignedintabsoluteTimeout=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)