Can't get ADC to work

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

would someone mind checking my code please? I'm having issues with my ADC and can't seem to get it to perform a conversion. I posted the ADC loops, can paste the rest of the code if someone wants.

/*---------------------------------------------------
Initialise the ADC. Single Conversion
---------------------------------------------------*/
void ADC_Init (void)
{


	/*Ensure ADC is not powered down*/
	PRR |= (0<<PRADC);

	/*Disable Digital Buffer on PinC 0 - 8*/
	DIDR0 = 0b1111111;

	/*Voltage Reference selection:
	AVCC with external capacitor at AREF pin*/
	ADMUX |= (0<<REFS1)|(1<<REFS0)|(1<<ADLAR);
	
	/*Set frequency scaler to 128*/

	ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);


	
}
/*---------------------------------------------------
Starting the ADC Conversion
Returns a float into an array. 
---------------------------------------------------*/
uint16_t ReadADC(uint8_t ch)
{
   //Select ADC Channel ch must be 0-7
   ch=ch&0b00000111;
   ADMUX|=ch;

   //Start Single conversion
   ADCSRA|=(1<<ADSC)|(1<<ADEN);

   //Wait for conversion to complete
   while(!(ADCSRA & (1<<ADIF)));

   //Clear ADIF by writing one to it
   ADCSRA|=(1<<ADIF);

   return(ADCL);
}

I am using a mega324P

Last Edited: Mon. Jun 27, 2011 - 07:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You selected ADLAR so ONLY read ADCH and don't touch ADCL. Also you are not clearing any existing channel selection when you set ADMUX

(I feel FAQ#6 coming on - that's about the 3rd one today!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, look here

https://www.avrfreaks.net/index.p...

There is just another post with the same discussion (and error).

and here
https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

(I feel FAQ#6 coming on - that's about the 3rd one today!)

Perhaps we could disallow |= until proficiency with it is certified?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PRR |= (0<<PRADC); 

This statement does nothing.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
You selected ADLAR so ONLY read ADCH and don't touch ADCL. Also you are not clearing any existing channel selection when you set ADMUX

(I feel FAQ#6 coming on - that's about the 3rd one today!)

RTFM? 1 Datasheet, 3 tuts and apart from a coding error, the answer was a 100K pot. :( :oops:

:lol:

But I've made the changes as suggested.

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:

PRR |= (0<<PRADC); 

This statement does nothing.

Because it's already set to 0?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because ORing anything with 0 does nothing.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Because ORing anything with 0 does nothing.

ZAPrime,

Just to add to what Steve said. The way you clear a single bit in a register is:

PRR &= ~(1<<PRADC); 

If you don't know why it's this and not:

PRR |= (0<<PRADC); 

you will benefit from reading the "Bit Manipulation 101" article in the Tutorial Forum.

Last Edited: Tue. Jun 28, 2011 - 09:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The way you clear a single bit in a register is:

PRR |= ~(1<<PRADC);

Nope, this is:

PRR &= ~(1<<PRADC); 

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Duly Noted, Thanks guys and girls :)

I took the line out my code.