AVR/Examples/AD — Analog-Digital Example
uint16_t ReadADC() {
uint16_t result = 0; /* Temporary variable */
PRR ^= (1<<PRADC); /* Set the Power Reduction ADC-bit to 0 */
ADMUX = (0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(0<<MUX0); /* ADC4 and AREF voltage reference */
DIDR0 = (1<<ADC4D); /* Disable the digital buffer at this pin */
ADCSRA = (1<<ADEN)|(1<<ADSC); /* Start conversion */
while(ADCSRA & (1<<ADSC)) /* Wait until the conversion is finished */
;
result = (unit16_t)ADCL; /* Get the lover 8 bits of the result*/
result += (((unit16_t)ADCH)<<8); /*+ upper 2 bits.*/
ADCSRA = 0x00; /* Turn off the ADC module */
PRR |= (1<<PRADC);
return result;
}