Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

prob with ADC !

prob with ADC !

2005-04-23 by arhodes19044

Ok, I solved the LCD display issue.  I can now interact directly 
with the board.  The cpu is an ATmega128 on a Mavric-IIb board.

I wired in a small keypad and that works well.  

I now am trying to read a multi-position rotary switch with ADC.  I 
have it set to a position where the voltage on the ADC pin (I triple 
checked that it IS the pin I think it is) is about 2.5 volts.

I have the code fragments for ADC included below.  I do not have 
PORT F set up in any special way.  It is just the way it comes on 
boot up. (Input Tristate?)

All I get back is a count of 1023 no matter what.

What did I do wrong?



//--------------------------------------------
void init_adc(void)
{
	ADMUX  |= ((1<<MUX2) | (1<<MUX1) | (1<<MUX0));	//div = 128
	ADMUX = 0;	
		//use a pin not used by important hardware (JTAG)

	ADCSRA |= 1<<ADEN;	//enable ADC (does not start an adc)

	ADCSRA |= 0x40;         //start the first (slow) conversion
	
	//while (ADCSRA & ADC_STATUS_MASK);	// do not bother to 
formally wait since a bunch of stuff comes next
}




//------------------------------------------
unsigned int read_adc(unsigned char pin_num)
{
	unsigned int low_byte, high_byte;

	ADMUX = pin_num;  //use pin 0
	
	ADCSRA |= 0x40;		// START CONVERSION	
	while (ADCSRA & 0x40);	// wait until conversion over
	
	low_byte = ADCL;
	high_byte = ADCH;
	
	return ((high_byte<<8) | low_byte);
}

Re: prob with ADC !

2005-04-24 by arhodes19044

Oh, yeah.  Look at that.  I will correct it.

I think I may also need to change my voltage reference.  Zeros at 
REFS1:0 use AREF.  I think I want the internal AVCC.

The docs say "AVCC, with external capacitor at AREF pin".  Do I HAVE 
to use the cap?  If so, what size?

-Tony

--- In AVR-Chat@yahoogroups.com, "Larry Barello" <yahoo@b...> wrote:
> Pay close attention to your first line of code....  It isn't doing 
what you
> intended.

Re: prob with ADC !

2005-04-24 by arhodes19044

Yes it is the Mavcric-IIb!  I am glad the cap is already there.  I 
foud that I got decent readings once I fixed my code and set the ACR 
to use the AVCC instead of the "default" AREF.

One other question:  in one of the little write-ups here on ADC, it 
said that "AVCC should be connected to VCC whenever PORTA is used a 
output.  This is because PORTA is always sourced by AVCC."  I think 
this write-up was specifically dealing with the 8535, so I do not know 
how that info applies to the 128.

I do not have AREF connected to anything and PORTA is working fine as 
output and input for my keypad scanning code.  

Is this true, and do I need to do anything special about it?

-Tony

--- In AVR-Chat@yahoogroups.com, Brian Dean <bsd@b...> wrote:
> Hi Tony,
> 
> On Sun, Apr 24, 2005 at 02:08:52AM -0000, arhodes19044 wrote:
> 
> > The docs say "AVCC, with external capacitor at AREF pin".  Do I 
HAVE
Show quoted textHide quoted text
> > to use the cap?  If so, what size?
> 
> This is your MAVRIC-IIB, correct?  If so, then it already has the cap
> installed.
> 
> -Brian
> -- 
> Brian Dean
> BDMICRO - ATmega128 Based MAVRIC Controllers
> http://www.bdmicro.com/

Re: [AVR-Chat] Re: prob with ADC !

2005-04-24 by Brian Dean

Hi Tony,

On Sun, Apr 24, 2005 at 02:08:52AM -0000, arhodes19044 wrote:

> The docs say "AVCC, with external capacitor at AREF pin".  Do I HAVE
> to use the cap?  If so, what size?

This is your MAVRIC-IIB, correct?  If so, then it already has the cap
installed.

-Brian
-- 
Brian Dean
BDMICRO - ATmega128 Based MAVRIC Controllers
http://www.bdmicro.com/

Re: [AVR-Chat] Re: prob with ADC !

2005-04-24 by Brian Dean

On Sun, Apr 24, 2005 at 03:22:00AM -0000, arhodes19044 wrote:

> Yes it is the Mavcric-IIb!  I am glad the cap is already there.  I
> foud that I got decent readings once I fixed my code and set the ACR
> to use the AVCC instead of the "default" AREF.
> 
> One other question:  in one of the little write-ups here on ADC, it
> said that "AVCC should be connected to VCC whenever PORTA is used a
> output.  This is because PORTA is always sourced by AVCC."  I think
> this write-up was specifically dealing with the 8535, so I do not know
> how that info applies to the 128.

In this case of the ATmega128, that would be PORTF instead of PORTA,
since PORTF is the A/D port on the Mega128.

> I do not have AREF connected to anything and PORTA is working fine as
> output and input for my keypad scanning code. 
> 
> Is this true, and do I need to do anything special about it?

Yes, it's true (for PORTF), but you don't need to do anything - your
MAVRIC-IIB connects AVCC to VCC though a 10 uH inductor as specified
in the datasheet.  You are good to go!

-Brian
-- 
Brian Dean
BDMICRO - ATmega128 Based MAVRIC Controllers
http://www.bdmicro.com/

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.