How can I catch a chip in if defined?

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

using AVR studio 7.

 

I thought I had this working but its not, I need to do the below here but it simple is not working.  How does the __AVR_xxxxxxxxxxx__ get set. My make files have the right chip names  MCU          = atmega32u4

 

but using the code below, its not landing in the if statements.

 

 

#if defined  (__AVR_ATmega32u2__) || defined (__AVR_AT90USB162__)
    #define _PORT PORTB
    #define _DDR DDRB
    #define _PIN PINB
#else if  defined(__AVR_ATmega32u4__)                      
    #define _PORT PORTF
    #define _DDR DDRF
    #define _PIN PINF
#endif

 

Am I missing an include?

Should I do #if defined  (__AVR_ATmega32u2__ || __AVR_AT90USB162__)

 

Last Edited: Mon. Dec 21, 2020 - 11:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to define it somewhere.  One of your files that gets used, like main.c   Or, it has be be defined somewhere, by somebody else in one of the files.

Just gettin' started, again....

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

ah, makes sense, I thought this could be in a MAKE file. I can use the main, thx.

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

Can I use CCFLAGS = -D like CCFLAGS = -D __AVR_ATmega32u2__  in my MAKE?

Last Edited: Tue. Dec 22, 2020 - 12:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

There should already be a part specific define... Try to compile with preprocessing only and have a look for it, or use save-temps to get the preprocessed c file...

:: Morten

 

(yes, I work for Microchip, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

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

S_K_U_N_X wrote:
How does the __AVR_xxxxxxxxxxx__ get set

See: https://www.nongnu.org/avr-libc/user-manual/using_tools.html

 

S_K_U_N_X wrote:
Should I do #if defined  (__AVR_ATmega32u2__ || __AVR_AT90USB162__) 

No, that would make no sense.

 

As the name suggests, #if defined just checks whether something is defined or not - it doesn't evaluate expressions.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[quote-The GCC Documentation]

-dletters

Says to make debugging dumps during compilation as specified by letters. The flags documented here are those relevant to the preprocessor. Other letters are interpreted by the compiler proper, or reserved for future versions of GCC, and so are silently ignored. If you specify letters whose behavior conflicts, the result is undefined. See Developer Options, for more information.

-dM

Instead of the normal output, generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros. This gives you a way of finding out what is predefined in your version of the preprocessor. Assuming you have no file foo.h, the command

touch foo.h; cpp -dM foo.h

shows all the predefined macros.

 

https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html - near the bottom of the page

 

There's various other options - see the linked page for the full list (near the bottom of the page).

 

EDIT

 

Example: https://www.avrfreaks.net/commen...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Dec 22, 2020 - 12:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OldMicroGuy wrote:

#if defined  (__AVR_ATmega32u2__) || defined (__AVR_AT90USB162__)

Case is significant.  The toolchain defines:

#define __AVR_ATmega32U2__ 1

... for the m32u2.  Note the upper-case 'U'.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

S_K_U_N_X wrote:

#else if  defined(__AVR_ATmega32u4__)

Err No !

You should write:

#elif  defined(__AVR_ATmega32U4__)

 

<edit Added capitalisation fix from #8 />

Last Edited: Tue. Dec 22, 2020 - 08:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
How does the __AVR_xxxxxxxxxxx__ get set.

By a lookup table inside the compiler. You pass - mmcu=atmega32u4 when you build the code and as a consequence it internally defines about 100 things it "knows" about Atmega32u4
.
Try...
.
avr-gcc - mmcu=atmega32u4 -E -dM - < nul
.
to see all the predefines.