Direct Port Manipulation with PORTD ( 328p )

Hi, ALL; I´ve done some work with Direct Port Manipulation but never with PORTD and less D0 and D1. The best I have is an328p and as I want to try an 8 x 8 unicolour LED Matrix, I´ll need 16 I/O pins. ( I know I can use shift registers, but I´ll start without them).
Now, I´ll have to combine port pins like so:

int row[8] = {5, 6, 7, 8, 9, 10, 11, 12, };
int column[8] = {3, 4, A0, A1, A2, A3, A4, A5};

DDRB = B111111; // set all PORTB pins as OUTPUTs
DDRC = B111111; // set all PORTC pins as OUTPUTs

Not sure about PORTD? Can´t afford to brick my 328p!, surely need to reupload code.
DDRD = B11111110; // set all PORTD pins, except D0, as OUTPUTs.
// supposing D0 is an INPUT and D1 is an OUTPUT
I´ll be using PNP transistors for columns and rows, then:

void AllON()
{
PORTB = B100000;
PORTC = B000000;
PORTD = B00000110;
}

void AllOFF()
{
PORTB = B111111;
PORTC = B111111;//I know that by just switchng all columns ( or rows ), all LEDS go OFF, but…
PORTD = B11111110;
}

I haven´t written any code except for a 5 x 5 Matrix; just need to know if this works.
If so, I can do the rest, THANKS.

When you reset the Arduino, the port pins all default to inputs. So, your program can set any combination of input and output you need, without having to worry about bricking.

Unless you are using the serial monitor, you can also use D0 and D1 for general purpose I/O.

You can take over PORTD, with the following caveats:

PD0 will be connected to TX line of serial adapter through a resistor (1k IIRC); the other end of this resistor is held high. You must make sure that this will not pose a problem.

In order to upload sketches, the serial adapter must be able to drive PD0 through that 1k resistor, and your controller must be able to drive PD1 high and low. You may need to provide a means to disconnect these pins from the rest of your project while uploading.

It doesn't matter what the code sets it to do - a hardware reset is performed (via DTR reset trick) to start the bootloader before it starts uploading, so the pins are restored to their reset state.

If you use Serial.begin() in your sketch, the UART functionality will take over PD0 and PD1 - in this case, DDRD, PORTD, etc will be ignored for those pins.

Great; that will simplify my code, which is very tedious, and where I come from, strive to survive, I cant afford another 328p. Thanks a lot.
BTW, Ive searched internet for software to build an 8 x 8 unicolor LED Matrix but all come down to RGB and special panels and modules; Any idea?

alah:
BTW, Ive searched internet for software to build an 8 x 8 unicolor LED Matrix but all come down to RGB and special panels and modules; Any idea?

The most basic method, not using any of the myriad of IC, is to drive the 8 sources with PNP transistors, taking them low to turn them on, and drive the 8 sinks with NPN transistors, taking them high to turn them on. Don't forget your current limiting resistors.
There are various IC, collectively labeled LED Display Drivers, that attach the 8x8 matrix to a bus on the micro, usually SPI, I2C, MC6800 etc.

Thanks perehama; Im trying to get a 1284 due to more memory
Meanwhile, Im doing it as you suggest; those chips cost an eye ( even both).