/* Speeduino - Simple engine management for the Arduino Mega 2560 platform Copyright (C) Josh Stewart 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) 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 */ #include "timers.h" #include "globals.h" #include "sensors.h" #if defined(CORE_AVR) #include #endif void initialiseTimers() { #if defined(CORE_AVR) //AVR chips use the ISR for this //Configure Timer2 for our low-freq interrupt code. 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 TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag TIMSK2 = 0x01; //Timer2 Set Overflow Interrupt enabled. TCCR2A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal /* Now configure the prescaler to CPU clock divided by 128 = 125Khz */ TCCR2B |= (1<= 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) { if(flexCounter < 50) { 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; } else if (flexCounter > 151) //1 pulse buffer { if(flexCounter < 169) { currentStatus.ethanolPct = 100; flexCounter = 0; } else { //This indicates an error condition. Spec of the sensor is that errors are above 170Hz) currentStatus.ethanolPct = 0; 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. flexCounter = 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 #endif }