ATmega162 - Need to correctly define pins

Hi Everyone... I have an ATmega162 micro controller which I have loaded the boot loader and a simple sketch without any problems. Found everything to do that here:

I can load a sketch without any errors, however, the pins seem to be mapped to the ATmega328. I say that because only 20 of the 35 I/O ports work and the outputs match that of the ATmega328. I know this from running "blink without delay" and finding the outputs are not following the diagram shown in this link:

Can someone please point me in the right direction?

Thanks in advance.

The mapping is in the pins_arduino.h file, you can change them in there if you want.

//                  +-\/-+
// PWM  (D 8) PB0  1|    |40  VCC
// PWM  (D 9) PB1  2|    |39  PA0 (D14)
//      (D19) PB2  3|    |38  PA1 (D15)
//      (D18) PB3  4|    |37  PA2 (D16)
// PWM  (D10) PB4  5|    |36  PA3 (D17)
//      (D11) PB5  6|    |35  PA4 (D31)
//      (D12) PB6  7|    |34  PA5 (D32)
//      (D13) PB7  8|    |33  PA6 (D33)
//            RST  9|    |32  PA7 (D34)
//      (D 0) PD0 10|    |31  PE0 (D28)
//      (D 1) PD1 11|    |30  PE1 (D29)
//      (D 2) PD2 12|    |29  PE2 (D30) PWM
//      (D 3) PD3 13|    |28  PC7 (D27)
// PWM  (D 5) PD4 14|    |27  PC6 (D26)
// PWM  (D 6) PD5 15|    |26  PC5 (D25)
//      (D 4) PD6 16|    |25  PC4 (D24)
//      (D 7) PD7 17|    |24  PC3 (D23)
//            XT2 18|    |23  PC2 (D22)
//            XT1 19|    |22  PC1 (D21)
//            GND 20|    |21  PC0 (D20)
//                  +----+

Having read that instructable, it seems it is just the code I wrote a couple of years ago. I have no problem with it. The mapping is nothing really like the ATMega328, they are completely different microcontrollers. I based the numbering scheme on what was easiest to remember for the specific application that I was using at the time.

Hi Tom, Thank you for the reply. I tried what you suggested however, the results remain the same. When I define LED pin as 14 in the "blink without delay" sketch, physical pin 21 or PCO (20) blinks instead. I am wondering if in the boards.txt file, the wrong pins_arduino.h file is being looked at?


Its possible. If you compile the sketch with the 'Verbose output' option in the IDE preferences, it will show you the directory in which the compiled sketch is placed. In that folder there will be a ".elf" file. Could you upload that here.

Hi Tom, For some reason the program is not creating the .elf file even with the boxes checked in the preferences. The only .elf file I see on my drive are those that were created some time ago. Could I be missing something here? Sorry for the questions. I am not a pro at this yet.


You should see a line like this at the end of the verbose output:

C:\Users\Thomas\AppData\Local\Temp\build1687262069962928034.tmp\sketch_jun21a.cpp.elf C:\Users\Thomas\AppData\Local\Temp\build1687262069962928034.tmp\sketch_jun21a.cpp.hex 
Binary sketch size: 106 bytes (of a 1,472 byte maximum)

The .elf file named there is the one.

Found it… Using the search feature in windows didn’t. Here it is… Attached.

blinkwithoutdelay.cpp.elf (13.5 KB)

You probably have the wrong board selected:
This is in your .elf:

00000070 <port_to_mode_PGM>:
  70:	00 00 00 00 37 00 34 00 31 00                       ....7.4.1.

0000007a <port_to_output_PGM>:
  7a:	00 00 00 00 38 00 35 00 32 00                       ....8.5.2.

00000084 <port_to_input_PGM>:
  84:	00 00 00 00 36 00 33 00 30 00                       ....6.3.0.

This is what it should be:

00000070 <port_to_mode_PGM>:
  70:	00 00 3a 00 37 00 34 00 31 00 26 00                 ..:.7.4.1.&.

0000007c <port_to_output_PGM>:
  7c:	00 00 3b 00 38 00 35 00 32 00 27 00                 ..;.8.5.2.'.

00000088 <port_to_input_PGM>:
  88:	00 00 39 00 36 00 33 00 30 00 25 00                 ..

That's what it seems like. Almost as if the settings are pointing to the Atmega328 instead of Atmega162. I know in my board selection it is the Atmega162 that I am selecting. Here is a part of my "boards.txt" file. Do you see anything wrong with it? I added to the name to give it a more detailed description but that is all I changed from the original. on a breadboard (16Mhz external clock)

atmega162.upload.protocol=arduino atmega162.upload.maximum_size=15360 atmega162.upload.speed=57600

atmega162.bootloader.low_fuses=0xFF atmega162.bootloader.high_fuses=0xD8 atmega162.bootloader.extended_fuses=0xFB atmega162.bootloader.path=optiboot atmega162.bootloader.file=optiboot_atmega162.hex atmega162.bootloader.unlock_bits=0x3F atmega162.bootloader.lock_bits=0x0F

I noticed that nothing is defined for the Atmega162 in the "arduino.h" file located in the core directory. Could this be the root of my problems? Perhaps you can paste what should be there, here.

Just for the sake of argument, add the following line to the top of the pins_arduino.h file for the Atmega162:

#error If you are seeing this, the correct pins_arduino.h file is included

That was a good idea. The program is not referring to the pins_arduino.h file that is located in the variants\atmega162 directory, instead, it is referring to the pins_arduino.h file that is located in the hardware\arduino\cores\arduino directory. So that would indicate that the boards.txt file requires editing or is there something that needs to be added to the arduino.h file in the cores directory?

There shouldn't be a pins_arduino.h file in the "hardware\arduino\cores\arduino" directory. If there is, then that one needs deleting! All of the pins_arduino.h files should be in separate folders in the \hardware\arduino\variants\ directory.

This line in the boards.txt file identifies the folder name in the variants directory from which the header file should be found:

But if there is one in the wrong place, like in the cores folder, that one will obscure the correct one.

That was it! Removing the pins_arduino.h from the cores directory worked.

Thank you very much for all your patience and help. Much appreciated!