Go Down

Topic: ADC on mega2560 (Read 574 times) previous topic - next topic

nanni91

Hi,

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:

Code: [Select]
void adc_init(void){

//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));
}

uint16_t read_adc(){
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?

cheers

Pietro

nanni91

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?

cheers

Pietro

AWOL

Any reason for not using analogRead?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
i'm working on a project and i need to have an ADC on port A15.

A15 isn't a port. It's a pin on a port.

nanni91

i have to work without the wiring library as it is a work for my university and is clearly required to not use that library.

sorry about that, i meant i want to use the channel a15 on the Mega, which is connected to pin A15 on the board.

any help?

AWOL

Do you know what I'd do?
I'd look at the library source, and hard code it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

nanni91

alright but i spent a lot of time on the code i've posted, using the atmega2560 data sheet and i relly can't find where's the error, so i'd really appreciate if someone could give me an advice!

Delta_G

I don't have the data sheet right here with me but I'm certain you're reading the result wrong.  You should get back a ten bit answer which means you'll have to read in at least two bytes and do some bit shifting into an int.  You can look at the wiring code to see how it is done.

Go Up