# Need Help with Port Manipulation

I'm trying to understand DDR and PORT registers for D and B but am confused on doing it properly

What do these translate to? What does the B mean?
DDRD = B11111110
DDRD = DDRD | B11111100;

I want to make ports 6, 9, 10, 11 outputs
and 2, 4, 7, 8, 12 inputs.

then later in the code I want to do digitalWrite for multiple pins as HIGH in a single statement.

Have you looked at? :

B is binary

From the most useful source of information I found, I've concluded that I would use:

``````DDRD = 0b10010111;
DDRB = 0b110001;
``````

Can you confirm if this is correct?

DDRD = 0b10010111;
DDRB = 0b110001;

For the UNO

``````DDRD is for D7 through D0

76543210
DDRD = 0b10010111;
You would be making D7, D4, D2, D1, D0 outputs

Playing around with D0 and D1 messes with your serial pins.

Then you are switching to port B which is for D13 to D8
1111
321098
DDRB = 0b110001
D13, D12, D8 outputs
``````

The standard way is to use pinMode(pin, mode), where mode is INPUT, INPUT_PULLUP or OUTPUT.

Usually the pin modes are set once early on so it isn't a performance issue and so it's not typically necessary to do direct port writes. You could code it like this:

``````pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(12, INPUT);
``````

Thank you Larry. That helps out a lot. I just did a quick LED test to confirm.

Jboyton, I am just curious on port manipulation as this is something I've never learned. Using this along with functions will save me a lot of space in my coding.

On an Uno none of the ports makes all 8 pins available to the user so you should not use = to set values.

For example don't use PORTD = b11110000 because pins 0 and 1 are Rx and Tx

Instead set pins to 1 with PORTD |= b11110000 - using OR the 0s won't change anything
and
set pins to 0 with PORTD &= b00001111 - using AND the 1s won't change anything

(Hope I have this the right way round )

...R