Why is "ADC" pre-defined?

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

Is it just to confuse me when I try to define ADC in my own code or is there a more sinister motive? :)

I have LCD and USART types defined as classes because it seems like a good idea to me. But when I try to define ADC as a type, or more confusingly, when I think I have it defined and try to use it, strange things happen.

Defining the cryptic register names in the GNU header files is good and I would never inadvertantly try to define those myself. But I would have never guessed that ADC was off limits.

I hope that TIME and TEMPERATURE are still available for my own use. :)

From iom329.h:

/* Combine ADCL and ADCH */
#ifndef __ASSEMBLER__
#define ADC _SFR_MEM16(0x78)
#endif
#define ADCW _SFR_MEM16(0x78)
#define ADCL _SFR_MEM8(0x78)
#define ADCH _SFR_MEM8(0x79)

#define ADCSRA _SFR_MEM8(0x7A)

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

Quote:
I try to define ADC as a type

C is case sensitive. so expressions like adc, Adc, ADc,... are valid.
In addition it's common to add a postfix to user types like _t.
So you get adc_t, ...
But still there could be some confusion left with the ADC SFR registers.

Regards
Sebastian

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

the ADC data register has high 8-bit part called ADCH and low 8-bit part ADCL, and because nobody cares to read the datasheet in which order the ADCH and ADCL registers must be accessed to get the correct 16-bit result, there is a 16-bit register defined with the name of ADCW, and using this gives the whole 16-bit result correctly. And it seems that also not using assembler code also defines ADC like ADCW.

- Jani

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

The common notion for this kind of 16-bit register pair is to just strip
off the trailing `H' and `L', and use the resulting name as the name of
a 16-bit pseudo register.

However, this causes the issue for assembly code that adc is also a valid
assembler mnemonic (add with carry), and as some assembly hackers prefer
to use ALL CAPS, they write it ADC. For that reason, the alias name ADCW
has been assigned, and this will be the only name recognized inside assembly
code. C code is free to use either, ADC or ADCW.

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

I understand. I guess it depends on your point of view. I would say a C programmer is not free to use ADC, but rather constrained to use it only to refer to a particular register pair.

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

S-Sohn wrote:
In addition it's common to add a postfix to user types like _t.
So you get adc_t, ...

Sebastian

Is it customary to add a postfix to struct names? I guess structs are types in C, I know they are in C++.

Where I encounter the _t suffix is the type size_t.

Steve

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

As far as I know does the C99 standard contain fixed width integer types. These types are named uint8_t, int8_t, uint16_t, ... Some companies have instructed a suffix for user types since many years in their coding guidelines. In my company we have to use the _T suffix.

A user type can be a struct or a simple typedef like
typedef unsigned int uint8_t;

Regards
Sebastian

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

> I guess structs are types in C

No, they aren't, this is one of the differences to C++. They form
a so-called tag namespace for structs, i. e. they are only defined
when preceded by the keyword "struct".

However, it is common C style to use all-CAPS names solely for
defining macros (and after all, that's what the ADC from
actually is), but use lower case or mixed case for variable names.
So it is not uncommon you could find code like:

ISR(ADC_vect)
{
   uint16_t adc;

   adc = ADC;
   /* ... */
}

Jörg Wunsch

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