Does AVR ADC clear MUX3...0 bits automatically after each single conversion?

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

Hello,

 

Im making program that reads voltages from different ADC channels one after another.

 

Im using single conversion started with writing to ADSC in ADCSRA.;

 

And im wondering, does AVR automatically clear MUX3...0 bits in ADMUX register after finishing conversion or do i have to do it before switching to channel?

 

Example, this is what i do to read ADC3 and ADC4

		_delay_us(250);
		ADMUX |=  (1<<MUX1)|(1<<MUX0);  //ADC3
		ADMUX &= ~(1<<MUX2); //ADC3
		ADCSRA |= (1<<ADSC); //start conversion
		while(ADCSRA & (1<<ADSC)); //wait
		return ADC;

		_delay_us(250);
		ADMUX |=   (1<<MUX2);  //ADC4
		ADMUX &= ~((1<<MUX1)|(1<<MUX0)); //ADC4
		ADCSRA |= (1<<ADSC); //start conversion
		while(ADCSRA & (1<<ADSC)); //wait
		return ADC;

To select ADC3 i need to clear MUX2 and set MUX1 and MUX0.

 

To select ADC4 tho i need to set MUX2 and clear MUX1 and MUX0.

 

And im wondering, can i simply change it to look like this

 

		_delay_us(250);
		ADMUX |=  (1<<MUX1)|(1<<MUX0);  //ADC3
		ADMUX &= ~(1<<MUX2); //ADC3
		ADCSRA |= (1<<ADSC); //start conversion
		while(ADCSRA & (1<<ADSC)); //wait
		return ADC;

		_delay_us(250);
		ADMUX |=   (1<<MUX2);  //ADC4
		ADMUX &= ~((1<<MUX1)|(1<<MUX0)); //ADC4
		ADCSRA |= (1<<ADSC); //start conversion
		while(ADCSRA & (1<<ADSC)); //wait
		return ADC;

or there will be error, where ill read ADC3 properly because its first and ADMUX by default is cleared (so MUX2 is actually cleared and i set MUX1 and MUX0) BUT instead of reading ADC4 (where i need MUX2 to be set and MUX1 and MUX0 to be cleared) ill read ADC7 because from previous ADC3 conversion MUX1 and MUX0 will still stay set and ill additionally set MUX2 too?

Last Edited: Fri. Jan 22, 2016 - 08:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KonradIC13 wrote:
And im wondering, does AVR automatically clear MUX3...0 bits in ADMUX register after finishing conversion

No.

 

But IMO you are making it too hard on yourself.  Simply rebuild ADMUX...

 

As I am fond of saying:

https://www.avrfreaks.net/comment...

For some reason, many posters on this site have a love affair with |=. There must be a sale on "|" in full-reel quantities.

Follow the links in this post:  https://www.avrfreaks.net/comment...

 

Let me pick out one with ADC example... https://www.avrfreaks.net/forum/m...

 

ADMUX = ADC_VREF_TYPE + ad_index;

The reference selection and ADLAR generally stay the same in most apps.  So the compiler will create a simple LDI of the constant part, add the channel specification, and do an OUT.  Three cycles; three words; clean (easier to read); eliminates RMW and possible side effects.

 

 

 

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.

Last Edited: Fri. Jan 22, 2016 - 08:44 PM