|
|
@ -1,209 +1,212 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
Speeduino - Simple engine management for the Arduino Mega 2560 platform
|
|
|
|
Speeduino - Simple engine management for the Arduino Mega 2560 platform
|
|
|
|
Copyright (C) Josh Stewart
|
|
|
|
Copyright (C) Josh Stewart
|
|
|
|
A full copy of the license may be found in the projects root directory
|
|
|
|
A full copy of the license may be found in the projects root directory
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
Timers are used for having actions performed repeatedly at a fixed interval (Eg every 100ms)
|
|
|
|
Timers are used for having actions performed repeatedly at a fixed interval (Eg every 100ms)
|
|
|
|
They should not be confused with Schedulers, which are for performing an action once at a given point of time in the future
|
|
|
|
They should not be confused with Schedulers, which are for performing an action once at a given point of time in the future
|
|
|
|
|
|
|
|
|
|
|
|
Timers are typically low resolution (Compared to Schedulers), with maximum frequency currently being approximately every 10ms
|
|
|
|
Timers are typically low resolution (Compared to Schedulers), with maximum frequency currently being approximately every 10ms
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
#include "timers.h"
|
|
|
|
#include "timers.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "sensors.h"
|
|
|
|
#include "sensors.h"
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(CORE_AVR)
|
|
|
|
#if defined(CORE_AVR)
|
|
|
|
#include <avr/wdt.h>
|
|
|
|
#include <avr/wdt.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void initialiseTimers()
|
|
|
|
void initialiseTimers()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#if defined(CORE_AVR) //AVR chips use the ISR for this
|
|
|
|
#if defined(CORE_AVR) //AVR chips use the ISR for this
|
|
|
|
//Configure Timer2 for our low-freq interrupt code.
|
|
|
|
//Configure Timer2 for our low-freq interrupt code.
|
|
|
|
TCCR2B = 0x00; //Disbale Timer2 while we set it up
|
|
|
|
TCCR2B = 0x00; //Disbale Timer2 while we set it up
|
|
|
|
TCNT2 = 131; //Preload timer2 with 131 cycles, leaving 125 till overflow. As the timer runs at 125Khz, this causes overflow to occur at 1Khz = 1ms
|
|
|
|
TCNT2 = 131; //Preload timer2 with 131 cycles, leaving 125 till overflow. As the timer runs at 125Khz, this causes overflow to occur at 1Khz = 1ms
|
|
|
|
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
|
|
|
|
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
|
|
|
|
TIMSK2 = 0x01; //Timer2 Set Overflow Interrupt enabled.
|
|
|
|
TIMSK2 = 0x01; //Timer2 Set Overflow Interrupt enabled.
|
|
|
|
TCCR2A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
|
|
|
|
TCCR2A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
|
|
|
|
/* Now configure the prescaler to CPU clock divided by 128 = 125Khz */
|
|
|
|
/* Now configure the prescaler to CPU clock divided by 128 = 125Khz */
|
|
|
|
TCCR2B |= (1<<CS22) | (1<<CS20); // Set bits
|
|
|
|
TCCR2B |= (1<<CS22) | (1<<CS20); // Set bits
|
|
|
|
TCCR2B &= ~(1<<CS21); // Clear bit
|
|
|
|
TCCR2B &= ~(1<<CS21); // Clear bit
|
|
|
|
|
|
|
|
|
|
|
|
//Enable the watchdog timer for 2 second resets (Good reference: https://tushev.org/articles/arduino/5/arduino-and-watchdog-timer)
|
|
|
|
//Enable the watchdog timer for 2 second resets (Good reference: https://tushev.org/articles/arduino/5/arduino-and-watchdog-timer)
|
|
|
|
//Boooooooooo WDT is currently broken on Mega 2560 bootloaders :(
|
|
|
|
//Boooooooooo WDT is currently broken on Mega 2560 bootloaders :(
|
|
|
|
//wdt_enable(WDTO_2S);
|
|
|
|
//wdt_enable(WDTO_2S);
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined (CORE_TEENSY)
|
|
|
|
#elif defined (CORE_TEENSY)
|
|
|
|
//Uses the PIT timer on Teensy.
|
|
|
|
//Uses the PIT timer on Teensy.
|
|
|
|
lowResTimer.begin(oneMSInterval, 1000);
|
|
|
|
lowResTimer.begin(oneMSInterval, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined(CORE_STM32)
|
|
|
|
#elif defined(CORE_STM32)
|
|
|
|
Timer4.setPeriod(1000); // Set up period
|
|
|
|
Timer4.setPeriod(1000); // Set up period
|
|
|
|
// Set up an interrupt
|
|
|
|
// Set up an interrupt
|
|
|
|
Timer4.setMode(1, TIMER_OUTPUT_COMPARE);
|
|
|
|
Timer4.setMode(1, TIMER_OUTPUT_COMPARE);
|
|
|
|
Timer4.attachInterrupt(1, oneMSInterval);
|
|
|
|
Timer4.attachInterrupt(1, oneMSInterval);
|
|
|
|
Timer4.resume(); //Start Timer
|
|
|
|
Timer4.resume(); //Start Timer
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
dwellLimit_uS = (1000 * configPage2.dwellLimit);
|
|
|
|
#if defined(CORE_STM32)
|
|
|
|
lastRPM_100ms = 0;
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
dwellLimit_uS = (1000 * configPage2.dwellLimit);
|
|
|
|
|
|
|
|
lastRPM_100ms = 0;
|
|
|
|
//Timer2 Overflow Interrupt Vector, called when the timer overflows.
|
|
|
|
}
|
|
|
|
//Executes every ~1ms.
|
|
|
|
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //AVR chips use the ISR for this
|
|
|
|
|
|
|
|
ISR(TIMER2_OVF_vect, ISR_NOBLOCK)
|
|
|
|
//Timer2 Overflow Interrupt Vector, called when the timer overflows.
|
|
|
|
#elif defined (CORE_TEENSY) || defined(CORE_STM32)
|
|
|
|
//Executes every ~1ms.
|
|
|
|
void oneMSInterval() //Most ARM chips can simply call a function
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //AVR chips use the ISR for this
|
|
|
|
#endif
|
|
|
|
ISR(TIMER2_OVF_vect, ISR_NOBLOCK)
|
|
|
|
{
|
|
|
|
#elif defined (CORE_TEENSY) || defined(CORE_STM32)
|
|
|
|
|
|
|
|
void oneMSInterval() //Most ARM chips can simply call a function
|
|
|
|
//Increment Loop Counters
|
|
|
|
#endif
|
|
|
|
loop33ms++;
|
|
|
|
{
|
|
|
|
loop66ms++;
|
|
|
|
|
|
|
|
loop100ms++;
|
|
|
|
//Increment Loop Counters
|
|
|
|
loop250ms++;
|
|
|
|
loop33ms++;
|
|
|
|
loopSec++;
|
|
|
|
loop66ms++;
|
|
|
|
|
|
|
|
loop100ms++;
|
|
|
|
unsigned long targetOverdwellTime;
|
|
|
|
loop250ms++;
|
|
|
|
|
|
|
|
loopSec++;
|
|
|
|
//Overdwell check
|
|
|
|
|
|
|
|
targetOverdwellTime = micros() - dwellLimit_uS; //Set a target time in the past that all coil charging must have begun after. If the coil charge began before this time, it's been running too long
|
|
|
|
unsigned long targetOverdwellTime;
|
|
|
|
bool isCrankLocked = configPage2.ignCranklock && (currentStatus.RPM < currentStatus.crankRPM); //Dwell limiter is disabled during cranking on setups using the locked cranking timing. WE HAVE to do the RPM check here as relying on the engine cranking bit can be potentially too slow in updating
|
|
|
|
|
|
|
|
//Check first whether each spark output is currently on. Only check it's dwell time if it is
|
|
|
|
//Overdwell check
|
|
|
|
|
|
|
|
targetOverdwellTime = micros() - dwellLimit_uS; //Set a target time in the past that all coil charging must have begun after. If the coil charge began before this time, it's been running too long
|
|
|
|
if(ignitionSchedule1.Status == RUNNING) { if( (ignitionSchedule1.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil1Charge(); } }
|
|
|
|
bool isCrankLocked = configPage2.ignCranklock && (currentStatus.RPM < currentStatus.crankRPM); //Dwell limiter is disabled during cranking on setups using the locked cranking timing. WE HAVE to do the RPM check here as relying on the engine cranking bit can be potentially too slow in updating
|
|
|
|
if(ignitionSchedule2.Status == RUNNING) { if( (ignitionSchedule2.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil2Charge(); } }
|
|
|
|
//Check first whether each spark output is currently on. Only check it's dwell time if it is
|
|
|
|
if(ignitionSchedule3.Status == RUNNING) { if( (ignitionSchedule3.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil3Charge(); } }
|
|
|
|
|
|
|
|
if(ignitionSchedule4.Status == RUNNING) { if( (ignitionSchedule4.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil4Charge(); } }
|
|
|
|
if(ignitionSchedule1.Status == RUNNING) { if( (ignitionSchedule1.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil1Charge(); } }
|
|
|
|
if(ignitionSchedule5.Status == RUNNING) { if( (ignitionSchedule5.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil5Charge(); } }
|
|
|
|
if(ignitionSchedule2.Status == RUNNING) { if( (ignitionSchedule2.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil2Charge(); } }
|
|
|
|
|
|
|
|
if(ignitionSchedule3.Status == RUNNING) { if( (ignitionSchedule3.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil3Charge(); } }
|
|
|
|
//15Hz loop
|
|
|
|
if(ignitionSchedule4.Status == RUNNING) { if( (ignitionSchedule4.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil4Charge(); } }
|
|
|
|
if (loop66ms == 66)
|
|
|
|
if(ignitionSchedule5.Status == RUNNING) { if( (ignitionSchedule5.startTime < targetOverdwellTime) && (configPage2.useDwellLim) && (isCrankLocked != true) ) { endCoil5Charge(); } }
|
|
|
|
{
|
|
|
|
|
|
|
|
loop66ms = 0;
|
|
|
|
//15Hz loop
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_15HZ);
|
|
|
|
if (loop66ms == 66)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
loop66ms = 0;
|
|
|
|
//30Hz loop
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_15HZ);
|
|
|
|
if (loop33ms == 33)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
loop33ms = 0;
|
|
|
|
//30Hz loop
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_30HZ);
|
|
|
|
if (loop33ms == 33)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
loop33ms = 0;
|
|
|
|
//Loop executed every 100ms loop
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_30HZ);
|
|
|
|
//Anything inside this if statement will run every 100ms.
|
|
|
|
}
|
|
|
|
if (loop100ms == 100)
|
|
|
|
|
|
|
|
{
|
|
|
|
//Loop executed every 100ms loop
|
|
|
|
loop100ms = 0; //Reset counter
|
|
|
|
//Anything inside this if statement will run every 100ms.
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_10HZ);
|
|
|
|
if (loop100ms == 100)
|
|
|
|
#if defined(CORE_STM32) //debug purpose, only visal for running code
|
|
|
|
{
|
|
|
|
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
|
|
|
loop100ms = 0; //Reset counter
|
|
|
|
#endif
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_10HZ);
|
|
|
|
|
|
|
|
#if defined(CORE_STM32) //debug purpose, only visal for running code
|
|
|
|
currentStatus.rpmDOT = (currentStatus.RPM - lastRPM_100ms) * 10; //This is the RPM per second that the engine has accelerated/decelleratedin the last loop
|
|
|
|
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
|
|
|
lastRPM_100ms = currentStatus.RPM; //Record the current RPM for next calc
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
currentStatus.rpmDOT = (currentStatus.RPM - lastRPM_100ms) * 10; //This is the RPM per second that the engine has accelerated/decelleratedin the last loop
|
|
|
|
//Loop executed every 250ms loop (1ms x 250 = 250ms)
|
|
|
|
lastRPM_100ms = currentStatus.RPM; //Record the current RPM for next calc
|
|
|
|
//Anything inside this if statement will run every 250ms.
|
|
|
|
}
|
|
|
|
if (loop250ms == 250)
|
|
|
|
|
|
|
|
{
|
|
|
|
//Loop executed every 250ms loop (1ms x 250 = 250ms)
|
|
|
|
loop250ms = 0; //Reset Counter
|
|
|
|
//Anything inside this if statement will run every 250ms.
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_4HZ);
|
|
|
|
if (loop250ms == 250)
|
|
|
|
|
|
|
|
{
|
|
|
|
#if defined(CORE_AVR)
|
|
|
|
loop250ms = 0; //Reset Counter
|
|
|
|
//Reset watchdog timer (Not active currently)
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_4HZ);
|
|
|
|
//wdt_reset();
|
|
|
|
|
|
|
|
//DIY watchdog
|
|
|
|
#if defined(CORE_AVR)
|
|
|
|
if( (initialisationComplete == true) && (last250msLoopCount == mainLoopCount) ) { setup(); } //This is a sign of a crash.
|
|
|
|
//Reset watchdog timer (Not active currently)
|
|
|
|
else { last250msLoopCount = mainLoopCount; }
|
|
|
|
//wdt_reset();
|
|
|
|
#endif
|
|
|
|
//DIY watchdog
|
|
|
|
}
|
|
|
|
if( (initialisationComplete == true) && (last250msLoopCount == mainLoopCount) ) { setup(); } //This is a sign of a crash.
|
|
|
|
|
|
|
|
else { last250msLoopCount = mainLoopCount; }
|
|
|
|
//Loop executed every 1 second (1ms x 1000 = 1000ms)
|
|
|
|
#endif
|
|
|
|
if (loopSec == 1000)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
loopSec = 0; //Reset counter.
|
|
|
|
//Loop executed every 1 second (1ms x 1000 = 1000ms)
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_1HZ);
|
|
|
|
if (loopSec == 1000)
|
|
|
|
|
|
|
|
{
|
|
|
|
dwellLimit_uS = (1000 * configPage2.dwellLimit); //Update uS value incase setting has changed
|
|
|
|
loopSec = 0; //Reset counter.
|
|
|
|
currentStatus.crankRPM = ((unsigned int)configPage2.crankRPM * 100);
|
|
|
|
BIT_SET(TIMER_mask, BIT_TIMER_1HZ);
|
|
|
|
|
|
|
|
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
dwellLimit_uS = (1000 * configPage2.dwellLimit); //Update uS value incase setting has changed
|
|
|
|
//This updates the runSecs variable
|
|
|
|
currentStatus.crankRPM = ((unsigned int)configPage2.crankRPM * 100);
|
|
|
|
//If the engine is running or cranking, we need ot update the run time counter.
|
|
|
|
|
|
|
|
if (BIT_CHECK(currentStatus.engine, BIT_ENGINE_RUN))
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
{ //NOTE - There is a potential for a ~1sec gap between engine crank starting and ths runSec number being incremented. This may delay ASE!
|
|
|
|
//This updates the runSecs variable
|
|
|
|
if (currentStatus.runSecs <= 254) //Ensure we cap out at 255 and don't overflow. (which would reset ASE)
|
|
|
|
//If the engine is running or cranking, we need ot update the run time counter.
|
|
|
|
{ currentStatus.runSecs++; } //Increment our run counter by 1 second.
|
|
|
|
if (BIT_CHECK(currentStatus.engine, BIT_ENGINE_RUN))
|
|
|
|
}
|
|
|
|
{ //NOTE - There is a potential for a ~1sec gap between engine crank starting and ths runSec number being incremented. This may delay ASE!
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
if (currentStatus.runSecs <= 254) //Ensure we cap out at 255 and don't overflow. (which would reset ASE)
|
|
|
|
//This records the number of main loops the system has completed in the last second
|
|
|
|
{ currentStatus.runSecs++; } //Increment our run counter by 1 second.
|
|
|
|
currentStatus.loopsPerSecond = mainLoopCount;
|
|
|
|
}
|
|
|
|
mainLoopCount = 0;
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
//This records the number of main loops the system has completed in the last second
|
|
|
|
//increament secl (secl is simply a counter that increments every second and is used to track whether the system has unexpectedly reset
|
|
|
|
currentStatus.loopsPerSecond = mainLoopCount;
|
|
|
|
currentStatus.secl++;
|
|
|
|
mainLoopCount = 0;
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
//Check the fan output status
|
|
|
|
//increament secl (secl is simply a counter that increments every second and is used to track whether the system has unexpectedly reset
|
|
|
|
if (configPage4.fanEnable == 1)
|
|
|
|
currentStatus.secl++;
|
|
|
|
{
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
fanControl(); // Fucntion to turn the cooling fan on/off
|
|
|
|
//Check the fan output status
|
|
|
|
}
|
|
|
|
if (configPage4.fanEnable == 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
//Check whether fuel pump priming is complete
|
|
|
|
fanControl(); // Fucntion to turn the cooling fan on/off
|
|
|
|
if(!fpPrimed)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
if(currentStatus.secl >= configPage1.fpPrime)
|
|
|
|
//Check whether fuel pump priming is complete
|
|
|
|
{
|
|
|
|
if(!fpPrimed)
|
|
|
|
fpPrimed = true; //Mark the priming as being completed
|
|
|
|
{
|
|
|
|
if(currentStatus.RPM == 0) { digitalWrite(pinFuelPump, LOW); fuelPumpOn = false; } //If we reach here then the priming is complete, however only turn off the fuel pump if the engine isn't running
|
|
|
|
if(currentStatus.secl >= configPage1.fpPrime)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
fpPrimed = true; //Mark the priming as being completed
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
if(currentStatus.RPM == 0) { digitalWrite(pinFuelPump, LOW); fuelPumpOn = false; } //If we reach here then the priming is complete, however only turn off the fuel pump if the engine isn't running
|
|
|
|
//Set the flex reading (if enabled). The flexCounter is updated with every pulse from the sensor. If cleared once per second, we get a frequency reading
|
|
|
|
}
|
|
|
|
if(configPage1.flexEnabled == true)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
//**************************************************************************************************************************************************
|
|
|
|
if(flexCounter < 50)
|
|
|
|
//Set the flex reading (if enabled). The flexCounter is updated with every pulse from the sensor. If cleared once per second, we get a frequency reading
|
|
|
|
{
|
|
|
|
if(configPage1.flexEnabled == true)
|
|
|
|
currentStatus.ethanolPct = 0; //Standard GM Continental sensor reads from 50Hz (0 ethanol) to 150Hz (Pure ethanol). Subtracting 50 from the frequency therefore gives the ethanol percentage.
|
|
|
|
{
|
|
|
|
flexCounter = 0;
|
|
|
|
if(flexCounter < 50)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
else if (flexCounter > 151) //1 pulse buffer
|
|
|
|
currentStatus.ethanolPct = 0; //Standard GM Continental sensor reads from 50Hz (0 ethanol) to 150Hz (Pure ethanol). Subtracting 50 from the frequency therefore gives the ethanol percentage.
|
|
|
|
{
|
|
|
|
flexCounter = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
if(flexCounter < 169)
|
|
|
|
else if (flexCounter > 151) //1 pulse buffer
|
|
|
|
{
|
|
|
|
{
|
|
|
|
currentStatus.ethanolPct = 100;
|
|
|
|
|
|
|
|
flexCounter = 0;
|
|
|
|
if(flexCounter < 169)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
else
|
|
|
|
currentStatus.ethanolPct = 100;
|
|
|
|
{
|
|
|
|
flexCounter = 0;
|
|
|
|
//This indicates an error condition. Spec of the sensor is that errors are above 170Hz)
|
|
|
|
}
|
|
|
|
currentStatus.ethanolPct = 0;
|
|
|
|
else
|
|
|
|
flexCounter = 0;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
//This indicates an error condition. Spec of the sensor is that errors are above 170Hz)
|
|
|
|
}
|
|
|
|
currentStatus.ethanolPct = 0;
|
|
|
|
else
|
|
|
|
flexCounter = 0;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
currentStatus.ethanolPct = flexCounter - 50; //Standard GM Continental sensor reads from 50Hz (0 ethanol) to 150Hz (Pure ethanol). Subtracting 50 from the frequency therefore gives the ethanol percentage.
|
|
|
|
}
|
|
|
|
flexCounter = 0;
|
|
|
|
else
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
currentStatus.ethanolPct = flexCounter - 50; //Standard GM Continental sensor reads from 50Hz (0 ethanol) to 150Hz (Pure ethanol). Subtracting 50 from the frequency therefore gives the ethanol percentage.
|
|
|
|
//Off by 1 error check
|
|
|
|
flexCounter = 0;
|
|
|
|
if (currentStatus.ethanolPct == 1) { currentStatus.ethanolPct = 0; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
//Off by 1 error check
|
|
|
|
|
|
|
|
if (currentStatus.ethanolPct == 1) { currentStatus.ethanolPct = 0; }
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //AVR chips use the ISR for this
|
|
|
|
}
|
|
|
|
//Reset Timer2 to trigger in another ~1ms
|
|
|
|
|
|
|
|
TCNT2 = 131; //Preload timer2 with 100 cycles, leaving 156 till overflow.
|
|
|
|
}
|
|
|
|
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //AVR chips use the ISR for this
|
|
|
|
#endif
|
|
|
|
//Reset Timer2 to trigger in another ~1ms
|
|
|
|
|
|
|
|
TCNT2 = 131; //Preload timer2 with 100 cycles, leaving 156 till overflow.
|
|
|
|
|
|
|
|
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|