NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Reading from Multiple ADC sources.

August 23, 2011
by Twarter369
Twarter369's Avatar

I am trying to set up the ADC converter to work on PC5 then PC4 Basically I want to average both and then compare those averages.

Specifically, how do I tell ADSC which input to read from? Does anyone have a code example for this?

I am currently trying to build a Photovore Robot, if that helps at all.

August 23, 2011
by Twarter369
Twarter369's Avatar

Nevermind, I found it!

"MUX0 to MUX3 are used to select which input channel you wish to read."

August 24, 2011
by bretm
bretm's Avatar

If you just need to compare voltages you could use the analog comparator instead of the ADC but it doesn't do averaging.

August 24, 2011
by Twarter369
Twarter369's Avatar

Thanks, I am not sure which of those methods would give me a better read? Could you show an example of using the Comparator two compare two voltage sources? Even though I know the different Registers for the different pins I am having trouble figuring out how to control where the ADSC is reading from. Do I switch the bit patter by assigning it a binary value such as

ADMUX = 0011;

or something like

ADMUX |= (1<<MUX0) | (1<<MUX1);
August 24, 2011
by Twarter369
Twarter369's Avatar

okay, I think this will handle the problem:

uint16_t ReadADC(uint8_t __channel){
   ADMUX |= __channel;                // Channel selection
   ADCSRA |= _BV(ADSC);               // Start conversion

   while(ADCSRA & (1<<ADSC)) {
    // do nothing... just hold your breath.
  }
  // bit is cleared, so we have a result.

  // read from the ADCL/ADCH registers, and combine the result
  // Note: ADCL must be read first (datasheet pp. 259)
  uint16_t result = ADCL;
  uint16_t temp = ADCH;
  result = result + (temp<<8);
  return result;
}

}

let me know if there is a more efficient way to do it (or any glaring holes in my logic)

Post a Reply

Please log in to post a reply.

Did you know that a NerdKit can take control of a remote-controlled car? Learn more...