//This sets the ADC (Analog to Digitial Converter) to run at 1Mhz, greatly reducing analog read times (MAP/TPS) when using the standard analogRead() function
//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 detail
BIT_SET(ADCSRA,ADPS2);
BIT_CLEAR(ADCSRA,ADPS1);
BIT_CLEAR(ADCSRA,ADPS0);
Serial.begin(115200);
//Setup for the MCP4921 SPI
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV4);//Probably need to speed this up
pinMode(pinChipSelect,OUTPUT);
digitalWrite(pinChipSelect,HIGH);
//Configure Timer2 for our 1ms 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 */
if(serialStream==true&&Serial.availableForWrite()){sendCSV();}//If serialStream is enabled and the serial write buffer isn't full, the current map values are sent continually as CSV
outputValue=map(currentMAP,0,2048,0,4095);//The MAP readings have been multiplied by 8 for better resolution. The 2048 brings them back into an 8 bit (256) range equivalent