Code from Nano not running on Leonardo and which Arduinos using Atmega328?


I am wondering which are the Arduinos using the Atmega328 since i have code already for the Arduino Nano and porting them to certain Arduinos using a different microcontroller is being problematic since the registers don't seem to be compatible.

Here is the list of boards that i have found using the Atmega328.

  • UNO
  • Pro Mini
  • Nano
    Am i missing any?

And somehow, in the Arduino IDE when i'm compiling my code, i selected the MEGA and it worked?! The MEGA uses the ATmega2560. But if i try to compile the same code for the Leonardo (i really need the extra serial port), i get the following error in the Arduino IDE:

'TCCR2A' was not declared in this scope
'TCCR2B' was not declared in this scope
'TIMSK2' was not declared in this scope

So, i suppose that i have no choice but to rewrite the code, if i want to run the same Arduino Nano compatible code on the Leonardo which uses the ATmega32u4 microcontroller?

For the official Arduino's that's all I think.

But want is using the non-portable code? Is it a library or your code? In case of the last, why?

And why do you need two serial ports?

If you want to maintain compatibility of the code with different boards, you can use compiler directives to selectively compile different code for the different boards, such as:

#if defined(__AVR_ATmega32U4__)
//code specific to atmega32u4 board

#if defined(__AVR_ATmega328P__)
//code specific to atmega328 board

#if defined(__AVR_ATmega2560__)
//code specific to atmega2560 board

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega2560__)
//code for atmega328 or atmega2560 boards