ATmega32 ADC

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

Hello!
I am using an ADC in free running mode.
i want to make a conversion into an infinity while loop, and i will take the result (10bit) in a variable.
how can i take all of 10bit? it must to add the ADCH and ADCL together?
at the first time i'm not interrested how and when the conversion its done. i want just to take the 10bit value, free running mode.

it's a neccessary to use a loop to pause the mcu until the conversion its done?

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

in setup (ADLAR = 0) can i just read the ADCL to get the
lowest 8bit?

from the datasheet:
"When ADCL is read, the ADC Data Register is not updated until ADCH is read."
what it mean that?

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

Check out the adc tutorials, which show how to work with the adc system. You can use the adc_ISR to get the results. Also, have your MCU datasheet open to the adc section. :wink: Afterwards, use the search function and type "adc lee" or just "adc" and you'll get a TON of examples of working code.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Thanks for your response! :-)

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

Quote:
how can i take all of 10bit?
If you are using C, most compilers will let you simply do this:

int result = ADC;

Regards,
Steve A.

The Board helps those that help themselves.

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

but theres two register for accessing in adc data (ADCH & ADCL) i have do this with shifting ADCH and merge though OR with ADCL.

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

Not if you use C. Most (all?) the AVR C compilers let you access a combined virtual 16bit register called "ADC" which is simply an access to ADCL then ADCH made in the correct sequence. A lot of beginners litter their code with stuff like:

uint16_t result = ADCL;
result += (ADCH << 8);

(+= could be |= I guess) or similar when a simple:

uint16_t result = ADC;

would work just fine.

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

i use the first way. works fine!
but i read the ADCH first

specifically:
u = ((ADCH << 8) | (ADCL));

which its the same with

u = ((ADCL) | (ADCH << 8));

right?

there's a matter where read the ADCH first?
instead. the datasheet says that reading ADCL
and then ADCH the ADC data then are update.

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

Quote:
but i read the ADCH first

specifically:
u = ((ADCH << 8) | (ADCL));

This does >>NOT<< guarantee that ADCH is read first. The compiler is allowed to order the access to the two registers in what ever way it sees fit. Since the compiler does not know the rule that ADCL must be read first, it will not necessarily read them in the proper order. If you insist on reading the two registers separately, you should put the access to each register on a separate line. But why would you not use ADC where the compiler does know to read both registers in the proper order.

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:

there's a matter where read the ADCH first?

You don't appear to be understanding. You don't read ADCH, your don't read ADCL. Your read ADC and get all 16 (well 10) bits in one go.

(however if you set the ADLAR bit in ADMUX then just read ADCH alone and forget about ADCL all together).

It might be easier if the C header files simply didn't make ADCL available as there's never really any reason for the programmer to try and access it on its own.