Using PIND

I’m banging my head because I can’t solve this, yet it seems so simple to me. I’m trying to use PIND to read a byte from port d as digital. After investigating I read you can’t read bits 7 and 8 of port d as digital, yet the atmega328 datastasheet says you can use the whole port. Did I read the datasheet wrong or is this an arduino limitation?

Here is my code:

void setup() {                                                         
  Serial.begin(2500000);
  DDRD = B00000000;
}
  
void loop() {
  Serial.print(PIND, BIN);
  Serial.print ("\n"); 
  Serial.print(digitalRead(14), BIN);
  Serial.print(digitalRead(15), BIN);
  Serial.print(digitalRead(16), BIN);
  Serial.print(digitalRead(17), BIN);
  Serial.print(digitalRead(18), BIN);
  Serial.print(digitalRead(19), BIN);
  Serial.print(digitalRead(20), BIN);
  Serial.print(digitalRead(21), BIN);
  Serial.print ("\n");  
  Serial.print ("\n");  
  delay(1000);
}

Output (as I bring each pin high):

11
10000000

11
01000000

11
00100000

11
00010000

11
00001000

11
00000100

As you can see, digitalread works as expected, but it seems PIND always is 00000011.

Furthermore, I tried to enable the internal pullups with PORTD = B11111111; The pullups don’t seem to work, but I get:

11111111
10000000

11111111
01000000

11111111
00100000

11111111
00010000

11111111
00001000

11111111
00000100

Any ideas? Thanks.

I think you are getting AVR PORTS Vs Arduino pin numbers mixed up.

Your using Arduino pin numbers 14-21 (of which 20 & 21 are not avalible) which belong to PORTC not PORTD, so you are comparing different ports in your printing statements. PORTD covers Arduio pin numbers 0-7.

Lefty

Oh... I knew it was something stupid like that. It's working now. Thanks a lot Lefty.