Fix timer buffer overflow at low RPM

This commit is contained in:
Josh Stewart 2015-08-22 16:24:49 +10:00
parent 4c669423b4
commit ae7b355c3e
2 changed files with 16 additions and 13 deletions

View File

@ -27,8 +27,8 @@ void initialiseSchedulers()
TCNT5 = 0; //Reset Timer Count
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
TCCR5B = 0x03;
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
//TCCR5B = 0x03; //Reverting this for now due to issue with low RPM overflow
ignitionSchedule1.Status = OFF;
ignitionSchedule2.Status = OFF;
ignitionSchedule3.Status = OFF;
@ -124,7 +124,8 @@ void setIgnitionSchedule1(void (*startCallback)(), unsigned long timeout, unsign
//unsigned char sreg;
//sreg = SREG;
//noInterrupts();
unsigned int absoluteTimeout = TCNT5 + (timeout >> 2); //As above, but with bit shift instead of / 16
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
OCR5A = absoluteTimeout;
//SREG = sreg;
ignitionSchedule1.duration = duration;
@ -139,8 +140,8 @@ void setIgnitionSchedule2(void (*startCallback)(), unsigned long timeout, unsign
//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 = TCNT5 + (timeout >> 2); //As above, but with bit shift instead of / 16
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
OCR5B = absoluteTimeout;
ignitionSchedule2.duration = duration;
ignitionSchedule2.StartCallback = startCallback; //Name the start callback function
@ -155,7 +156,8 @@ void setIgnitionSchedule3(void (*startCallback)(), unsigned long timeout, unsign
//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 = TCNT5 + (timeout >> 2); //As above, but with bit shift instead of / 16
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
OCR5C = absoluteTimeout;
ignitionSchedule3.duration = duration;
ignitionSchedule3.StartCallback = startCallback; //Name the start callback function
@ -260,8 +262,8 @@ ISR(TIMER5_COMPA_vect, ISR_NOBLOCK) //ignitionSchedule1
ignitionSchedule1.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
ignitionSchedule1.startTime = currentLoopTime;
ignitionSchedule1.StartCallback();
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule1.duration / 4);
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule1.duration >> 2); //Divide by 4
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule1.duration >> 4);
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule1.duration >> 2); //Divide by 4
OCR5A = absoluteTimeout;
}
else if (ignitionSchedule1.Status == RUNNING)
@ -279,8 +281,8 @@ ISR(TIMER5_COMPB_vect, ISR_NOBLOCK) //ignitionSchedule2
ignitionSchedule2.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
ignitionSchedule2.startTime = currentLoopTime;
ignitionSchedule2.StartCallback();
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration / 4);
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration >> 2); //Divide by 4
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration >> 4);
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration >> 2); //Divide by 4
OCR5B = absoluteTimeout;
}
else if (ignitionSchedule2.Status == RUNNING)
@ -298,8 +300,8 @@ ISR(TIMER5_COMPC_vect, ISR_NOBLOCK) //ignitionSchedule3
ignitionSchedule3.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
ignitionSchedule3.startTime = currentLoopTime;
ignitionSchedule3.StartCallback();
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration / 4);
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule3.duration >> 2); //Divide by 4
unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule2.duration >> 4);
//unsigned int absoluteTimeout = TCNT5 + (ignitionSchedule3.duration >> 2); //Divide by 4
OCR5C = absoluteTimeout;
}
else if (ignitionSchedule3.Status == RUNNING)
@ -326,6 +328,6 @@ ISR(TIMER4_COMPA_vect, ISR_NOBLOCK) //ignitionSchedule4
ignitionSchedule4.Status = OFF; //Turn off the schedule
ignitionSchedule4.EndCallback();
ignitionCount += 1; //Increment the igintion counter
TIMSK5 &= ~(1 << OCIE4A); //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
TIMSK4 &= ~(1 << OCIE4A); //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
}
}

View File

@ -563,6 +563,7 @@ void loop()
unsigned int pwLimit = percentage(configPage1.dutyLim, revolutionTime); //The pulsewidth limit is determined to be the duty cycle limit (Eg 85%) by the total time it takes to perform 1 revolution
if (currentStatus.PW > pwLimit) { currentStatus.PW = pwLimit; }
//***********************************************************************************************
//BEGIN INJECTION TIMING
//Determine next firing angles