i'm working on a project and i need to have an ADC on port A15. I know that there's a built in function in the wiring library but i'm trying to do it with the AVR libraries.
here is the two functions i've wrote:
//16MHz/128 = 125kHz the ADC reference clock
ADCSRA = ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADEN));
ADCSRA |= (1<<ADSC);
ADMUX = (1<<REFS0)|(channel & 0x1F); //Set Voltage reference to Avcc (5v)
ADCSRB = channel & 0x20; //Set MUX5
while(ADCSRA & (1<<ADSC));
ADCSRA |= (1<<ADSC); //Starts a new conversion
while(ADCSRA & (1<<ADSC));//Wait until the conversion is done
return ADCW; //Returns the ADC value of the chosen channel
I really can't understand why the function keep returning 0 instead of a number ( note: the analog pin is connected to a potentiometer so that the value of the analog input is between 0 and 5 V).
can you see any errors?