0017 Serial and port defining

If you with to use serial, and you change the DDRD register, serial will not work.

The following code does not work and prints gibberish.

setup()
{
DDRD = 0b11111111;
Serial.begin(9600);
}

The following code works fine.

setup()
{
DDRD = 0b11111100;
Serial.begin(9600);
}

Direct port addressing isn't part of the arduino interface. If you look at the description in the reference they warn you about exactly this issue.

I've used direct port access and have had no problems doing what I needed to do. However the Arduino reference has this caution:

"Referring to the pin map above, the PortD registers control Arduino digital pins 0 to 7.

You should note, however, that pins 0 & 1 are used for serial communications for programming and debugging the Arduino, so changing these pins should usually be avoided unless needed for serial input or output functions. Be aware that this can interfere with program download or debugging.

DDRD is the direction register for Port D (Arduino digital pins 0-7). The bits in this register control whether the pins in PORTD are configured as inputs or outputs so, for example:

DDRD = B11111110; // sets Arduino pins 1 to 7 as outputs, pin 0 as input DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs // without changing the value of pins 0 & 1, which are RX & TX "

That from here: http://www.arduino.cc/en/Reference/PortManipulation

Lefty