The ADCs in the ATtiny85 and ATmega2560 (the chip powering the Arduino
Mega 2560) are pretty similar, except for the Mega having more inputs.
In particular, the way you set the clock prescaler is the same, namely
the bits ADPS2:0 in the register ADCSRA. You could use the same code to
set the prescaler on both chips, but you probably don't want to unless
your ATtiny is clocked at 16 MHz, like your Mega. I would normally
just set the control register to the value I want, rather than
touching only a few bits, but that is a matter of personal preference.
So, to set the ADC clock of the Mega to 1 MHz, I would
ADCSRA = _BV(ADEN) // enable the ADC
| _BV(ADPS2); // clock at F_CPU / 16 = 1 MHz
The same code would do the same thing on an ATtiny clocked at
16 MHz. If the ATtiny runs at 8 MHz, I would instead
ADCSRA = _BV(ADEN) // enable the ADC
| _BV(ADPS1) // clock at
| _BV(ADPS0); // F_CPU / 8 = 1 MHz
If you want to be generic:
#if F_CPU >= 12000000
# define ADPS_SETTING _BV(ADPS2) // F_CPU / 16
#else
# define ADPS_SETTING (_BV(ADPS1) | _BV(ADPS0)) // F_CPU / 8
#endif
ADCSRA = _BV(ADEN) // enable the ADC
| ADPS_SETTING; // clock somewhere near 1 MHz
As bigjosh explained in comments, the prescaler setting is a trade-off
between speed and accuracy. I recommend you read the article on the
Arduino ADC by Nick Gammon. He did some tests at various speeds
which showed that you can get decent results with clock speeds up to
1 MHz, then it becomes pretty bad at 2 MHz and mostly
useless beyond. You may want to run the same tests with your particular
setup.
Except for the very first conversion, which takes longer, an ADC
conversion takes 13 cycles of the ADC clock. If you run that clock
at 1 MHz, that is 13 µs per reading. If you get the readings
with analogRead()
, you won't get one reading every 13 µs, because
the CPU needs extra time to execute the code you have between successive
calls of analogRead()
. You can save time by having the CPU and the ADC
work in parallel, maybe along these lines:
ADCSRA |= ADSC; // start the first conversion
for (int i = 0; i < NB_READINGS; i++) {
loop_until_bit_is_clear(ADCSRA, ADSC); // wait for the ADC
uint16_t reading = ADC; // get the reading
ADCSRA |= ADSC; // start the next conversion
process(reading); // process the reading we have
}
This way you process one reading while the ADC is taking the next one.
Or you can set the ADC to “free running mode” (where it takes one
reading after another without ever stopping) and get the readings in an
interrupt service routine. Whichever method works best depends on your
particular application: the amount of works your CPU has to do while
reading the ADC and whether you can afford to do the whole series of
readings with interrupts disabled.
/2
, then the lock that goes into the ADC will be 500Khz. The ADC clock (after the prescaller) needs to be 50-200Khz to get all 10 bits of precision, but you can run it up to 1mhz but more of the bottom bits will start being wrong the faster you go. How much precision do you need and how fast you do need samples?