ATMEGA88 ADC read problem

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

Hello,

i´m using the atmega88 and want convert a voltage signal on the ADC7 input.

if i run the code in the main programm in an "while(1)" loop all works fine and i get my ADC results cyclic.

Now the Problem:
i execute the code in an 100ms cyclic routine.
read the adc and at the end i start the adc.

the code:

cyc100ms(){
 TempVal = ADCH << 8;
 TempVal += ADCL;        // Get value of temperature 
 ADCSRA |= ADC_START;    // Start adc 
}

the code will be execute! i testet it with the debugger.

Is there any time window where i have to read the registers after the adc conversion is finished?

thanks a lot

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

Quote:
the code will be execute! i testet it with the debugger.
So what is the problem? Or do did you intend to write that it will not execute? If so you need to show more code (a complete example).
/Lars

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

Btw, from the datasheet:

Quote:
ADCL must be read first, then ADCH.

/Lars

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

Quote:

ADCL must be read first, then ADCH.

A lot of the C compilers provide a composite 16 bit access to the whole value as "ADC" or sometimes "ADCW". If you compiler has that then use it. It saves you aking the read order error and it sames you having to do <<8 - just use:

TempVal = ADC(W);

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

Thanks Lajon,

that`s it, i forgot that i have to read the ADCL first
Thank you.

Thanks clawson,

i used the same code in other controllers where i can´t access to 16 bit registers. But now it´s the better way to do that.
thank you.

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

Heh, I spent a few gruesome hours a few days ago with a similar problem: forgot this L/H stuff. BTW, avr-gcc allows you to just read ADC, which is a meta-16-bit-register. I'm not sure how it handles the alignment, but with the defaults it works fine.

The Dark Boxes are coming.