/*
* adc.c
*
* Created: 14-Oct-15 7:39:18 PM
* Author: Krishna
*/
#include <avr/io.h>
#define F_CPU 16000000UL
#include<util/delay.h>
void adc_init()
{
ADMUX |= (REFS0); // AVcc with external capacitor at AREF
// Set the ADC prescaler to 128 (i.e., 16MHz/128 = 125KHz)
ADCSRA |= ( 1 << ADPS2 )|( 1 << ADPS1 ) | ( 1 << ADPS0 );
ADCSRA |= ( 1 << ADEN ); // ADC ON
ADCSRA |= (1<<ADSC);
}
unsigned int read_adc(unsigned int ch_selection)
{
ch_selection &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch_selection; // clears the bottom 3 bits before ORing
ADCSRA |= (1<<ADSC); //Starts a new conversion
while(ADCSRA & (1<<ADSC)); //Wait until the conversion is done
return ADCW;
}