Read PORTD on Arduino Mega2560

Hi people!

Yesterday I tried to read PORTD port register on an Arduino Mega2560. I found some docs saying that this port is mapped from the pin 38 to 45. I wrote the following program to read this port, but if I change any value in those pins, no value is changed at PORTB port register. Any advices?

/

//read_rom.ino

void setup() {
  DDRD = 0x00; // All pins in PORTD are inputs

  PORTD = 0xFF;    // Pull-ups enabled in all pins at PORTD
   

  Serial.begin(115200);

}

void loop() {

  Serial.println();
  Serial.println(PIND, HEX);

  delay(30);
}

Why does your program reference PORTD?

Read port B using PINB.

Welcome to the forum.

First, the title and body of your post refer to PORTB, but the code indicates PORTD. Which register do you actually wish to read?

I found some docs saying that this port is mapped from the pin 38 to 45.

I don't know what you found, but that information is wrong for either of the ports.

https://www.arduino.cc/en/Hacking/PinMapping2560

PB0:7 are processor pins 19-26 and PD0:7 are processor pins 43-50.

but if I change any value in those pins, no value is changed at PORTB port register

You also need to understand the relationship between the physical pins on the processor and the pins on the Arduino board. Are you changing the PORTX pin values as outputs in your code? If so, reading PORTX should reflect the changes, but that is a pretty useless exercise. It sounds as if you may be trying to read the state of the pins as inputs in which case the syntax is PINX. The physical pins on the board which map to that register are not the same as the chip pins.

Sorry, I typed PORTB instead of PORTD.

You also need to understand the relationship between the physical pins on the processor and the pins on the Arduino board. Are you changing the PORTX pin values as outputs in your code? If so, reading PORTX should reflect the changes, but that is a pretty useless exercise. It sounds as if you may be trying to read the state of the pins as inputs in which case the syntax is PINX. The physical pins on the board which map to that register are not the same as the chip pins.

I know the relationship between physical pins and the pins on the board. The PORTD is used as input with pull-ups enabled. That's not an exercise, I need that to read an output from an EPROM with 8 outputs ports.

I just need to know the pins on the board, not the physical pins on the MCU.

You can follow the PORT pins out to the header pins on the schematic.

Not all of PORTD is broken out it seems.

arduino-mega2560_R3-sch.pdf (47.1 KB)

jremington:
Why does your program reference PORTD?

Read port B using PINB.

Thanks!

That changed the value only when I change de value of pin 38 at Arduino board.
Do you know the others?

43 PD0 ( SCL/INT0 ) Digital pin 21 (SCL)
44 PD1 ( SDA/INT1 ) Digital pin 20 (SDA)
45 PD2 ( RXDI/INT2 ) Digital pin 19 (RX1)
46 PD3 ( TXD1/INT3 ) Digital pin 18 (TX1)
47 PD4 ( ICP1 )
48 PD5 ( XCK1 )
49 PD6 ( T1 )
50 PD7 ( T0 ) Digital pin 38

CrossRoads:
You can follow the PORT pins out to the header pins on the schematic.

Not all of PORTD is broken out it seems.

So, it means that I cannot use all pins of PORTD because they are not connected to the board?

cattledog:
43 PD0 ( SCL/INT0 ) Digital pin 21 (SCL)
44 PD1 ( SDA/INT1 ) Digital pin 20 (SDA)
45 PD2 ( RXDI/INT2 ) Digital pin 19 (RX1)
46 PD3 ( TXD1/INT3 ) Digital pin 18 (TX1)
47 PD4 ( ICP1 )
48 PD5 ( XCK1 )
49 PD6 ( T1 )
50 PD7 ( T0 ) Digital pin 38

Thanks!!
I found these pins, but unfortunately it seems that PD4..PD6 are not accessible :frowning:

I managed to use PORTB instead of PORTD, because all of its pins are available.

PB7 - PIN 13 ??
PB6 - PIN 12
PB5 - PIN 11
PB4 - PIN 10
PB3 - PIN 50
PB2 - PIN 51
PB1 - PIN 52
PB0 - PIN 53

gduarte:
I found some docs saying that this port is mapped from the pin 38 to 45.

That doc lied to you. Pin 38 is PD7 but PD0-3 is Pins 21-18 and the other bits of PORTD do not map to digital pins.

Try one of these other ports that ARE on 8 consecutive pins:
PORTA: Pins 22-29 (bits 0-7)
PORTC: Pins 30-37 (bits 7-0)
PORTF: Pins A0-A7 (bits 0-7)
PORTK: Pins A8-A15 (bits 0-7)
PORTL: Pins 42-49 (bits 7-0)

Note that some ports (C and L) map bits to pins in reverse order.

So, it means that I cannot use all pins of PORTD because they are not connected to the board?

You got it. You seem to have moved on from there already.