Go Down

Topic: Digital read/write pinMode : fast execution (Read 16835 times) previous topic - next topic

nickgammon

Feb 09, 2012, 06:46 am Last Edit: Feb 09, 2012, 09:09 pm by Nick Gammon Reason: 1
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:

Code: [Select]

// --- 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:

Code: [Select]

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
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

CrossRoads

Cool. Can you create same for a 1284 please?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nickgammon

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.

Code: [Select]
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
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

CrossRoads

Pins as defined by maniacbug, following Sanguino lead I think.
Code: [Select]

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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nickgammon

#4
Feb 09, 2012, 07:42 am Last Edit: Feb 09, 2012, 09:10 pm by Nick Gammon Reason: 1
Given the above data:

For the Atmega1284:

Code: [Select]

// --- 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);
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

CrossRoads

Thanks Nick.

So to make bit D0 toggle really fast a few times, I would just do this?
Code: [Select]

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?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

j514

Nice post, Nick.  How about the Mega 2560 ?
a][+ ascii express, 110/300 novation cat, xmodem

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nickgammon


Thanks Nick.

So to make bit D0 toggle really fast a few times, I would just do this?
Code: [Select]

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.


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

j514

Quote
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

Code: [Select]
5 PE3 ( OC3A/AIN1 )

convert to:

Code: [Select]
{ name = "5", port = "E", bit = 3 },

or am I mistaken?
a][+ ascii express, 110/300 novation cat, xmodem

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

j514

Okay here we go.

Code: [Select]
{ 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 },
a][+ ascii express, 110/300 novation cat, xmodem

nickgammon

On the Mega aren't the last 16:

Code: [Select]
{ 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?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

j514

Quote
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.
a][+ ascii express, 110/300 novation cat, xmodem

nickgammon

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:

Code: [Select]

lua pinmappings.lua


Or you could rework the code into C++. I was just feeling lazy when I did the Lua version.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up