ADC data registers: ADCL, ADCH, ADCW, ADC

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

First of all, I would just like to say hi guys! Only just started getting into microprocessors about a month ago, definitely opened my eyes at how easy some things can be!!

Quick question today though, I am using the atmega324pa in atmel studio 6, according to the definitions in iom324pa.h

#ifndef __ASSEMBLER__
#define ADC _SFR_MEM16(0x78)
#endif
#define ADCW _SFR_MEM16(0x78)

#define ADCL _SFR_MEM8(0x78)
#define ADCH _SFR_MEM8(0x79)

The (I never know what to call these...) term ADC is the same as ADCW and is by default ADCL and ADCH put together into a single 16-bit carrier based on the definition.

If this is the case, why is that I cannot find any documentation of this in comparison to everyone continually stating that you should try to combine ADCL and ADCH to create a 10-bit characterization of your input signal.

Note: Bare in mind I only stumbled onto this discovery a few hours ago.

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

ADC & ADCW are indeed the same. They both are special function registers that point to the location of the ADC conversion result register. They are defined as 16Bit.

ADCL reads only the low byte (8bit) of the ADC conversion result and ADCH reads only the High byte.

IIRC normal reading is first read ADCL and then read ADCH to tell the ADC peripheral that full result is read and next conversion result can be written.

Normally in C code you would read ADC to get the full result. This will result in the compiler first reading ADCL and then ADCH.

If you use ADLAR then you can also switch to 8bit mode. Then you only have to read ADCH.

Hope this clarifies a bit

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

Quote:
If this is the case, why is that I cannot find any documentation of this in comparison to everyone continually stating that you should try to combine ADCL and ADCH to create a 10-bit characterization of your input signal.

Actually, what I see happening much more often is that people believe that they MUST combine ADCL and ADCH ( or other 16-bit registers ) themselves and, by doing so, they run afoul of the fact that they must be read in the proper order. Using ADC/ADCW solves the issue and is typically the suggestion that solves the OP's problem.

As to why there are both ADC and ADCW, well, the AVR assembly has the adc instruction ( add with carry ) and, given that the assembler is case independent, ADC would conflict. As you can see from the fragment that you quoted, ADC is only defined when the preprocessor macro __ASSEMBLER__ is not defined... that is, when the header is not used by the assembler. The assembler only sees ADCW.

So the choice is to use ADCW in all code or to split with ADCW in asm and ADC in C/C++. Both are functionally identical, it is just a difference of programming style.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

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

Quote:

is to use ADCW in all code

The fact that ADCW works in all code and the hint given to the reader by W(=Word) that it is 16 bit wide seem like two good reasons to prefer ADCW over plain ADC (I do).

ADCL might as well not exist (if it didn't it would force people to use the more sensible ADC/ADCW) and the only use of ADCH is when the ADLAR flag is set.

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

> seem like two good reasons to prefer ADCW over plain ADC

ADC is there since the common notation for regH/regL "automatic" 16-bit
pseudo registers is to derive their name by just omitting the L/H suffix.

ADCW is mainly there for assembly programmers since ADC is unavailable
in assembly language as it already refers to an opcode of that name.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Same convention works for reading and clearing TCNT1 for example rather than TCNT1H and L

Imagecraft compiler user

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

Quote:

Same convention

Not entirely as there isn't a TCNT1W - but I guess that's because there doesn't need to be as it wouldn't conflict with an Asm mnemonic.

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

thanks for the help guys!