Confused over port manipulation

I’m trying to understand what the follow sample code does I got from github. Read through the port manipulation docs and still very confused over this.

void flash_change_pins_mode(boolean io)
{
  if (io) {
    DDRD &= ~(1<<2);
    DDRD &= ~(1<<3);
    DDRD &= ~(1<<4);
    DDRD &= ~(1<<5);
    DDRD &= ~(1<<6);
    DDRD &= ~(1<<7);
    DDRB &= ~(1<<0);
    DDRB &= ~(1<<1);
  }
  else
  {
    DDRD |= (1<<2);
    DDRD |= (1<<3);
    DDRD |= (1<<4);
    DDRD |= (1<<5);
    DDRD |= (1<<6);
    DDRD |= (1<<7);
    DDRB |= (1<<0);
    DDRB |= (1<<1);
  }
}

So if io = 1 then run the first section.
DDRD is port D register. read/write
&= is compound bitwise and
<< shift the bit s left.

So putting all that together what does DDRD |= (1<<2); do?

1<<2 is B00000100. So set pin 2 as output? Then set put 3 as output etc etc. Does this routing just set all the pins as output if io=1 or all inupts if io=0

Isn’t there a less confusing way of doing this?

You have found a particularly odd example of port manipulation. It is doing something similar to pinMode(). Post a link to the site where you got this.
Incidentally, the tilde ~ does a bit wise inversion.

gregmcc:
I’m trying to understand what the follow sample code does I got from github. Read through the port manipulation docs and still very confused over this.

void flash_change_pins_mode(boolean io)

{
 if (io) {
   DDRD &= ~(1<<2);
   DDRD &= ~(1<<3);
   DDRD &= ~(1<<4);
   DDRD &= ~(1<<5);
   DDRD &= ~(1<<6);
   DDRD &= ~(1<<7);
   DDRB &= ~(1<<0);
   DDRB &= ~(1<<1);
 }
 else
 {
   DDRD |= (1<<2);
   DDRD |= (1<<3);
   DDRD |= (1<<4);
   DDRD |= (1<<5);
   DDRD |= (1<<6);
   DDRD |= (1<<7);
   DDRB |= (1<<0);
   DDRB |= (1<<1);
 }
}




So if io = 1 then run the first section.

No, if io is not zero, run the first section. Any non-zero value evaluates as true

DDRD is port D register. read/write

DDRD is the port D direction register. It makes the pins either input (0) or output (1)

&= is compound bitwise and
<< shift the bit s left.

So putting all that together what does DDRD |= (1<<2); do?

1<<2 is B00000100. So set pin 2 as output? Then set put 3 as output etc etc. Does this routing just set all the pins as output if io=1 or all inupts if io=0

No, if io is non-zero, it clears all those bits making them inputs. That’s the &= ~(1<<2) lines.

Isn’t there a less confusing way of doing this?

You don’t have to do them all individually

void flash_change_pins_mode(boolean io)
{
  if (io) {
    DDRD &= ~(B11111100);
    DDRB &= ~(B00000011);
  }
  else
  {
    DDRD |= (B11111100);
    DDRB |= (B00000011);
  }
}

or save yourself some grief and just use pinMode() and make your code easier to read/understand

6v6gt:
You have found a particularly odd example of port manipulation. It is doing something similar to pinMode(). Post a link to the site where you got this.
Incidentally, the tilde ~ does a bit wise inversion.

Thanks for the info. I got a bit confused over the operations but seem to be following.

The code is to program a Atmel chip so original author is using this code as opposed to pinMode for the speed.

The example above in doing them all at once makes a lot of sense.