Pages: [1]   Go Down
Author Topic: ADC on mega2560  (Read 399 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25733
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any reason for not using analogRead?
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25733
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you know what I'd do?
I'd look at the library source, and hard code it.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: