Allow variable idle PWM frequency

This commit is contained in:
Josh Stewart 2015-09-29 17:21:00 +10:00
parent 85d1d55bc6
commit 0630d85f39
5 changed files with 20 additions and 16 deletions

View File

@ -34,7 +34,7 @@ void initialiseAuxPWM()
vvt_pin_mask = digitalPinToBitMask(pinVVT_1);
boost_pwm_max_count = 1000000L / (16 * configPage3.boostFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle. The x2 is there because the frequency is stored at half value (in a byte)
vvt_pwm_max_count = 1000000L / (16 * configPage3.vvtFreq * 2);; //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle
vvt_pwm_max_count = 1000000L / (16 * configPage3.vvtFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle
TIMSK1 |= (1 << OCIE1A); //Turn on the A compare unit (ie turn on the interrupt)
TIMSK1 |= (1 << OCIE1B); //Turn on the B compare unit (ie turn on the interrupt)
}

View File

@ -268,7 +268,7 @@ struct config3 {
byte airDenRates[9];
byte boostFreq; //Frequency of the boost PWM valve
byte vvtFreq; //Frequency of the vvt PWM valve
byte unused47;
byte idleFreq;
byte unused48;
byte unused49;
byte unused50;

View File

@ -42,8 +42,8 @@ void initialiseIdle()
idle_pin_port = portOutputRegister(digitalPinToPort(pinIdle1));
idle_pin_mask = digitalPinToBitMask(pinIdle1);
idle_pwm_max_count = 512; //Timer3 ticks every 16us. 16 * 512 = 8192uS. 1000000/8192 = 122Hz
TIMSK3 |= (1 << OCIE3A); //Turn on the A compare unit (ie turn on the interrupt)
idle_pwm_max_count = 1000000L / (16 * configPage3.idleFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle. Note that the frequency is divided by 2 coming from TS to allow for up to 512hz
TIMSK4 |= (1 << OCIE4C); //Turn on the A compare unit (ie turn on the interrupt)
break;
case 3:
@ -207,18 +207,18 @@ void homeStepper()
}
//The interrupt to turn off the idle pwm
ISR(TIMER3_COMPA_vect)
ISR(TIMER4_COMPC_vect)
{
if (idle_pwm_state)
{
*idle_pin_port &= ~(idle_pin_mask); // Switch pin to low
OCR3A = TCNT3 + (idle_pwm_max_count - idle_pwm_cur_value);
OCR4C = TCNT4 + (idle_pwm_max_count - idle_pwm_cur_value);
idle_pwm_state = false;
}
else
{
*idle_pin_port |= (idle_pin_mask); // Switch pin high
OCR3A = TCNT3 + idle_pwm_cur_value;
OCR4A = TCNT4 + idle_pwm_cur_value;
idle_pwm_state = true;
}

View File

@ -305,8 +305,8 @@ page = 6
airDenRates = array, U08, 36, [9], "%", 1.0, 0.0, 0, 255, 0 ; Values for the air density correction curve
boostFreq = scalar, U08, 45, "Hz", 2.0, 0.0, 10, 511, 0
vvtFreq = scalar, U08, 46, "Hz", 2.0, 0.0, 10, 511, 0
unused49 = scalar, U08, 47, "RPM", 100.0, 0.0, 100, 25500, 0
unused49 = scalar, U08, 48, "RPM", 100.0, 0.0, 100, 25500, 0
idleFreq = scalar, U08, 47, "Hz", 2.0, 0.0, 10, 511, 0
unused48 = scalar, U08, 48, "RPM", 100.0, 0.0, 100, 25500, 0
unused49 = scalar, U08, 49, "RPM", 100.0, 0.0, 100, 25500, 0
unused50 = scalar, U08, 50, "RPM", 100.0, 0.0, 100, 25500, 0
unused51 = scalar, U08, 51, "RPM", 100.0, 0.0, 100, 25500, 0
@ -390,6 +390,9 @@ page = 8
requiresPowerCycle = iacAlgorithm
requiresPowerCycle = boostEnabled
requiresPowerCycle = vvtEnabled
requiresPowerCycle = boostFreq
requiresPowerCycle = vvtFreq
requiresPowerCycle = idleFreq
defaultValue = pinLayout, 1
defaultValue = TrigPattern, 0

View File

@ -19,6 +19,7 @@ void initialiseSchedulers()
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
//TCCR3B = 0x03; //Timer3 Control Reg B: Timer Prescaler set to 64. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
//Timer 3 compare channel A is reserved for idle control, therefore there are only 2 fuel channels here
fuelSchedule1.Status = OFF;
fuelSchedule2.Status = OFF;
fuelSchedule3.Status = OFF;
@ -41,7 +42,7 @@ void initialiseSchedulers()
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
ignitionSchedule4.Status = OFF;
fuelSchedule4.Status = OFF;
fuelSchedule1.Status = OFF; //Uses compare channel C
//Note that timer4 compare channel C is used by the idle control
}
/*
@ -60,14 +61,14 @@ void setFuelSchedule1(void (*startCallback)(), unsigned long timeout, unsigned l
//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 = TCNT4 + (timeout >> 4); //As above, but with bit shift instead of / 16
unsigned int absoluteTimeout = TCNT3 + (timeout >> 4); //As above, but with bit shift instead of / 16
OCR4C = absoluteTimeout;
fuelSchedule1.duration = duration;
fuelSchedule1.StartCallback = startCallback; //Name the start callback function
fuelSchedule1.EndCallback = endCallback; //Name the end callback function
fuelSchedule1.Status = PENDING; //Turn this schedule on
TIMSK4 |= (1 << OCIE4C); //Turn on the C compare unit (ie turn on the interrupt)
TIMSK3 |= (1 << OCIE3A); //Turn on the C compare unit (ie turn on the interrupt)
}
void setFuelSchedule2(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)())
{
@ -189,21 +190,21 @@ void setIgnitionSchedule4(void (*startCallback)(), unsigned long timeout, unsign
//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
//Timer3A (fuel schedule 1) Compare Vector
ISR(TIMER4_COMPC_vect, ISR_NOBLOCK) //fuelSchedule1
ISR(TIMER3_COMPA_vect, ISR_NOBLOCK) //fuelSchedule1
{
if (fuelSchedule1.Status == PENDING) //Check to see if this schedule is turn on
{
fuelSchedule1.StartCallback();
fuelSchedule1.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 + (fuelSchedule1.duration / 16);
unsigned int absoluteTimeout = TCNT4 + (fuelSchedule1.duration >> 4); //Divide by 16
OCR4C = absoluteTimeout;
unsigned int absoluteTimeout = TCNT3 + (fuelSchedule1.duration >> 4); //Divide by 16
OCR3A = absoluteTimeout;
}
else if (fuelSchedule1.Status == RUNNING)
{
fuelSchedule1.EndCallback();
fuelSchedule1.Status = OFF; //Turn off the schedule
TIMSK4 &= ~(1 << OCIE4C); //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
TIMSK3 &= ~(1 << OCIE3A); //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
}
}
ISR(TIMER3_COMPB_vect, ISR_NOBLOCK) //fuelSchedule2