There are a lot of #define
-d constants in Arduino.
Most of them are from avr-gcc compiler but some are also Arduino specific. Let’s take a look at some interesting ones.
Gcc constants
__DATE__
and __TIME__
Date and time of compilation.
__FILE__
and __LINE__
FILE is full path to current compiled file and LINE is current line number inside this file. However FILE is not *.pde (sketch file), it’s *.cpp. If you don’t know how Arduino build process works you will probably don’t need to use this constants. However it would be useful in libraries.
__func__
Name of current function.
void setup() { Serial.println(__func__); // prints "setup" }
__COUNTER__
Every time you use this one, it increases its value.
Serial.println(__COUNTER__); // 0 Serial.println(__COUNTER__); // 1 Serial.println(__COUNTER__); // 2
CPU Type and How to Determine Arduino Bord
According to selected target processor, compiler defined constant by processor name.
Constant | CPU | Board |
---|---|---|
__AVR_ATmega168__ |
ATmega 168 | Arduino Decimilia and older |
__AVR_ATmega328P__ |
ATmega 328P | Arduino Duemilanove and Uno |
__AVR_ATmega1280__ |
ATmega 1280 | Arduino Mega |
__AVR_ATmega2560__ |
ATmega 2560 | Arduino Mega 2560 |
__AVR_ATmega32U4__ |
ATmega 32U4 | Arduino Leonardo |
__SAM3X8E__ |
AT91SAM3X8E | Arduino Due |
So by testing of these constants you can determine board type. For example:
#if not (defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)) #error Sorry dude, this program works only on Arduino Mega #endif
Another example is in sketch in the end of article.
Arduino Specific Constant
Arduino compiler define these constants
ARDUINO
Version of Arduino SW. So if you use Arduino 0022 you wil get:
Serial.println(ARDUINO); // 22
F_CPU
Frequency of oscillator on the board (in hertz). For example on Arduino Uno you will get:
Serial.println(F_CPU); // 16000000
Try it
You can try example sketch on your board.
Thanks for posting this, I keep coming back here when I forget the exact format of the CPU names!
ReplyDeleteOne to add might be the (__AVR_ATmega32U4__), comes in very handy for dealing with the new pin assignments!
Thanks for suggestion, Tom. I added processor type for Arduino Leonardo and Due.
DeleteAdam, you can add the Arduino Yun under ATmega32U4. Thanks!
ReplyDeleteNo doubt it will be very useful for my future projects. Would like to see some other posts on the same subject!
ReplyDeleteused pcb equipment
Where I could find all the pre defined constants in AVR-GCC?
ReplyDelete謝謝
ReplyDeleteHi great readding your blog
ReplyDelete