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.
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?