Enumerating pins (on ATmega2560) with OrangutanDigital

In the OrangutanDigital library the pins and ports for the ATmega1284p (Orangutan SVP) are not enumerated in a simple linear manner, but rather some of the ports are out of order, and some of the pin assignments are reversed (with MSB having a lower number than the LSB).

Eg…

#elif defined(__AVR_ATmega324P__)  || defined(__AVR_ATmega644P__)|| defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega644PA__)

// port D pins
#define IO_D0				0
#define IO_D1				1
#define IO_D2				2
#define IO_D3				3
#define IO_D4				4
#define IO_D5				5
#define IO_D6				6
#define IO_D7				7

// port B pins
#define IO_B0				8
#define IO_B1				9
#define IO_B2				10
#define IO_B3				11
#define IO_B4				12
#define IO_B5				13
#define IO_B6				14
#define IO_B7				15

// port C pins
#define IO_C0				16
#define IO_C1				17
#define IO_C2				18
#define IO_C3				19
#define IO_C4				20
#define IO_C5				21
#define IO_C6				22
#define IO_C7				23

// port A pins
#define IO_A0				24
#define IO_A1				25
#define IO_A2				26
#define IO_A3				27
#define IO_A4				28
#define IO_A5				29
#define IO_A6				30
#define IO_A7				31

Here the pins are flipped.

#if defined(_ORANGUTAN_SVP) || defined(_ORANGUTAN_X2)

		else if (pin < 16)		// pin 8 = PB0, ..., 15 = PB7
		{
			io->pinRegister = (unsigned char*)&PINB;
			io->portRegister = (unsigned char*)&PORTB;
			io->ddrRegister = (unsigned char*)&DDRB;
			io->bitmask = 1 << (pin - 8);
		}
		else if (pin < 24)		// pin 16 = PC0, ..., 23 = PC7
		{
			io->pinRegister = (unsigned char*)&PINC;
			io->portRegister = (unsigned char*)&PORTC;
			io->ddrRegister = (unsigned char*)&DDRC;
			io->bitmask = 1 << (pin - 16);
		}
		else if (pin < 32)		// pin 24 = PA7, ..., 31 = PA0
		{
			io->pinRegister = (unsigned char*)&PINA;
			io->portRegister = (unsigned char*)&PORTA;
			io->ddrRegister = (unsigned char*)&DDRA;
			io->bitmask = 1 << (31 - pin);
		}

#else

Is there are reason behind this?

I’m asking because I want to use your code for the Arduino Mega2560, and it didn’t work when I just enumerated the ATmega2560 ports & pins in the simple linear way. So, I thought I’d ask first before spending to much time to debug.

Regards, Phillip

Hello, feilipu.

The library should work fine even if you change the pin numbering. The only unusual thing in our pin definitions is that Port A is reversed for ATmega324/644/1284 processors. We don’t remember exactly why we did it that way, but we were probably copying the pin numbering of some existing Arduino clone. The reversal of the pins on that clone was probably motivated by the fact that the PORTA pins on the actual chip are reversed in relation to the other ports.

–David