Go Down

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

nickgammon


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.


Ah I see the confusion. From that page:

Code: [Select]
1 PG5 ( OC0B ) Digital pin 4 (PWM)

What we really want is:

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

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

nickgammon

I think this is it:

Atmega2560

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

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

nickgammon

Atmega2560:

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

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

nickgammon

Atmega2560:

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

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

nickgammon

Lua code to generate the above:

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

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

j514

Looks good, thanks for this!  Is there any equivalent speedup for AnalogWrite ?
a][+ ascii express, 110/300 novation cat, xmodem

nickgammon

#21
Feb 09, 2012, 09:19 pm Last Edit: Feb 10, 2012, 06:24 pm by Nick Gammon Reason: 1
Disclaimer

The above code is faster because it does less. The "real" digitalWrite, for example, does more than just turn the pin on.


  • It turns off PWM if necessary

  • It turns interrupts off in case an ISR is also fiddling with the same port



This won't be a problem if you are aware of what you are doing (for example, don't expect an analogWrite to be cancelled).

You can also make things even faster by combining the bits. For example, instead of:

Code: [Select]

  PORTD |= _BV (4); // digitalWrite (4, HIGH);
  PORTD |= _BV (5); // digitalWrite (5, HIGH);
 PORTD |= _BV (6); // digitalWrite (6, HIGH);
 PORTD |= _BV (7); // digitalWrite (7, HIGH);


You could do this:

Code: [Select]
PORTD |= _BV (4) | _BV (5) | _BV (6) | _BV (7); // turn on D4 to D7
Please post technical questions on the forum, not by personal message. Thanks!

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

nickgammon


Looks good, thanks for this!  Is there any equivalent speedup for AnalogWrite ?


Not really, because analogWrite does quite a bit. It fiddles with the internal timers, turning on the correct "timer output" to the correct pin. That is an inherently more complicated operation.

For the digital writes (and reads) the intention of posting this is to help people squeeze a bit of extra speed out of their system, where the friendliness of the digitalRead/digitalWrite isn't required.

Having posted all of the above, I should point out that digitalRead/digitalWrite is more readable, and more portable from one processor to another (eg. porting from the Uno to the Mega).
Please post technical questions on the forum, not by personal message. Thanks!

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

j514

Quote
That is an inherently more complicated operation.

Okay, I understand.  Thanks again, this really helps !
a][+ ascii express, 110/300 novation cat, xmodem

florinc

I found somewhere these equivalent one-liners:

Code: [Select]
//Atmega644/1284 Version of fastWrite - for pins 0-15
#define fastWriteA(_pin_, _state_) ( _pin_ < 8 ? (_state_ ? PORTB |= 1 << _pin_ : \
PORTB &= ~(1 << _pin_ )) : (_state_ ? PORTD |= 1 << (_pin_ -8) : PORTD &= ~(1 << (_pin_ -8) )))

//Atmega644/1284 Version of fastWrite - for pins 16-31 (Note: PORTA mapping reversed from others)
#define fastWriteB(_pin_, _state_) ( _pin_ < 24 ? (_state_ ? PORTC |= 1 << (_pin_ -16) : \
PORTC &= ~(1 << (_pin_ -16))) : (_state_ ? PORTA |= 1 << (31- _pin_) : PORTA &= ~(1 << (31- _pin_) )))


giftlife

hello!
i want to use "DIGITAL READ". but not work
this code.
Quote
int state = 0;
void setup()
{
  DDRD |=_BV (5);
  DDRD |=~_BV (6);

}

   


void loop()
{
  state = (PIND & _BV(6)) == 0;
  if (state == 1)
  {
    PORTD |= _BV (5);
    delay (500); 
    PORTD &= ~_BV (5);
    delay (100);
  }
  else
  {
    PORTD &= ~_BV (5);
  }
}

AWOL

Code: [Select]
DDRD |=~_BV (6);
Did you mean "&=" ?

Unles you want the ultimate in speed, I suggest you stick with the Arduino-supplied functions.


scotcarl

Hi all.
Long time read, first time poster.
Firstly, big shout out to nickgammon and CrossRoads, who have pretty much provided all of my education on arduino boards.  Kudos guys.
I know this is a very old post, but I think this is the best place for these 2 questions - I hope they get through.

Regarding use of the port mapping here (thank you for setting this up), the pin obviously has to be set as input or output prior to use, but does the direction have to be set via the direct access functions, or can we use the arduino pinMode() function in its place.
eg, if I wanted to read data from pin 5 on a Mega2560, I can set the pin direction and then interrogate the port  as follows

Code: [Select]

// Pin 5 = PortE3
DDRE &= ~_BV (3); // Set as input
x = (PINE & _BV (3)) == 0; // read from PortE3


can I write this instead

Code: [Select]

// Pin 5 = PortE3
pinMode (5, INPUT) // Set as input
x = (PINE & _BV (3)) == 0; // read from PortE3


Secondly, if I wanted to set the pin as input with pull up resistor active, could I please get feedback on whether my understanding is correct that we would set the port as input, then write high to it (like it was an output) as follows.

Code: [Select]

// Pin 5 = PortE3
DDRE &= ~_BV (3); // Set as input
PORTE |= _BV (3); // enable pull-up resistor
x = (PINE & _BV (3)) == 0; // read from PortE3


and is this equivalent (less any fancy extras performed by the pinMode() function) to

Code: [Select]

// Pin 5 = PortE3
pinMode (5, INPUT_PULLUP) // Set as input with pull-up
x = (PINE & _BV (3)) == 0; // read from PortE3


Kind regards,
Scott.

Go Up