Move ADC interrupt setup into its own initialise function (+minor cleanups)

This commit is contained in:
Josh Stewart 2016-11-18 12:57:11 +11:00
parent 7ca41a7355
commit 082010a07d
4 changed files with 53 additions and 49 deletions

View File

@ -1,12 +0,0 @@
#ifndef FASTANALOG_H
#define FASTANALOG_H
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#endif // FASTANALOG_H

View File

@ -12,16 +12,10 @@
#define BARO_MIN 87
#define BARO_MAX 108
volatile byte flexCounter = 0;
#define ANALOG_ISR
#define ANALOG_ISR //Comment this line to disable the ADC interrupt routine
#if defined(ANALOG_ISR)
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
int AnChannel[7];
#else
int AnChannel[15];
#endif
#endif
volatile byte flexCounter = 0;
volatile int AnChannel[15];
/*
* Simple low pass IIR filter macro for the analog inputs
@ -42,8 +36,10 @@ ISR(ADC_vect)
{
byte nChannel;
int result = ADCL | (ADCH << 8);
//ADCSRA = 0x6E; // ADC disabled by clearing bit 7(ADEN)
//BIT_CLEAR(ADCSRA, ADIE);
ADCSRA = 0x6E; // ADC disabled by clearing bit 7(ADEN)
nChannel = ADMUX & 0x07;
#if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
if (nChannel==7) { ADMUX = 0x40; }
@ -61,9 +57,10 @@ ISR(ADC_vect)
}
#endif
else { ADMUX++; }
AnChannel[nChannel] = result;
ADCSRA = 0xEE; // ADC Interrupt Flag enabled
AnChannel[nChannel-1] = result;
//BIT_SET(ADCSRA, ADIE);
//ADCSRA = 0xEE; // ADC Interrupt Flag enabled
}
#endif

View File

@ -4,6 +4,48 @@ Copyright (C) Josh Stewart
A full copy of the license may be found in the projects root directory
*/
void initialiseADC()
{
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this
#if defined(ANALOG_ISR)
//This sets the ADC (Analog to Digitial Converter) to run at 250KHz, greatly reducing analog read times (MAP/TPS)
//the code on ISR run each conversion every 25 ADC clock, conversion run about 100KHz effectively
//making a 6250 conversions/s on 16 channels and 12500 on 8 channels devices.
noInterrupts(); //Interrupts should be turned off when playing with any of these registers
ADCSRB = 0x00; //ADC Auto Trigger Source is in Free Running mode
//ADCSRB &= B11111000;
ADMUX = 0x40; //Select AREF as reference, ADC Left Adjust Result, Starting at channel 0
//All of the below is the longhand version of: ADCSRA = 0xEE;
#define ADFR 5 //Why the HELL isn't this defined in the same place as everything else (wiring.h)?!?!
BIT_SET(ADCSRA,ADFR); //Set free running mode
BIT_SET(ADCSRA,ADIE); //Set ADC interrupt enabled
BIT_CLEAR(ADCSRA,ADIF); //Clear interrupt flag
// Set ADC clock to 250KHz (Prescaler = 64)
BIT_SET(ADCSRA,ADPS2);
BIT_SET(ADCSRA,ADPS1);
BIT_SET(ADCSRA,ADPS0);
BIT_SET(ADCSRA,ADEN); //Enable ADC
interrupts();
BIT_SET(ADCSRA,ADSC); //Start conversion
#else
//This sets the ADC (Analog to Digitial Converter) to run at 1Mhz, greatly reducing analog read times (MAP/TPS)
//1Mhz is the fastest speed permitted by the CPU without affecting accuracy
//Please see chapter 11 of 'Practical Arduino' (http://books.google.com.au/books?id=HsTxON1L6D4C&printsec=frontcover#v=onepage&q&f=false) for more details
//Can be disabled by removing the #include "fastAnalog.h" above
BIT_SET(ADCSRA,ADPS2);
BIT_CLEAR(ADCSRA,ADPS1);
BIT_CLEAR(ADCSRA,ADPS0);
#endif
#endif
}
void instanteneousMAPReading()
{
//Instantaneous MAP readings

View File

@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "decoders.h"
#include "idle.h"
#include "auxiliaries.h"
#include "fastAnalog.h"
#include "sensors.h"
#include "src/PID_v1/PID_v1.h"
//#include "src/DigitalWriteFast/digitalWriteFast.h"
@ -254,6 +253,7 @@ void setup()
initialiseFan();
initialiseAuxPWM();
initialiseCorrections();
initialiseADC();
//Check whether the flex sensor is enabled and if so, attach an interupt for it
if(configPage1.flexEnabled)
@ -484,29 +484,6 @@ void setup()
//Initial values for loop times
previousLoopTime = 0;
currentLoopTime = micros();
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this
#if defined(ANALOG_ISR) //ADC interrupt routine
//This sets the ADC (Analog to Digitial Converter) to run at 250KHz, greatly reducing analog read times (MAP/TPS)
//the code on ISR run each conversion every 25 ADC clock, conversion run about 100KHz effectively
//making a 6250 conversions/s on 16 channels and 12500 on 8 channels devices.
ADCSRB = 0x00; //ADC Auto Trigger Source is in Free Running mode
ADMUX = 0x40; //Select AREF as reference, ADC Left Adjust Result, Starting at channel 0
ADCSRA = 0xEE; // ADC Interrupt Flag enabled and prescaler selected to 250KHz
#else
//This sets the ADC (Analog to Digitial Converter) to run at 1Mhz, greatly reducing analog read times (MAP/TPS)
//1Mhz is the fastest speed permitted by the CPU without affecting accuracy
//Please see chapter 11 of 'Practical Arduino' (http://books.google.com.au/books?id=HsTxON1L6D4C&printsec=frontcover#v=onepage&q&f=false) for more details
//Can be disabled by removing the #include "fastAnalog.h" above
#ifdef sbi
sbi(ADCSRA,ADPS2);
cbi(ADCSRA,ADPS1);
cbi(ADCSRA,ADPS0);
#endif
#endif
#endif
mainLoopCount = 0;
ignitionCount = 0;