Visualizing bits in a register

The page https://docs.arduino.cc/learn/programming/bit-math/ gives below example for setting pins using registers, supposedly much faster than using a loop and pinMode and digitalWrite. Is there an easy way to print each bit for these ports in loop()?? Thanks for any help. AA

void setup() {
  DDRD = B11111110;  // digital pins 7,6,5,4,3,2,1,0 
  PORTD &= B00000011;   // turns off 2..7, but leaves pins 0 and 1 alone
  DDRB = B00111111;  // digital pins -,-,13,12,11,10,9,8
  PORTB = B00111000;   // turns on 13,12,11; turns off 10,9,8
}
void setup()
{
    Serial.begin(115200);
    for (int b = 7; b >= 0; b--)
    {
        Serial.print(bitRead(DDRD, b));
    }
    Serial.println();
}

void loop()
{
}

You could just use
Serial.println(DDRD, BIN);
but leading zeroes will not be printed

Official page introduction to ports here.
https://docs.arduino.cc/retired/hacking/software/PortManipulation/

If you want to see the change in PORT value

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

DDRD = DDRD | B11111100;

PORTD = B10100000;

}

void loop() {

// put your main code here, to run repeatedly:

Serial.println("Binary");

Serial.println(PORTD,BIN);

Serial.println();

Serial.println("Decimal");

Serial.println(PORTD,DEC);

Serial.println();

delay(5000);

PORTD += 4;

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.