1. This is the block diagram that contains the hardware resources (at conceptual level) of the ADC of Arduino MEGA Board.
Figure-1: Internal structure of the ADC of Arduino MEGA Board
An analog channel (say, ADC0) is selected first; wait for a while for the signal to stabilize in the internal hold capacitor.
Issue a START command to the ADC for converting the input analog signal into digital form. The maximum value of the input analog signal will depend on the selection of the Reference Voltage for the VREF-pin of the ADC. The START command is implemented by putting LH at the ADSC-bit of the ADCSRA Register. The ADSC-bit remains at LH-state as long as the conversion is going ON. At the end-of-conversion the ADSC-bit assumes LL-state.
The ADC takes about 13us - 260 us time to convert a sample depending on the clkADC. The recommended clkADC is 125 kHz.
After the conversion, the 10-bit digital value enters into a 10-bit ADC (ADCH, ADCL). At the same time, the ADIF-bit of ADCSRA Register assumes LH-state to indicate the end-of-conversion.
A user can determine the end-of-conversion state by continuous polling of the ADSC-bit or the ADIF-bit of the ADCSRA Register.
If interrupt logic bits are kept at enabled states (ADIE-bit of ADCSRA Register is at LH; the I-bit of SREG is at LH), the ADIF-flag/bit can interrupt the MCU to convey the message of the end-of-conversion.
2. Functional Test of Ch-0 using Arduino Instructions
Upload the following sketch in the MEGA Board, Short A0-pin to 3.3V point of the MEGA. Check that the Serial Monitor shows about 675 (1023/5*3.3 ~= 675). Connect the A0-pin to GND; check that the Serial Moroni shows 0.
analogReference(DEFAULT); //5V for Vref-pin of ADC
//select Ch-0; (clkADC=125 kHz by Arduino); start conversion; check end-of-conversion; read ADC //data into variable x
int x = analogRead(A0);
Serial.println(x, DEC); //3.3V point is shorted to A0-pin
3. To write Register Level Codes for the codes of Step-2, we have to deal with bit manipulation of the following registers.