0

Hello Everyone (I might be making some mistake in asking this "question", bear with me as I am new to these sites... I honestly think this is the best way to frame my inquiry because it's rather elaborate)

I'd like some help in understanding the "timings" of Arduino MEGA 2560's ADC module, especially with respect to the command analogRead.

I read ATmega2560 microcontroller Datasheet

Let's assume, for now, that our conversions are normal ones ( the other type being a "first" conversion, which takes 25 ADC clock cycles instead of 13, due to the time required for the initialization of analog components )

From what I understood, on a normal single conversion, the sample and hold happens after 1.5 ADC clock cycles from the start of the conversion.

Let's say that I set the prescaler to 32 with the following commands:

const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

ADCSRA &= ~PS_128;  // remove bits set by Arduino library
ADCSRA |= PS_32;  // set our own prescaler to 32 

hence 16MHz / 32 = 500 kHz should be the new ADC clock frequency ( or 2 microseconds ADC clock period )

since a normal single conversion takes 13 ADC cycles, the entire conversion should take about 26 microseconds

If I'm not mistaken, (2 microseconds * 1,5) = 3 microseconds after the ADC conversion starts, my analog pin value is set on hold, right?

I measured the elapsed time using the micros() function and, like I'd expect, I found out that there's a little overhead introduced with analogRead function because it takes about 28 \ 32 \ 36 microseconds (these are empirical measures).

analyzing the code in wiring_analog.c ( which is located in the folder [...]/arduino/hardware/arduino/cores/arduino/ ) the actual conversion seems to happen at line 77

// start the conversion
sbi(ADCSRA, ADSC);

does that mean that this is the instant from which the actual ADC conversion start, and so 1.5 ADC clock cycles from here, my analog pin value is set on hold?

how can I know how much time passed before that sbi function call is issued?

I saw that a single instruction on arduino won't take more than 4 microseconds (but micros() function has that resolution so I don't exactly know!)

Now, considering the so called "first" conversion, which take 25 ADC clock cycles

does this conversion happen just the first time I call analogRead since I powered on Arduino? or does it happen even after some time , because the ADC module gets powered off? (of course, I consider the case when Arduino remains powered on all the time)

Judging by the timings I measured it seems like that the former is the one really happening, even if I don't use ADC for some time, but can anyone confirm?

1

does that mean that this is the instant from which the actual ADC conversion start, and so 1.5 ADC clock cycles from here, my analog pin value is set on hold?

Yes. After between 3μs and 5μs. This is because conversion start at the following rising edge of the ADC clock cycle. This rising edge might have just happened, which means you need to wait 31 (main) clock cycles.

how can I know how much time passed before that sbi function call is issued?

You could copy the original code and create your own analogRead2 function that measures the time before setting ADSC.

I saw that a single instruction on arduino won't take more than 4 microseconds (but micros() function has that resolution so I don't exactly know!)

A single instruction will take 1 or 2 clock cycles (if I remember correctly). So at 16Mhz that will mean 62.5ns (nano seconds) per clock cycle. Not 4ms

Now, considering the so called "first" conversion, which take 25 ADC clock cycles

does this conversion happen just the first time I call analogRead since I powered on Arduino? or does it happen even after some time , because the ADC module gets powered off? (of course, I consider the case when Arduino remains powered on all the time)

Judging by the timings I measured it seems like that the former is the one really happening, even if I don't use ADC for some time, but can anyone confirm?

This first conversion is after the ADEN bit changes from 0 to 1. If you don't unset this bit, all further conversions will take the shorter time.

PS on the first conversion, the sample-and-hold takes place after the extra 12 ADC clock cycles, so between cycle 12 and 13.5.

PPS this info is actually based on the ATMega328, but should be pretty similar in the 2560.

  • First of all, thx Gerben. "Yes. After between 3ms and 5ms. [...]" Just to be crystal clear: you meant microseconds, right? I'm asking because I work with that magnitude: I need to read within 40 microseconds "A single instruction will take [...] 62.5ns per clock cycle." if it gets translated into one machine instruction, yes, but like you suggested, I can create my copy of AnalogRead and get the info I need; Nice advice, thanks! :D the first sbi() call took about 52 microsecs, the next ones took nearly 24 microsecs. – RookieGui Dec 23 '15 at 12:41
  • Yes, sorry. I meant μs. – Gerben Dec 23 '15 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.