I set up a test to look at what happens with higher prescaler values for the ADC and got some strange results. Can anybody explain what I am seeing?
With prescaler values from 128 to 16, both reading methods track the same.
Prescaler of 8, the full scale input, A0, doesn't read full value and A1 begins to wonder.
Prescaler of 4 and 2, all the inputs begin to wonder.
However, the readings using the register control seem to be solid.
Setup:
- Mega
- target - 4 resistors in series between +5 and gnd on a breadboard
- inputs are at the 5 junctions - A0 5v to A4 gnd
Readings on left are using the analogRead() calls. Readings on right are done using registers to start and stop. I threw in some time markers to see how long the samples took.
//receive single char
char receivedChar;
boolean newData = false;
boolean takesample = false;
unsigned long timestart;
unsigned long timestop0;
unsigned long timestop0a;
unsigned long timestop1;
unsigned long timestop2;
unsigned long timestop3;
unsigned long timestop4;
int sensorValue0 = 0; // value read from the pot
int sensorValue1 = 0; // value read from the pot
int sensorValue2 = 0; // value read from the pot
int sensorValue3 = 0; // value read from the pot
int sensorValue4 = 0; // value read from the pot
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready>");
DIDR0 = 0xFF; //diable digital inputs
}
void loop()
{
recvOneChar();
setSampleRate();
takereadings();
}
void recvOneChar()
{
if (Serial.available() > 0)
{
receivedChar = Serial.read();
newData = true;
}
}
void setSampleRate()
{
if (newData == true)
{
switch (receivedChar)
{
case '7':
Serial.print("128 prescalar - ");
bitWrite(ADCSRA,2,1);
bitWrite(ADCSRA,1,1);
bitWrite(ADCSRA,0,1);
Serial.println(ADCSRA);
takesample = true;
break;
case '6':
Serial.print("64 prescalar - ");
bitWrite(ADCSRA,2,1);
bitWrite(ADCSRA,1,1);
bitWrite(ADCSRA,0,0);
Serial.println(ADCSRA);
takesample = true;
break;
case '5':
Serial.print("32 prescalar - ");
bitWrite(ADCSRA,2,1);
bitWrite(ADCSRA,1,0);
bitWrite(ADCSRA,0,1);
Serial.println(ADCSRA);
takesample = true;
break;
case '4':
Serial.print("16 prescalar - ");
bitWrite(ADCSRA,2,1);
bitWrite(ADCSRA,1,0);
bitWrite(ADCSRA,0,0);
Serial.println(ADCSRA);
takesample = true;
break;
case '3':
Serial.print("8 prescalar - ");
bitWrite(ADCSRA,2,0);
bitWrite(ADCSRA,1,1);
bitWrite(ADCSRA,0,1);
Serial.println(ADCSRA);
takesample = true;
break;
case '2':
Serial.print("4 prescalar - ");
bitWrite(ADCSRA,2,0);
bitWrite(ADCSRA,1,1);
bitWrite(ADCSRA,0,0);
Serial.println(ADCSRA);
takesample = true;
break;
case '1':
Serial.print("2 prescalar - ");
bitWrite(ADCSRA,2,0);
bitWrite(ADCSRA,1,0);
bitWrite(ADCSRA,0,1);
Serial.println(ADCSRA);
takesample = true;
break;
case '0':
Serial.print("0 prescalar - ");
bitWrite(ADCSRA,2,0);
bitWrite(ADCSRA,1,0);
bitWrite(ADCSRA,0,0);
Serial.println(ADCSRA);
takesample = true;
break;
}
newData = false;
}
}
void takereadings()
{
if(takesample)
{
// read the analog in value:
timestart = micros();
sensorValue0 = analogRead(A0);
timestop0 = micros() - timestart;
timestart = micros();
bitWrite(ADCSRA,6,1); // start ADC
while(!(ADCSRA & 0x10)); // wait for adc to be ready
byte m = ADCL; // fetch adc data
byte j = ADCH;
int a = (j << 8) | m; // form into an int
timestop0a = micros() - timestart;
timestart = micros();
sensorValue1 = analogRead(A1);
timestop1 = micros() - timestart;
//timestart = micros();
bitWrite(ADCSRA,6,1); // start ADC
while(!(ADCSRA & 0x10)); // wait for adc to be ready
m = ADCL; // fetch adc data
j = ADCH;
int b = (j << 8) | m; // form into an int
//timestop0a = micros() - timestart;
timestart = micros();
sensorValue2 = analogRead(A2);
timestop2 = micros() - timestart;
//timestart = micros();
bitWrite(ADCSRA,6,1); // start ADC
while(!(ADCSRA & 0x10)); // wait for adc to be ready
m = ADCL; // fetch adc data
j = ADCH;
int c = (j << 8) | m; // form into an int
//timestop0a = micros() - timestart;
timestart = micros();
sensorValue3 = analogRead(A3);
timestop3 = micros() - timestart;
//timestart = micros();
bitWrite(ADCSRA,6,1); // start ADC
while(!(ADCSRA & 0x10)); // wait for adc to be ready
m = ADCL; // fetch adc data
j = ADCH;
int d = (j << 8) | m; // form into an int
//timestop0a = micros() - timestart;
timestart = micros();
sensorValue4 = analogRead(A4);
timestop4 = micros() - timestart;
//timestart = micros();
bitWrite(ADCSRA,6,1); // start ADC
while(!(ADCSRA & 0x10)); // wait for adc to be ready
m = ADCL; // fetch adc data
j = ADCH;
int e = (j << 8) | m; // form into an int
//timestop0a = micros() - timestart;
//Serial.print("ADMUX = ");
//Serial.println(ADMUX);
Serial.print("A0 = " );
Serial.print(sensorValue0);
Serial.print("\t ");
Serial.println(a);
Serial.print("t = ");
Serial.print(timestop0);
Serial.print("\t\t ");
Serial.println(timestop0a);
Serial.println("");
Serial.print("A1 = " );
Serial.print(sensorValue1);
Serial.print("\t ");
Serial.println(b);
Serial.print("t = ");
Serial.println(timestop1);
Serial.println("");
Serial.print("A2 = " );
Serial.print(sensorValue2);
Serial.print("\t ");
Serial.println(c);
Serial.print("t = ");
Serial.println(timestop2);
Serial.println("");
Serial.print("A3 = " );
Serial.print(sensorValue3);
Serial.print("\t ");
Serial.println(d);
Serial.print("t = ");
Serial.println(timestop3);
Serial.println("");
Serial.print("A4 = " );
Serial.print(sensorValue4);
Serial.print("\t\t ");
Serial.println(e);
Serial.print("t = ");
Serial.println(timestop4);
Serial.println("");
takesample = false;
}
}