Digital read/write pinMode : fast execution

Every now and then I need to turn pins on or off quickly (like in an interrupt service routine) and have to reach for my "pins mapping" sheet to see exactly what port I required, and what bit.

With a small Lua script I generated a file which has all the input/output read/write mappings you should need. This is for the Atmega328 (eg. Uno, Duemilanove).

To use, just skim down for the pinMode/digitalRead/digitalWrite you want, and copy and paste that line.

Atmega328:

// --- PIN MODE: OUTPUT  --- 

  DDRD |= _BV (0); // pinMode (0, OUTPUT);
  DDRD |= _BV (1); // pinMode (1, OUTPUT);
  DDRD |= _BV (2); // pinMode (2, OUTPUT);
  DDRD |= _BV (3); // pinMode (3, OUTPUT);
  DDRD |= _BV (4); // pinMode (4, OUTPUT);
  DDRD |= _BV (5); // pinMode (5, OUTPUT);
  DDRD |= _BV (6); // pinMode (6, OUTPUT);
  DDRD |= _BV (7); // pinMode (7, OUTPUT);
  DDRB |= _BV (0); // pinMode (8, OUTPUT);
  DDRB |= _BV (1); // pinMode (9, OUTPUT);
  DDRB |= _BV (2); // pinMode (10, OUTPUT);
  DDRB |= _BV (3); // pinMode (11, OUTPUT);
  DDRB |= _BV (4); // pinMode (12, OUTPUT);
  DDRB |= _BV (5); // pinMode (13, OUTPUT);
  DDRC |= _BV (0); // pinMode (A0, OUTPUT);
  DDRC |= _BV (1); // pinMode (A1, OUTPUT);
  DDRC |= _BV (2); // pinMode (A2, OUTPUT);
  DDRC |= _BV (3); // pinMode (A3, OUTPUT);
  DDRC |= _BV (4); // pinMode (A4, OUTPUT);
  DDRC |= _BV (5); // pinMode (A5, OUTPUT);

// --- PIN MODE: INPUT  --- 

  DDRD &= ~_BV (0); // pinMode (0, INPUT);
  DDRD &= ~_BV (1); // pinMode (1, INPUT);
  DDRD &= ~_BV (2); // pinMode (2, INPUT);
  DDRD &= ~_BV (3); // pinMode (3, INPUT);
  DDRD &= ~_BV (4); // pinMode (4, INPUT);
  DDRD &= ~_BV (5); // pinMode (5, INPUT);
  DDRD &= ~_BV (6); // pinMode (6, INPUT);
  DDRD &= ~_BV (7); // pinMode (7, INPUT);
  DDRB &= ~_BV (0); // pinMode (8, INPUT);
  DDRB &= ~_BV (1); // pinMode (9, INPUT);
  DDRB &= ~_BV (2); // pinMode (10, INPUT);
  DDRB &= ~_BV (3); // pinMode (11, INPUT);
  DDRB &= ~_BV (4); // pinMode (12, INPUT);
  DDRB &= ~_BV (5); // pinMode (13, INPUT);
  DDRC &= ~_BV (0); // pinMode (A0, INPUT);
  DDRC &= ~_BV (1); // pinMode (A1, INPUT);
  DDRC &= ~_BV (2); // pinMode (A2, INPUT);
  DDRC &= ~_BV (3); // pinMode (A3, INPUT);
  DDRC &= ~_BV (4); // pinMode (A4, INPUT);
  DDRC &= ~_BV (5); // pinMode (A5, INPUT);

// --- DIGITAL WRITE: HIGH  --- 

  PORTD |= _BV (0); // digitalWrite (0, HIGH);
  PORTD |= _BV (1); // digitalWrite (1, HIGH);
  PORTD |= _BV (2); // digitalWrite (2, HIGH);
  PORTD |= _BV (3); // digitalWrite (3, HIGH);
  PORTD |= _BV (4); // digitalWrite (4, HIGH);
  PORTD |= _BV (5); // digitalWrite (5, HIGH);
  PORTD |= _BV (6); // digitalWrite (6, HIGH);
  PORTD |= _BV (7); // digitalWrite (7, HIGH);
  PORTB |= _BV (0); // digitalWrite (8, HIGH);
  PORTB |= _BV (1); // digitalWrite (9, HIGH);
  PORTB |= _BV (2); // digitalWrite (10, HIGH);
  PORTB |= _BV (3); // digitalWrite (11, HIGH);
  PORTB |= _BV (4); // digitalWrite (12, HIGH);
  PORTB |= _BV (5); // digitalWrite (13, HIGH);
  PORTC |= _BV (0); // digitalWrite (A0, HIGH);
  PORTC |= _BV (1); // digitalWrite (A1, HIGH);
  PORTC |= _BV (2); // digitalWrite (A2, HIGH);
  PORTC |= _BV (3); // digitalWrite (A3, HIGH);
  PORTC |= _BV (4); // digitalWrite (A4, HIGH);
  PORTC |= _BV (5); // digitalWrite (A5, HIGH);

// --- DIGITAL WRITE: LOW  --- 

  PORTD &= ~_BV (0); // digitalWrite (0, LOW);
  PORTD &= ~_BV (1); // digitalWrite (1, LOW);
  PORTD &= ~_BV (2); // digitalWrite (2, LOW);
  PORTD &= ~_BV (3); // digitalWrite (3, LOW);
  PORTD &= ~_BV (4); // digitalWrite (4, LOW);
  PORTD &= ~_BV (5); // digitalWrite (5, LOW);
  PORTD &= ~_BV (6); // digitalWrite (6, LOW);
  PORTD &= ~_BV (7); // digitalWrite (7, LOW);
  PORTB &= ~_BV (0); // digitalWrite (8, LOW);
  PORTB &= ~_BV (1); // digitalWrite (9, LOW);
  PORTB &= ~_BV (2); // digitalWrite (10, LOW);
  PORTB &= ~_BV (3); // digitalWrite (11, LOW);
  PORTB &= ~_BV (4); // digitalWrite (12, LOW);
  PORTB &= ~_BV (5); // digitalWrite (13, LOW);
  PORTC &= ~_BV (0); // digitalWrite (A0, LOW);
  PORTC &= ~_BV (1); // digitalWrite (A1, LOW);
  PORTC &= ~_BV (2); // digitalWrite (A2, LOW);
  PORTC &= ~_BV (3); // digitalWrite (A3, LOW);
  PORTC &= ~_BV (4); // digitalWrite (A4, LOW);
  PORTC &= ~_BV (5); // digitalWrite (A5, LOW);

// --- DIGITAL READ  --- 

  x = (PIND & _BV (0)) == 0; // digitalRead (0);
  x = (PIND & _BV (1)) == 0; // digitalRead (1);
  x = (PIND & _BV (2)) == 0; // digitalRead (2);
  x = (PIND & _BV (3)) == 0; // digitalRead (3);
  x = (PIND & _BV (4)) == 0; // digitalRead (4);
  x = (PIND & _BV (5)) == 0; // digitalRead (5);
  x = (PIND & _BV (6)) == 0; // digitalRead (6);
  x = (PIND & _BV (7)) == 0; // digitalRead (7);
  x = (PINB & _BV (0)) == 0; // digitalRead (8);
  x = (PINB & _BV (1)) == 0; // digitalRead (9);
  x = (PINB & _BV (2)) == 0; // digitalRead (10);
  x = (PINB & _BV (3)) == 0; // digitalRead (11);
  x = (PINB & _BV (4)) == 0; // digitalRead (12);
  x = (PINB & _BV (5)) == 0; // digitalRead (13);
  x = (PINC & _BV (0)) == 0; // digitalRead (A0);
  x = (PINC & _BV (1)) == 0; // digitalRead (A1);
  x = (PINC & _BV (2)) == 0; // digitalRead (A2);
  x = (PINC & _BV (3)) == 0; // digitalRead (A3);
  x = (PINC & _BV (4)) == 0; // digitalRead (A4);
  x = (PINC & _BV (5)) == 0; // digitalRead (A5);

Lua code to generate this:

pins = {
  { name = "0",  port = "D", bit = 0 },
  { name = "1",  port = "D", bit = 1 },
  { name = "2",  port = "D", bit = 2 },
  { name = "3",  port = "D", bit = 3 },
  { name = "4",  port = "D", bit = 4 },
  { name = "5",  port = "D", bit = 5 },
  { name = "6",  port = "D", bit = 6 },
  { name = "7",  port = "D", bit = 7 },

  { name = "8",  port = "B", bit = 0 },
  { name = "9",  port = "B", bit = 1 },
  { name = "10", port = "B", bit = 2 },
  { name = "11", port = "B", bit = 3 },
  { name = "12", port = "B", bit = 4 },
  { name = "13", port = "B", bit = 5 },

  { name = "A0", port = "C", bit = 0 },
  { name = "A1", port = "C", bit = 1 },
  { name = "A2", port = "C", bit = 2 },
  { name = "A3", port = "C", bit = 3 },
  { name = "A4", port = "C", bit = 4 },
  { name = "A5", port = "C", bit = 5 },
} -- end of pins


print ""
print ("// --- PIN MODE: OUTPUT  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  DDR" .. pin.port .. " |= _BV (" .. pin.bit .. "); // pinMode (" .. pin.name .. ", OUTPUT);")
end -- for

print ""
print ("// --- PIN MODE: INPUT  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  DDR" .. pin.port .. " &= ~_BV (" .. pin.bit .. "); // pinMode (" .. pin.name .. ", INPUT);")
end -- for


print ""
print ("// --- DIGITAL WRITE: HIGH  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  PORT" .. pin.port .. " |= _BV (" .. pin.bit .. "); // digitalWrite (" .. pin.name .. ", HIGH);")
end -- for


print ""
print ("// --- DIGITAL WRITE: LOW  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  PORT" .. pin.port .. " &= ~_BV (" .. pin.bit .. "); // digitalWrite (" .. pin.name .. ", LOW);")
end -- for

print ""
print ("// --- DIGITAL READ  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  x = (PIN" .. pin.port .. " & _BV (" .. pin.bit .. ")) == 0; // digitalRead (" .. pin.name .. ");")
end -- for

Cool. Can you create same for a 1284 please?

I know this sounds crazy, but I'm struggling to find the mappings (pin "name" to port). Can you fill this in? Basically it means pin D0 is port D, bit 0, and so on. Then I can run the script on it.

pins = {
  { name = "0",  port = "D", bit = 0 },
  { name = "1",  port = "D", bit = 1 },
  { name = "2",  port = "D", bit = 2 },
  { name = "3",  port = "D", bit = 3 },
  { name = "4",  port = "D", bit = 4 },
  { name = "5",  port = "D", bit = 5 },
  { name = "6",  port = "D", bit = 6 },
  { name = "7",  port = "D", bit = 7 },

  { name = "8",  port = "B", bit = 0 },
  { name = "9",  port = "B", bit = 1 },
  { name = "10", port = "B", bit = 2 },
  { name = "11", port = "B", bit = 3 },
  { name = "12", port = "B", bit = 4 },
  { name = "13", port = "B", bit = 5 },

  { name = "A0", port = "C", bit = 0 },
  { name = "A1", port = "C", bit = 1 },
  { name = "A2", port = "C", bit = 2 },
  { name = "A3", port = "C", bit = 3 },
  { name = "A4", port = "C", bit = 4 },
  { name = "A5", port = "C", bit = 5 },
} -- end of pins

Pins as defined by maniacbug, following Sanguino lead I think.

pins = {
{ name = "0", port = "B", bit = 0 },
{ name = "1", port = "B", bit = 1 },
{ name = "2", port = "B", bit = 2 },
{ name = "3", port = "B", bit = 3 },
{ name = "4", port = "B", bit = 4 },
{ name = "5", port = "B", bit = 5 },
{ name = "6", port = "B", bit = 6 },
{ name = "7", port = "B", bit = 7 },

{ name = "8", port = "D", bit = 0 },
{ name = "9", port = "D", bit = 1 },
{ name = "10", port = "D", bit = 2 },
{ name = "11", port = "D", bit = 3 },
{ name = "12", port = "D", bit = 4 },
{ name = "13", port = "D", bit = 5 },
{ name = "14", port = "D", bit = 6 },
{ name = "15", port = "D", bit = 7 },

{ name = "16", port = "C", bit = 0 },
{ name = "17", port = "C", bit = 1 },
{ name = "18", port = "C", bit = 2 },
{ name = "18", port = "C", bit = 3 },
{ name = "20", port = "C", bit = 4 },
{ name = "21", port = "C", bit = 5 },
{ name = "22", port = "C", bit = 6 },
{ name = "23", port = "C", bit = 7 },

{ name = "A0", port = "A", bit = 0 },
{ name = "A1", port = "A", bit = 1 },
{ name = "A2", port = "A", bit = 2 },
{ name = "A3", port = "A", bit = 3 },
{ name = "A4", port = "A", bit = 4 },
{ name = "A5", port = "A", bit = 5 },
{ name = "A6", port = "A", bit = 6 },
{ name = "A7", port = "A", bit = 7 },

} -- end of pins

Given the above data:

For the Atmega1284:

// --- PIN MODE: OUTPUT  --- 

  DDRB |= _BV (0); // pinMode (0, OUTPUT);
  DDRB |= _BV (1); // pinMode (1, OUTPUT);
  DDRB |= _BV (2); // pinMode (2, OUTPUT);
  DDRB |= _BV (3); // pinMode (3, OUTPUT);
  DDRB |= _BV (4); // pinMode (4, OUTPUT);
  DDRB |= _BV (5); // pinMode (5, OUTPUT);
  DDRB |= _BV (6); // pinMode (6, OUTPUT);
  DDRB |= _BV (7); // pinMode (7, OUTPUT);
  DDRD |= _BV (0); // pinMode (8, OUTPUT);
  DDRD |= _BV (1); // pinMode (9, OUTPUT);
  DDRD |= _BV (2); // pinMode (10, OUTPUT);
  DDRD |= _BV (3); // pinMode (11, OUTPUT);
  DDRD |= _BV (4); // pinMode (12, OUTPUT);
  DDRD |= _BV (5); // pinMode (13, OUTPUT);
  DDRD |= _BV (6); // pinMode (14, OUTPUT);
  DDRD |= _BV (7); // pinMode (15, OUTPUT);
  DDRC |= _BV (0); // pinMode (16, OUTPUT);
  DDRC |= _BV (1); // pinMode (17, OUTPUT);
  DDRC |= _BV (2); // pinMode (18, OUTPUT);
  DDRC |= _BV (3); // pinMode (18, OUTPUT);
  DDRC |= _BV (4); // pinMode (20, OUTPUT);
  DDRC |= _BV (5); // pinMode (21, OUTPUT);
  DDRC |= _BV (6); // pinMode (22, OUTPUT);
  DDRC |= _BV (7); // pinMode (23, OUTPUT);
  DDRA |= _BV (0); // pinMode (A0, OUTPUT);
  DDRA |= _BV (1); // pinMode (A1, OUTPUT);
  DDRA |= _BV (2); // pinMode (A2, OUTPUT);
  DDRA |= _BV (3); // pinMode (A3, OUTPUT);
  DDRA |= _BV (4); // pinMode (A4, OUTPUT);
  DDRA |= _BV (5); // pinMode (A5, OUTPUT);
  DDRA |= _BV (6); // pinMode (A6, OUTPUT);
  DDRA |= _BV (7); // pinMode (A7, OUTPUT);

// --- PIN MODE: INPUT  --- 

  DDRB &= ~_BV (0); // pinMode (0, INPUT);
  DDRB &= ~_BV (1); // pinMode (1, INPUT);
  DDRB &= ~_BV (2); // pinMode (2, INPUT);
  DDRB &= ~_BV (3); // pinMode (3, INPUT);
  DDRB &= ~_BV (4); // pinMode (4, INPUT);
  DDRB &= ~_BV (5); // pinMode (5, INPUT);
  DDRB &= ~_BV (6); // pinMode (6, INPUT);
  DDRB &= ~_BV (7); // pinMode (7, INPUT);
  DDRD &= ~_BV (0); // pinMode (8, INPUT);
  DDRD &= ~_BV (1); // pinMode (9, INPUT);
  DDRD &= ~_BV (2); // pinMode (10, INPUT);
  DDRD &= ~_BV (3); // pinMode (11, INPUT);
  DDRD &= ~_BV (4); // pinMode (12, INPUT);
  DDRD &= ~_BV (5); // pinMode (13, INPUT);
  DDRD &= ~_BV (6); // pinMode (14, INPUT);
  DDRD &= ~_BV (7); // pinMode (15, INPUT);
  DDRC &= ~_BV (0); // pinMode (16, INPUT);
  DDRC &= ~_BV (1); // pinMode (17, INPUT);
  DDRC &= ~_BV (2); // pinMode (18, INPUT);
  DDRC &= ~_BV (3); // pinMode (18, INPUT);
  DDRC &= ~_BV (4); // pinMode (20, INPUT);
  DDRC &= ~_BV (5); // pinMode (21, INPUT);
  DDRC &= ~_BV (6); // pinMode (22, INPUT);
  DDRC &= ~_BV (7); // pinMode (23, INPUT);
  DDRA &= ~_BV (0); // pinMode (A0, INPUT);
  DDRA &= ~_BV (1); // pinMode (A1, INPUT);
  DDRA &= ~_BV (2); // pinMode (A2, INPUT);
  DDRA &= ~_BV (3); // pinMode (A3, INPUT);
  DDRA &= ~_BV (4); // pinMode (A4, INPUT);
  DDRA &= ~_BV (5); // pinMode (A5, INPUT);
  DDRA &= ~_BV (6); // pinMode (A6, INPUT);
  DDRA &= ~_BV (7); // pinMode (A7, INPUT);

// --- DIGITAL WRITE: HIGH  --- 

  PORTB |= _BV (0); // digitalWrite (0, HIGH);
  PORTB |= _BV (1); // digitalWrite (1, HIGH);
  PORTB |= _BV (2); // digitalWrite (2, HIGH);
  PORTB |= _BV (3); // digitalWrite (3, HIGH);
  PORTB |= _BV (4); // digitalWrite (4, HIGH);
  PORTB |= _BV (5); // digitalWrite (5, HIGH);
  PORTB |= _BV (6); // digitalWrite (6, HIGH);
  PORTB |= _BV (7); // digitalWrite (7, HIGH);
  PORTD |= _BV (0); // digitalWrite (8, HIGH);
  PORTD |= _BV (1); // digitalWrite (9, HIGH);
  PORTD |= _BV (2); // digitalWrite (10, HIGH);
  PORTD |= _BV (3); // digitalWrite (11, HIGH);
  PORTD |= _BV (4); // digitalWrite (12, HIGH);
  PORTD |= _BV (5); // digitalWrite (13, HIGH);
  PORTD |= _BV (6); // digitalWrite (14, HIGH);
  PORTD |= _BV (7); // digitalWrite (15, HIGH);
  PORTC |= _BV (0); // digitalWrite (16, HIGH);
  PORTC |= _BV (1); // digitalWrite (17, HIGH);
  PORTC |= _BV (2); // digitalWrite (18, HIGH);
  PORTC |= _BV (3); // digitalWrite (18, HIGH);
  PORTC |= _BV (4); // digitalWrite (20, HIGH);
  PORTC |= _BV (5); // digitalWrite (21, HIGH);
  PORTC |= _BV (6); // digitalWrite (22, HIGH);
  PORTC |= _BV (7); // digitalWrite (23, HIGH);
  PORTA |= _BV (0); // digitalWrite (A0, HIGH);
  PORTA |= _BV (1); // digitalWrite (A1, HIGH);
  PORTA |= _BV (2); // digitalWrite (A2, HIGH);
  PORTA |= _BV (3); // digitalWrite (A3, HIGH);
  PORTA |= _BV (4); // digitalWrite (A4, HIGH);
  PORTA |= _BV (5); // digitalWrite (A5, HIGH);
  PORTA |= _BV (6); // digitalWrite (A6, HIGH);
  PORTA |= _BV (7); // digitalWrite (A7, HIGH);

// --- DIGITAL WRITE: LOW  --- 

  PORTB &= ~_BV (0); // digitalWrite (0, LOW);
  PORTB &= ~_BV (1); // digitalWrite (1, LOW);
  PORTB &= ~_BV (2); // digitalWrite (2, LOW);
  PORTB &= ~_BV (3); // digitalWrite (3, LOW);
  PORTB &= ~_BV (4); // digitalWrite (4, LOW);
  PORTB &= ~_BV (5); // digitalWrite (5, LOW);
  PORTB &= ~_BV (6); // digitalWrite (6, LOW);
  PORTB &= ~_BV (7); // digitalWrite (7, LOW);
  PORTD &= ~_BV (0); // digitalWrite (8, LOW);
  PORTD &= ~_BV (1); // digitalWrite (9, LOW);
  PORTD &= ~_BV (2); // digitalWrite (10, LOW);
  PORTD &= ~_BV (3); // digitalWrite (11, LOW);
  PORTD &= ~_BV (4); // digitalWrite (12, LOW);
  PORTD &= ~_BV (5); // digitalWrite (13, LOW);
  PORTD &= ~_BV (6); // digitalWrite (14, LOW);
  PORTD &= ~_BV (7); // digitalWrite (15, LOW);
  PORTC &= ~_BV (0); // digitalWrite (16, LOW);
  PORTC &= ~_BV (1); // digitalWrite (17, LOW);
  PORTC &= ~_BV (2); // digitalWrite (18, LOW);
  PORTC &= ~_BV (3); // digitalWrite (18, LOW);
  PORTC &= ~_BV (4); // digitalWrite (20, LOW);
  PORTC &= ~_BV (5); // digitalWrite (21, LOW);
  PORTC &= ~_BV (6); // digitalWrite (22, LOW);
  PORTC &= ~_BV (7); // digitalWrite (23, LOW);
  PORTA &= ~_BV (0); // digitalWrite (A0, LOW);
  PORTA &= ~_BV (1); // digitalWrite (A1, LOW);
  PORTA &= ~_BV (2); // digitalWrite (A2, LOW);
  PORTA &= ~_BV (3); // digitalWrite (A3, LOW);
  PORTA &= ~_BV (4); // digitalWrite (A4, LOW);
  PORTA &= ~_BV (5); // digitalWrite (A5, LOW);
  PORTA &= ~_BV (6); // digitalWrite (A6, LOW);
  PORTA &= ~_BV (7); // digitalWrite (A7, LOW);

// --- DIGITAL READ  --- 

  x = (PINB & _BV (0)) == 0; // digitalRead (0);
  x = (PINB & _BV (1)) == 0; // digitalRead (1);
  x = (PINB & _BV (2)) == 0; // digitalRead (2);
  x = (PINB & _BV (3)) == 0; // digitalRead (3);
  x = (PINB & _BV (4)) == 0; // digitalRead (4);
  x = (PINB & _BV (5)) == 0; // digitalRead (5);
  x = (PINB & _BV (6)) == 0; // digitalRead (6);
  x = (PINB & _BV (7)) == 0; // digitalRead (7);
  x = (PIND & _BV (0)) == 0; // digitalRead (8);
  x = (PIND & _BV (1)) == 0; // digitalRead (9);
  x = (PIND & _BV (2)) == 0; // digitalRead (10);
  x = (PIND & _BV (3)) == 0; // digitalRead (11);
  x = (PIND & _BV (4)) == 0; // digitalRead (12);
  x = (PIND & _BV (5)) == 0; // digitalRead (13);
  x = (PIND & _BV (6)) == 0; // digitalRead (14);
  x = (PIND & _BV (7)) == 0; // digitalRead (15);
  x = (PINC & _BV (0)) == 0; // digitalRead (16);
  x = (PINC & _BV (1)) == 0; // digitalRead (17);
  x = (PINC & _BV (2)) == 0; // digitalRead (18);
  x = (PINC & _BV (3)) == 0; // digitalRead (18);
  x = (PINC & _BV (4)) == 0; // digitalRead (20);
  x = (PINC & _BV (5)) == 0; // digitalRead (21);
  x = (PINC & _BV (6)) == 0; // digitalRead (22);
  x = (PINC & _BV (7)) == 0; // digitalRead (23);
  x = (PINA & _BV (0)) == 0; // digitalRead (A0);
  x = (PINA & _BV (1)) == 0; // digitalRead (A1);
  x = (PINA & _BV (2)) == 0; // digitalRead (A2);
  x = (PINA & _BV (3)) == 0; // digitalRead (A3);
  x = (PINA & _BV (4)) == 0; // digitalRead (A4);
  x = (PINA & _BV (5)) == 0; // digitalRead (A5);
  x = (PINA & _BV (6)) == 0; // digitalRead (A6);
  x = (PINA & _BV (7)) == 0; // digitalRead (A7);

Thanks Nick.

So to make bit D0 toggle really fast a few times, I would just do this?

DDRB |= _BV (0); // pinMode (0, OUTPUT);
for (x=0; x<10; x=x+1){
PORTB |= _BV (0); // digitalWrite (0, HIGH);
PORTB &= ~_BV (0); // digitalWrite (0, LOW);
}

do I need the DDRB anyplace except in void setup() if the pin will always be an output?

Nice post, Nick. How about the Mega 2560 ?

Gotta do your part j514 - post the ports/pin assignments like I did for the 1284.
Take that one and build up for all the 2560 pins.

CrossRoads:
Thanks Nick.

So to make bit D0 toggle really fast a few times, I would just do this?

DDRB |= _BV (0); // pinMode (0, OUTPUT);

for (x=0; x<10; x=x+1){
PORTB |= _BV (0); // digitalWrite (0, HIGH);
PORTB &= ~_BV (0); // digitalWrite (0, LOW);
}

That looks good.

CrossRoads:
do I need the DDRB anyplace except in void setup() if the pin will always be an output?

No. It's like pinMode. Only need to do it once.

Gotta do your part j514

Fair enough. Just to confirm before I get going at it, using http://arduino.cc/en/Hacking/PinMapping2560 as the source, does

5	PE3 ( OC3A/AIN1 )

convert to:

{ name = "5", port = "E", bit = 3 },

or am I mistaken?

I believe you are correct.
Note that not all ports/pins are brought out on the Arduino Mega, so there will be some holes in the listing.

Okay here we go.

{ name = "1", port = "G", bit = 5 },
{ name = "2", port = "E", bit = 0 },
{ name = "3", port = "E", bit = 1 },
{ name = "4", port = "E", bit = 2 },
{ name = "5", port = "E", bit = 3 },
{ name = "6", port = "E", bit = 4 },
{ name = "7", port = "E", bit = 5 },
{ name = "8", port = "E", bit = 6 },
{ name = "9", port = "E", bit = 7 },
{ name = "12", port = "H", bit = 0 },
{ name = "13", port = "H", bit = 1 },
{ name = "14", port = "H", bit = 2 },
{ name = "15", port = "H", bit = 3 },
{ name = "16", port = "H", bit = 4 },
{ name = "17", port = "H", bit = 5 },
{ name = "18", port = "H", bit = 6 },
{ name = "19", port = "B", bit = 0 },
{ name = "20", port = "B", bit = 1 },
{ name = "21", port = "B", bit = 2 },
{ name = "22", port = "B", bit = 3 },
{ name = "23", port = "B", bit = 4 },
{ name = "24", port = "B", bit = 5 },
{ name = "25", port = "B", bit = 6 },
{ name = "26", port = "B", bit = 7 },
{ name = "27", port = "H", bit = 7 },
{ name = "28", port = "G", bit = 3 },
{ name = "29", port = "G", bit = 4 },
{ name = "35", port = "L", bit = 0 },
{ name = "36", port = "L", bit = 1 },
{ name = "37", port = "L", bit = 2 },
{ name = "38", port = "L", bit = 3 },
{ name = "39", port = "L", bit = 4 },
{ name = "40", port = "L", bit = 5 },
{ name = "41", port = "L", bit = 6 },
{ name = "42", port = "L", bit = 7 },
{ name = "43", port = "D", bit = 0 },
{ name = "44", port = "D", bit = 1 },
{ name = "45", port = "D", bit = 2 },
{ name = "46", port = "D", bit = 3 },
{ name = "47", port = "D", bit = 4 },
{ name = "48", port = "D", bit = 5 },
{ name = "49", port = "D", bit = 6 },
{ name = "50", port = "D", bit = 7 },
{ name = "51", port = "G", bit = 0 },
{ name = "52", port = "G", bit = 1 },
{ name = "53", port = "C", bit = 0 },
{ name = "54", port = "C", bit = 1 },
{ name = "55", port = "C", bit = 2 },
{ name = "56", port = "C", bit = 3 },
{ name = "57", port = "C", bit = 4 },
{ name = "58", port = "C", bit = 5 },
{ name = "59", port = "C", bit = 6 },
{ name = "60", port = "C", bit = 7 },
{ name = "63", port = "J", bit = 0 },
{ name = "64", port = "J", bit = 1 },
{ name = "65", port = "J", bit = 2 },
{ name = "66", port = "J", bit = 3 },
{ name = "67", port = "J", bit = 4 },
{ name = "68", port = "J", bit = 5 },
{ name = "69", port = "J", bit = 6 },
{ name = "70", port = "G", bit = 2 },
{ name = "71", port = "A", bit = 7 },
{ name = "72", port = "A", bit = 6 },
{ name = "73", port = "A", bit = 5 },
{ name = "74", port = "A", bit = 4 },
{ name = "75", port = "A", bit = 3 },
{ name = "76", port = "A", bit = 2 },
{ name = "77", port = "A", bit = 1 },
{ name = "78", port = "A", bit = 0 },
{ name = "79", port = "J", bit = 7 },
{ name = "82", port = "K", bit = 7 },
{ name = "83", port = "K", bit = 6 },
{ name = "84", port = "K", bit = 5 },
{ name = "85", port = "K", bit = 4 },
{ name = "86", port = "K", bit = 3 },
{ name = "87", port = "K", bit = 2 },
{ name = "88", port = "K", bit = 1 },
{ name = "89", port = "K", bit = 0 },
{ name = "90", port = "F", bit = 7 },
{ name = "91", port = "F", bit = 6 },
{ name = "92", port = "F", bit = 5 },
{ name = "93", port = "F", bit = 4 },
{ name = "94", port = "F", bit = 3 },
{ name = "95", port = "F", bit = 2 },
{ name = "96", port = "F", bit = 1 },
{ name = "97", port = "F", bit = 0 },

On the Mega aren't the last 16:

{ name = "A15", port = "K", bit = 7 },
{ name = "A14", port = "K", bit = 6 },
{ name = "A13", port = "K", bit = 5 },
{ name = "A12", port = "K", bit = 4 },
{ name = "A11", port = "K", bit = 3 },
{ name = "A10", port = "K", bit = 2 },
{ name = "A9", port = "K", bit = 1 },
{ name = "A8", port = "K", bit = 0 },
{ name = "A7", port = "F", bit = 7 },
{ name = "A6", port = "F", bit = 6 },
{ name = "A5", port = "F", bit = 5 },
{ name = "A4", port = "F", bit = 4 },
{ name = "A3", port = "F", bit = 3 },
{ name = "A2", port = "F", bit = 2 },
{ name = "A1", port = "F", bit = 1 },
{ name = "A0", port = "F", bit = 0 },

Also I don't seem to have D54 to D79 on my Mega. Are they brought out anywhere?

On the Mega aren't the last 16:

I'm not sure. What I did was copy + paste the pin layout from http://arduino.cc/en/Hacking/PinMapping2560 and ran it through a formatting routine. I then trimmed the Vcc and Gnd pins from the output.

If you want to try this at home, Lua isn't hard to install.

Download source from:

http://www.lua.org/download.html

Pre-built binaries:

http://lua-users.org/wiki/LuaBinaries

Put the Lua source (above) into a file (eg. pinmappings.lua) and apply Lua to it:

lua pinmappings.lua

Or you could rework the code into C++. I was just feeling lazy when I did the Lua version.

j514:

On the Mega aren't the last 16:

I'm not sure. What I did was copy + paste the pin layout from http://arduino.cc/en/Hacking/PinMapping2560 and ran it through a formatting routine. I then trimmed the Vcc and Gnd pins from the output.

Ah I see the confusion. From that page:

1	PG5 ( OC0B )	Digital pin 4 (PWM)

What we really want is:

{ name = "4", port = "G", bit = 5 },

Because that refers to "board" pin 4, not processor pin 4.

Now I have the reference, I'll run a regexp on it.

I think this is it:

Atmega2560

// --- PIN MODE: OUTPUT  --- 

  DDRE |= _BV (0); // pinMode (0, OUTPUT);
  DDRE |= _BV (1); // pinMode (1, OUTPUT);
  DDRE |= _BV (4); // pinMode (2, OUTPUT);
  DDRE |= _BV (5); // pinMode (3, OUTPUT);
  DDRG |= _BV (5); // pinMode (4, OUTPUT);
  DDRE |= _BV (3); // pinMode (5, OUTPUT);
  DDRH |= _BV (3); // pinMode (6, OUTPUT);
  DDRH |= _BV (4); // pinMode (7, OUTPUT);
  DDRH |= _BV (5); // pinMode (8, OUTPUT);
  DDRH |= _BV (6); // pinMode (9, OUTPUT);
  DDRB |= _BV (4); // pinMode (10, OUTPUT);
  DDRB |= _BV (5); // pinMode (11, OUTPUT);
  DDRB |= _BV (6); // pinMode (12, OUTPUT);
  DDRB |= _BV (7); // pinMode (13, OUTPUT);
  DDRJ |= _BV (1); // pinMode (14, OUTPUT);
  DDRJ |= _BV (0); // pinMode (15, OUTPUT);
  DDRH |= _BV (1); // pinMode (16, OUTPUT);
  DDRH |= _BV (0); // pinMode (17, OUTPUT);
  DDRD |= _BV (3); // pinMode (18, OUTPUT);
  DDRD |= _BV (2); // pinMode (19, OUTPUT);
  DDRD |= _BV (1); // pinMode (20, OUTPUT);
  DDRD |= _BV (0); // pinMode (21, OUTPUT);
  DDRA |= _BV (0); // pinMode (22, OUTPUT);
  DDRA |= _BV (1); // pinMode (23, OUTPUT);
  DDRA |= _BV (2); // pinMode (24, OUTPUT);
  DDRA |= _BV (3); // pinMode (25, OUTPUT);
  DDRA |= _BV (4); // pinMode (26, OUTPUT);
  DDRA |= _BV (5); // pinMode (27, OUTPUT);
  DDRA |= _BV (6); // pinMode (28, OUTPUT);
  DDRA |= _BV (7); // pinMode (29, OUTPUT);
  DDRC |= _BV (7); // pinMode (30, OUTPUT);
  DDRC |= _BV (6); // pinMode (31, OUTPUT);
  DDRC |= _BV (5); // pinMode (32, OUTPUT);
  DDRC |= _BV (4); // pinMode (33, OUTPUT);
  DDRC |= _BV (3); // pinMode (34, OUTPUT);
  DDRC |= _BV (2); // pinMode (35, OUTPUT);
  DDRC |= _BV (1); // pinMode (36, OUTPUT);
  DDRC |= _BV (0); // pinMode (37, OUTPUT);
  DDRD |= _BV (7); // pinMode (38, OUTPUT);
  DDRG |= _BV (2); // pinMode (39, OUTPUT);
  DDRG |= _BV (1); // pinMode (40, OUTPUT);
  DDRG |= _BV (0); // pinMode (41, OUTPUT);
  DDRL |= _BV (7); // pinMode (42, OUTPUT);
  DDRL |= _BV (6); // pinMode (43, OUTPUT);
  DDRL |= _BV (5); // pinMode (44, OUTPUT);
  DDRL |= _BV (4); // pinMode (45, OUTPUT);
  DDRL |= _BV (3); // pinMode (46, OUTPUT);
  DDRL |= _BV (2); // pinMode (47, OUTPUT);
  DDRL |= _BV (1); // pinMode (48, OUTPUT);
  DDRL |= _BV (0); // pinMode (49, OUTPUT);
  DDRB |= _BV (3); // pinMode (50, OUTPUT);
  DDRB |= _BV (2); // pinMode (51, OUTPUT);
  DDRB |= _BV (1); // pinMode (52, OUTPUT);
  DDRB |= _BV (0); // pinMode (53, OUTPUT);
  DDRF |= _BV (0); // pinMode (A0, OUTPUT);
  DDRF |= _BV (1); // pinMode (A1, OUTPUT);
  DDRF |= _BV (2); // pinMode (A2, OUTPUT);
  DDRF |= _BV (3); // pinMode (A3, OUTPUT);
  DDRF |= _BV (4); // pinMode (A4, OUTPUT);
  DDRF |= _BV (5); // pinMode (A5, OUTPUT);
  DDRF |= _BV (6); // pinMode (A6, OUTPUT);
  DDRF |= _BV (7); // pinMode (A7, OUTPUT);
  DDRK |= _BV (0); // pinMode (A8, OUTPUT);
  DDRK |= _BV (1); // pinMode (A9, OUTPUT);
  DDRK |= _BV (2); // pinMode (A10, OUTPUT);
  DDRK |= _BV (3); // pinMode (A11, OUTPUT);
  DDRK |= _BV (4); // pinMode (A12, OUTPUT);
  DDRK |= _BV (5); // pinMode (A13, OUTPUT);
  DDRK |= _BV (6); // pinMode (A14, OUTPUT);
  DDRK |= _BV (7); // pinMode (A15, OUTPUT);

// --- PIN MODE: INPUT  --- 

  DDRE &= ~_BV (0); // pinMode (0, INPUT);
  DDRE &= ~_BV (1); // pinMode (1, INPUT);
  DDRE &= ~_BV (4); // pinMode (2, INPUT);
  DDRE &= ~_BV (5); // pinMode (3, INPUT);
  DDRG &= ~_BV (5); // pinMode (4, INPUT);
  DDRE &= ~_BV (3); // pinMode (5, INPUT);
  DDRH &= ~_BV (3); // pinMode (6, INPUT);
  DDRH &= ~_BV (4); // pinMode (7, INPUT);
  DDRH &= ~_BV (5); // pinMode (8, INPUT);
  DDRH &= ~_BV (6); // pinMode (9, INPUT);
  DDRB &= ~_BV (4); // pinMode (10, INPUT);
  DDRB &= ~_BV (5); // pinMode (11, INPUT);
  DDRB &= ~_BV (6); // pinMode (12, INPUT);
  DDRB &= ~_BV (7); // pinMode (13, INPUT);
  DDRJ &= ~_BV (1); // pinMode (14, INPUT);
  DDRJ &= ~_BV (0); // pinMode (15, INPUT);
  DDRH &= ~_BV (1); // pinMode (16, INPUT);
  DDRH &= ~_BV (0); // pinMode (17, INPUT);
  DDRD &= ~_BV (3); // pinMode (18, INPUT);
  DDRD &= ~_BV (2); // pinMode (19, INPUT);
  DDRD &= ~_BV (1); // pinMode (20, INPUT);
  DDRD &= ~_BV (0); // pinMode (21, INPUT);
  DDRA &= ~_BV (0); // pinMode (22, INPUT);
  DDRA &= ~_BV (1); // pinMode (23, INPUT);
  DDRA &= ~_BV (2); // pinMode (24, INPUT);
  DDRA &= ~_BV (3); // pinMode (25, INPUT);
  DDRA &= ~_BV (4); // pinMode (26, INPUT);
  DDRA &= ~_BV (5); // pinMode (27, INPUT);
  DDRA &= ~_BV (6); // pinMode (28, INPUT);
  DDRA &= ~_BV (7); // pinMode (29, INPUT);
  DDRC &= ~_BV (7); // pinMode (30, INPUT);
  DDRC &= ~_BV (6); // pinMode (31, INPUT);
  DDRC &= ~_BV (5); // pinMode (32, INPUT);
  DDRC &= ~_BV (4); // pinMode (33, INPUT);
  DDRC &= ~_BV (3); // pinMode (34, INPUT);
  DDRC &= ~_BV (2); // pinMode (35, INPUT);
  DDRC &= ~_BV (1); // pinMode (36, INPUT);
  DDRC &= ~_BV (0); // pinMode (37, INPUT);
  DDRD &= ~_BV (7); // pinMode (38, INPUT);
  DDRG &= ~_BV (2); // pinMode (39, INPUT);
  DDRG &= ~_BV (1); // pinMode (40, INPUT);
  DDRG &= ~_BV (0); // pinMode (41, INPUT);
  DDRL &= ~_BV (7); // pinMode (42, INPUT);
  DDRL &= ~_BV (6); // pinMode (43, INPUT);
  DDRL &= ~_BV (5); // pinMode (44, INPUT);
  DDRL &= ~_BV (4); // pinMode (45, INPUT);
  DDRL &= ~_BV (3); // pinMode (46, INPUT);
  DDRL &= ~_BV (2); // pinMode (47, INPUT);
  DDRL &= ~_BV (1); // pinMode (48, INPUT);
  DDRL &= ~_BV (0); // pinMode (49, INPUT);
  DDRB &= ~_BV (3); // pinMode (50, INPUT);
  DDRB &= ~_BV (2); // pinMode (51, INPUT);
  DDRB &= ~_BV (1); // pinMode (52, INPUT);
  DDRB &= ~_BV (0); // pinMode (53, INPUT);
  DDRF &= ~_BV (0); // pinMode (A0, INPUT);
  DDRF &= ~_BV (1); // pinMode (A1, INPUT);
  DDRF &= ~_BV (2); // pinMode (A2, INPUT);
  DDRF &= ~_BV (3); // pinMode (A3, INPUT);
  DDRF &= ~_BV (4); // pinMode (A4, INPUT);
  DDRF &= ~_BV (5); // pinMode (A5, INPUT);
  DDRF &= ~_BV (6); // pinMode (A6, INPUT);
  DDRF &= ~_BV (7); // pinMode (A7, INPUT);
  DDRK &= ~_BV (0); // pinMode (A8, INPUT);
  DDRK &= ~_BV (1); // pinMode (A9, INPUT);
  DDRK &= ~_BV (2); // pinMode (A10, INPUT);
  DDRK &= ~_BV (3); // pinMode (A11, INPUT);
  DDRK &= ~_BV (4); // pinMode (A12, INPUT);
  DDRK &= ~_BV (5); // pinMode (A13, INPUT);
  DDRK &= ~_BV (6); // pinMode (A14, INPUT);
  DDRK &= ~_BV (7); // pinMode (A15, INPUT);

(continued)

Atmega2560:

// --- DIGITAL WRITE: HIGH  --- 

  PORTE |= _BV (0); // digitalWrite (0, HIGH);
  PORTE |= _BV (1); // digitalWrite (1, HIGH);
  PORTE |= _BV (4); // digitalWrite (2, HIGH);
  PORTE |= _BV (5); // digitalWrite (3, HIGH);
  PORTG |= _BV (5); // digitalWrite (4, HIGH);
  PORTE |= _BV (3); // digitalWrite (5, HIGH);
  PORTH |= _BV (3); // digitalWrite (6, HIGH);
  PORTH |= _BV (4); // digitalWrite (7, HIGH);
  PORTH |= _BV (5); // digitalWrite (8, HIGH);
  PORTH |= _BV (6); // digitalWrite (9, HIGH);
  PORTB |= _BV (4); // digitalWrite (10, HIGH);
  PORTB |= _BV (5); // digitalWrite (11, HIGH);
  PORTB |= _BV (6); // digitalWrite (12, HIGH);
  PORTB |= _BV (7); // digitalWrite (13, HIGH);
  PORTJ |= _BV (1); // digitalWrite (14, HIGH);
  PORTJ |= _BV (0); // digitalWrite (15, HIGH);
  PORTH |= _BV (1); // digitalWrite (16, HIGH);
  PORTH |= _BV (0); // digitalWrite (17, HIGH);
  PORTD |= _BV (3); // digitalWrite (18, HIGH);
  PORTD |= _BV (2); // digitalWrite (19, HIGH);
  PORTD |= _BV (1); // digitalWrite (20, HIGH);
  PORTD |= _BV (0); // digitalWrite (21, HIGH);
  PORTA |= _BV (0); // digitalWrite (22, HIGH);
  PORTA |= _BV (1); // digitalWrite (23, HIGH);
  PORTA |= _BV (2); // digitalWrite (24, HIGH);
  PORTA |= _BV (3); // digitalWrite (25, HIGH);
  PORTA |= _BV (4); // digitalWrite (26, HIGH);
  PORTA |= _BV (5); // digitalWrite (27, HIGH);
  PORTA |= _BV (6); // digitalWrite (28, HIGH);
  PORTA |= _BV (7); // digitalWrite (29, HIGH);
  PORTC |= _BV (7); // digitalWrite (30, HIGH);
  PORTC |= _BV (6); // digitalWrite (31, HIGH);
  PORTC |= _BV (5); // digitalWrite (32, HIGH);
  PORTC |= _BV (4); // digitalWrite (33, HIGH);
  PORTC |= _BV (3); // digitalWrite (34, HIGH);
  PORTC |= _BV (2); // digitalWrite (35, HIGH);
  PORTC |= _BV (1); // digitalWrite (36, HIGH);
  PORTC |= _BV (0); // digitalWrite (37, HIGH);
  PORTD |= _BV (7); // digitalWrite (38, HIGH);
  PORTG |= _BV (2); // digitalWrite (39, HIGH);
  PORTG |= _BV (1); // digitalWrite (40, HIGH);
  PORTG |= _BV (0); // digitalWrite (41, HIGH);
  PORTL |= _BV (7); // digitalWrite (42, HIGH);
  PORTL |= _BV (6); // digitalWrite (43, HIGH);
  PORTL |= _BV (5); // digitalWrite (44, HIGH);
  PORTL |= _BV (4); // digitalWrite (45, HIGH);
  PORTL |= _BV (3); // digitalWrite (46, HIGH);
  PORTL |= _BV (2); // digitalWrite (47, HIGH);
  PORTL |= _BV (1); // digitalWrite (48, HIGH);
  PORTL |= _BV (0); // digitalWrite (49, HIGH);
  PORTB |= _BV (3); // digitalWrite (50, HIGH);
  PORTB |= _BV (2); // digitalWrite (51, HIGH);
  PORTB |= _BV (1); // digitalWrite (52, HIGH);
  PORTB |= _BV (0); // digitalWrite (53, HIGH);
  PORTF |= _BV (0); // digitalWrite (A0, HIGH);
  PORTF |= _BV (1); // digitalWrite (A1, HIGH);
  PORTF |= _BV (2); // digitalWrite (A2, HIGH);
  PORTF |= _BV (3); // digitalWrite (A3, HIGH);
  PORTF |= _BV (4); // digitalWrite (A4, HIGH);
  PORTF |= _BV (5); // digitalWrite (A5, HIGH);
  PORTF |= _BV (6); // digitalWrite (A6, HIGH);
  PORTF |= _BV (7); // digitalWrite (A7, HIGH);
  PORTK |= _BV (0); // digitalWrite (A8, HIGH);
  PORTK |= _BV (1); // digitalWrite (A9, HIGH);
  PORTK |= _BV (2); // digitalWrite (A10, HIGH);
  PORTK |= _BV (3); // digitalWrite (A11, HIGH);
  PORTK |= _BV (4); // digitalWrite (A12, HIGH);
  PORTK |= _BV (5); // digitalWrite (A13, HIGH);
  PORTK |= _BV (6); // digitalWrite (A14, HIGH);
  PORTK |= _BV (7); // digitalWrite (A15, HIGH);

// --- DIGITAL WRITE: LOW  --- 

  PORTE &= ~_BV (0); // digitalWrite (0, LOW);
  PORTE &= ~_BV (1); // digitalWrite (1, LOW);
  PORTE &= ~_BV (4); // digitalWrite (2, LOW);
  PORTE &= ~_BV (5); // digitalWrite (3, LOW);
  PORTG &= ~_BV (5); // digitalWrite (4, LOW);
  PORTE &= ~_BV (3); // digitalWrite (5, LOW);
  PORTH &= ~_BV (3); // digitalWrite (6, LOW);
  PORTH &= ~_BV (4); // digitalWrite (7, LOW);
  PORTH &= ~_BV (5); // digitalWrite (8, LOW);
  PORTH &= ~_BV (6); // digitalWrite (9, LOW);
  PORTB &= ~_BV (4); // digitalWrite (10, LOW);
  PORTB &= ~_BV (5); // digitalWrite (11, LOW);
  PORTB &= ~_BV (6); // digitalWrite (12, LOW);
  PORTB &= ~_BV (7); // digitalWrite (13, LOW);
  PORTJ &= ~_BV (1); // digitalWrite (14, LOW);
  PORTJ &= ~_BV (0); // digitalWrite (15, LOW);
  PORTH &= ~_BV (1); // digitalWrite (16, LOW);
  PORTH &= ~_BV (0); // digitalWrite (17, LOW);
  PORTD &= ~_BV (3); // digitalWrite (18, LOW);
  PORTD &= ~_BV (2); // digitalWrite (19, LOW);
  PORTD &= ~_BV (1); // digitalWrite (20, LOW);
  PORTD &= ~_BV (0); // digitalWrite (21, LOW);
  PORTA &= ~_BV (0); // digitalWrite (22, LOW);
  PORTA &= ~_BV (1); // digitalWrite (23, LOW);
  PORTA &= ~_BV (2); // digitalWrite (24, LOW);
  PORTA &= ~_BV (3); // digitalWrite (25, LOW);
  PORTA &= ~_BV (4); // digitalWrite (26, LOW);
  PORTA &= ~_BV (5); // digitalWrite (27, LOW);
  PORTA &= ~_BV (6); // digitalWrite (28, LOW);
  PORTA &= ~_BV (7); // digitalWrite (29, LOW);
  PORTC &= ~_BV (7); // digitalWrite (30, LOW);
  PORTC &= ~_BV (6); // digitalWrite (31, LOW);
  PORTC &= ~_BV (5); // digitalWrite (32, LOW);
  PORTC &= ~_BV (4); // digitalWrite (33, LOW);
  PORTC &= ~_BV (3); // digitalWrite (34, LOW);
  PORTC &= ~_BV (2); // digitalWrite (35, LOW);
  PORTC &= ~_BV (1); // digitalWrite (36, LOW);
  PORTC &= ~_BV (0); // digitalWrite (37, LOW);
  PORTD &= ~_BV (7); // digitalWrite (38, LOW);
  PORTG &= ~_BV (2); // digitalWrite (39, LOW);
  PORTG &= ~_BV (1); // digitalWrite (40, LOW);
  PORTG &= ~_BV (0); // digitalWrite (41, LOW);
  PORTL &= ~_BV (7); // digitalWrite (42, LOW);
  PORTL &= ~_BV (6); // digitalWrite (43, LOW);
  PORTL &= ~_BV (5); // digitalWrite (44, LOW);
  PORTL &= ~_BV (4); // digitalWrite (45, LOW);
  PORTL &= ~_BV (3); // digitalWrite (46, LOW);
  PORTL &= ~_BV (2); // digitalWrite (47, LOW);
  PORTL &= ~_BV (1); // digitalWrite (48, LOW);
  PORTL &= ~_BV (0); // digitalWrite (49, LOW);
  PORTB &= ~_BV (3); // digitalWrite (50, LOW);
  PORTB &= ~_BV (2); // digitalWrite (51, LOW);
  PORTB &= ~_BV (1); // digitalWrite (52, LOW);
  PORTB &= ~_BV (0); // digitalWrite (53, LOW);
  PORTF &= ~_BV (0); // digitalWrite (A0, LOW);
  PORTF &= ~_BV (1); // digitalWrite (A1, LOW);
  PORTF &= ~_BV (2); // digitalWrite (A2, LOW);
  PORTF &= ~_BV (3); // digitalWrite (A3, LOW);
  PORTF &= ~_BV (4); // digitalWrite (A4, LOW);
  PORTF &= ~_BV (5); // digitalWrite (A5, LOW);
  PORTF &= ~_BV (6); // digitalWrite (A6, LOW);
  PORTF &= ~_BV (7); // digitalWrite (A7, LOW);
  PORTK &= ~_BV (0); // digitalWrite (A8, LOW);
  PORTK &= ~_BV (1); // digitalWrite (A9, LOW);
  PORTK &= ~_BV (2); // digitalWrite (A10, LOW);
  PORTK &= ~_BV (3); // digitalWrite (A11, LOW);
  PORTK &= ~_BV (4); // digitalWrite (A12, LOW);
  PORTK &= ~_BV (5); // digitalWrite (A13, LOW);
  PORTK &= ~_BV (6); // digitalWrite (A14, LOW);
  PORTK &= ~_BV (7); // digitalWrite (A15, LOW);

(continued)

Atmega2560:

// --- DIGITAL READ  --- 

  x = (PINE & _BV (0)) == 0; // digitalRead (0);
  x = (PINE & _BV (1)) == 0; // digitalRead (1);
  x = (PINE & _BV (4)) == 0; // digitalRead (2);
  x = (PINE & _BV (5)) == 0; // digitalRead (3);
  x = (PING & _BV (5)) == 0; // digitalRead (4);
  x = (PINE & _BV (3)) == 0; // digitalRead (5);
  x = (PINH & _BV (3)) == 0; // digitalRead (6);
  x = (PINH & _BV (4)) == 0; // digitalRead (7);
  x = (PINH & _BV (5)) == 0; // digitalRead (8);
  x = (PINH & _BV (6)) == 0; // digitalRead (9);
  x = (PINB & _BV (4)) == 0; // digitalRead (10);
  x = (PINB & _BV (5)) == 0; // digitalRead (11);
  x = (PINB & _BV (6)) == 0; // digitalRead (12);
  x = (PINB & _BV (7)) == 0; // digitalRead (13);
  x = (PINJ & _BV (1)) == 0; // digitalRead (14);
  x = (PINJ & _BV (0)) == 0; // digitalRead (15);
  x = (PINH & _BV (1)) == 0; // digitalRead (16);
  x = (PINH & _BV (0)) == 0; // digitalRead (17);
  x = (PIND & _BV (3)) == 0; // digitalRead (18);
  x = (PIND & _BV (2)) == 0; // digitalRead (19);
  x = (PIND & _BV (1)) == 0; // digitalRead (20);
  x = (PIND & _BV (0)) == 0; // digitalRead (21);
  x = (PINA & _BV (0)) == 0; // digitalRead (22);
  x = (PINA & _BV (1)) == 0; // digitalRead (23);
  x = (PINA & _BV (2)) == 0; // digitalRead (24);
  x = (PINA & _BV (3)) == 0; // digitalRead (25);
  x = (PINA & _BV (4)) == 0; // digitalRead (26);
  x = (PINA & _BV (5)) == 0; // digitalRead (27);
  x = (PINA & _BV (6)) == 0; // digitalRead (28);
  x = (PINA & _BV (7)) == 0; // digitalRead (29);
  x = (PINC & _BV (7)) == 0; // digitalRead (30);
  x = (PINC & _BV (6)) == 0; // digitalRead (31);
  x = (PINC & _BV (5)) == 0; // digitalRead (32);
  x = (PINC & _BV (4)) == 0; // digitalRead (33);
  x = (PINC & _BV (3)) == 0; // digitalRead (34);
  x = (PINC & _BV (2)) == 0; // digitalRead (35);
  x = (PINC & _BV (1)) == 0; // digitalRead (36);
  x = (PINC & _BV (0)) == 0; // digitalRead (37);
  x = (PIND & _BV (7)) == 0; // digitalRead (38);
  x = (PING & _BV (2)) == 0; // digitalRead (39);
  x = (PING & _BV (1)) == 0; // digitalRead (40);
  x = (PING & _BV (0)) == 0; // digitalRead (41);
  x = (PINL & _BV (7)) == 0; // digitalRead (42);
  x = (PINL & _BV (6)) == 0; // digitalRead (43);
  x = (PINL & _BV (5)) == 0; // digitalRead (44);
  x = (PINL & _BV (4)) == 0; // digitalRead (45);
  x = (PINL & _BV (3)) == 0; // digitalRead (46);
  x = (PINL & _BV (2)) == 0; // digitalRead (47);
  x = (PINL & _BV (1)) == 0; // digitalRead (48);
  x = (PINL & _BV (0)) == 0; // digitalRead (49);
  x = (PINB & _BV (3)) == 0; // digitalRead (50);
  x = (PINB & _BV (2)) == 0; // digitalRead (51);
  x = (PINB & _BV (1)) == 0; // digitalRead (52);
  x = (PINB & _BV (0)) == 0; // digitalRead (53);
  x = (PINF & _BV (0)) == 0; // digitalRead (A0);
  x = (PINF & _BV (1)) == 0; // digitalRead (A1);
  x = (PINF & _BV (2)) == 0; // digitalRead (A2);
  x = (PINF & _BV (3)) == 0; // digitalRead (A3);
  x = (PINF & _BV (4)) == 0; // digitalRead (A4);
  x = (PINF & _BV (5)) == 0; // digitalRead (A5);
  x = (PINF & _BV (6)) == 0; // digitalRead (A6);
  x = (PINF & _BV (7)) == 0; // digitalRead (A7);
  x = (PINK & _BV (0)) == 0; // digitalRead (A8);
  x = (PINK & _BV (1)) == 0; // digitalRead (A9);
  x = (PINK & _BV (2)) == 0; // digitalRead (A10);
  x = (PINK & _BV (3)) == 0; // digitalRead (A11);
  x = (PINK & _BV (4)) == 0; // digitalRead (A12);
  x = (PINK & _BV (5)) == 0; // digitalRead (A13);
  x = (PINK & _BV (6)) == 0; // digitalRead (A14);
  x = (PINK & _BV (7)) == 0; // digitalRead (A15);

Lua code to generate the above:

-- Atmega2560

pins = {
{ name = "0",  port = "E", bit = 0 },
{ name = "1",  port = "E", bit = 1 },
{ name = "2",  port = "E", bit = 4 },
{ name = "3",  port = "E", bit = 5 },
{ name = "4",  port = "G", bit = 5 },
{ name = "5",  port = "E", bit = 3 },
{ name = "6",  port = "H", bit = 3 },
{ name = "7",  port = "H", bit = 4 },
{ name = "8",  port = "H", bit = 5 },
{ name = "9",  port = "H", bit = 6 },

{ name = "10",  port = "B", bit = 4 },
{ name = "11",  port = "B", bit = 5 },
{ name = "12",  port = "B", bit = 6 },
{ name = "13",  port = "B", bit = 7 },
{ name = "14",  port = "J", bit = 1 },
{ name = "15",  port = "J", bit = 0 },
{ name = "16",  port = "H", bit = 1 },
{ name = "17",  port = "H", bit = 0 },
{ name = "18",  port = "D", bit = 3 },
{ name = "19",  port = "D", bit = 2 },

{ name = "20",  port = "D", bit = 1 },
{ name = "21",  port = "D", bit = 0 },
{ name = "22",  port = "A", bit = 0 },
{ name = "23",  port = "A", bit = 1 },
{ name = "24",  port = "A", bit = 2 },
{ name = "25",  port = "A", bit = 3 },
{ name = "26",  port = "A", bit = 4 },
{ name = "27",  port = "A", bit = 5 },
{ name = "28",  port = "A", bit = 6 },
{ name = "29",  port = "A", bit = 7 },

{ name = "30",  port = "C", bit = 7 },
{ name = "31",  port = "C", bit = 6 },
{ name = "32",  port = "C", bit = 5 },
{ name = "33",  port = "C", bit = 4 },
{ name = "34",  port = "C", bit = 3 },
{ name = "35",  port = "C", bit = 2 },
{ name = "36",  port = "C", bit = 1 },
{ name = "37",  port = "C", bit = 0 },
{ name = "38",  port = "D", bit = 7 },
{ name = "39",  port = "G", bit = 2 },

{ name = "40",  port = "G", bit = 1 },
{ name = "41",  port = "G", bit = 0 },
{ name = "42",  port = "L", bit = 7 },
{ name = "43",  port = "L", bit = 6 },
{ name = "44",  port = "L", bit = 5 },
{ name = "45",  port = "L", bit = 4 },
{ name = "46",  port = "L", bit = 3 },
{ name = "47",  port = "L", bit = 2 },
{ name = "48",  port = "L", bit = 1 },
{ name = "49",  port = "L", bit = 0 },

{ name = "50",  port = "B", bit = 3 },
{ name = "51",  port = "B", bit = 2 },
{ name = "52",  port = "B", bit = 1 },
{ name = "53",  port = "B", bit = 0 },

{ name = "A0",  port = "F", bit = 0 },
{ name = "A1",  port = "F", bit = 1 },
{ name = "A2",  port = "F", bit = 2 },
{ name = "A3",  port = "F", bit = 3 },
{ name = "A4",  port = "F", bit = 4 },
{ name = "A5",  port = "F", bit = 5 },
{ name = "A6",  port = "F", bit = 6 },
{ name = "A7",  port = "F", bit = 7 },
{ name = "A8",  port = "K", bit = 0 },
{ name = "A9",  port = "K", bit = 1 },

{ name = "A10",  port = "K", bit = 2 },
{ name = "A11",  port = "K", bit = 3 },
{ name = "A12",  port = "K", bit = 4 },
{ name = "A13",  port = "K", bit = 5 },
{ name = "A14",  port = "K", bit = 6 },
{ name = "A15",  port = "K", bit = 7 },


}

print ""
print ("// --- PIN MODE: OUTPUT  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  DDR" .. pin.port .. " |= _BV (" .. pin.bit .. "); // pinMode (" .. pin.name .. ", OUTPUT);")
end -- for

print ""
print ("// --- PIN MODE: INPUT  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  DDR" .. pin.port .. " &= ~_BV (" .. pin.bit .. "); // pinMode (" .. pin.name .. ", INPUT);")
end -- for


print ""
print ("// --- DIGITAL WRITE: HIGH  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  PORT" .. pin.port .. " |= _BV (" .. pin.bit .. "); // digitalWrite (" .. pin.name .. ", HIGH);")
end -- for


print ""
print ("// --- DIGITAL WRITE: LOW  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  PORT" .. pin.port .. " &= ~_BV (" .. pin.bit .. "); // digitalWrite (" .. pin.name .. ", LOW);")
end -- for

print ""
print ("// --- DIGITAL READ  --- ")
print ""

for k, pin in ipairs (pins) do
  print ("  x = (PIN" .. pin.port .. " & _BV (" .. pin.bit .. ")) == 0; // digitalRead (" .. pin.name .. ");")
end -- for