Yahoo Groups archive

AVR-Chat

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

Message

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

Attachments

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.