This is a question that you probably already asked yourself when trying to write code that must work with different Arduino's variants. Indeed, when using I2C, timers or PWM, the pins concerned on the Arduino Uno for these operations are different from Arduino Mega or form Due, etc. Hence, how to avoid the multiplication of the
source files, of the classes or to excessively overload functions?
Solution: use conditional compilation!
In this short note we try to explain how to do that. Indeed, digging into the avr include files (see
These are used to recognize that we are compiling for Arduino Uno. For the Arduino Mega we find:
Therefore your code might look like:
Solution: use conditional compilation!
In this short note we try to explain how to do that. Indeed, digging into the avr include files (see
io.h
for example) one can find the following constants are used:
__AVR_ATmega328P__ __AVR_ATmega328__
These are used to recognize that we are compiling for Arduino Uno. For the Arduino Mega we find:
__AVR_ATmega1280__ __AVR_ATmega2560__
Therefore your code might look like:
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328P__) [your arduino uno code here] #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) [your arduino mega code here]
No comments :
Post a Comment