PORTD doesn't work (bare ATmega328) [solved]

Ok, so after getting some feasibility questions answered in the microcontroller section, I have started to program my project (mostly driving a 7-segment display [bare '328]).

I have successfully been able to modify the PORTB values, but I am having trouble with PORTD. Namely, with this bit of code (library).

void Seg::initialization()
{
  DDRB = B11111111;
  DDRD = DDRD | 11110011;
  /*
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);*/
  PortD[0] = 00010011; //The HIGH value of pins 0 and 1 are for something else
  PortD[1] = 00100011;
  PortD[2] = 01000011;
  PortD[3] = 10000011;
}
void Seg::update() {
  for (int x = 0; x < 4; x++) {
    //digitalWrite(x+4, HIGH);
    PORTB = PortB[x];  //don't worry about this, there's more to this program
    PORTD = PortD[x];
    delayMicroseconds(5000);
    //digitalWrite(x+4, LOW);
  }
}

The PORTD values don't all change state, but when digitalWrite is used instead, it works perfectly.

PORTD includes the Rx Tx pins which you probably should not be trying to change.

Do you know how to selectively change some of the bits in a PORT using &= and |= ?

...R

Robin2:
PORTD includes the Rx Tx pins which you probably should not be trying to change.

Do you know how to selectively change some of the bits in a PORT using &= and |= ?

...R

Also, since I am using a bare '328, I'm planning to eventually use the TX and RX pins as I/O. Is this a bad idea?

I'm still trying to figure out the bitmath... do you have any suggestions?

You want to use direct port manipulation.

jremington:
You want to use direct port manipulation.

Exactly, except PORTD only seems to work on PD7 and PD6... and strangely, according to the oscilloscope, pin 7 seems to also be acting like an extra pin! (Double duty cycle w/ tiny dip halfway through HIGH state)

Post your complete program. There may be a glitch somewwhere else.

And if you are using port manipulation please include plenty of comments so it is clear what you are supposed to be doing.

...R

Turns out... I was trying to assign them to an 8 digit integer! (I didn't designate them all as Bytes)!

PORTD = 01000000;

Needs to be

PORTD = B01000000;

Thanks for your time!

Or

PORTD = 0b01000000 ;

Since the language can already specify binary values.

Personally I'd use hexadecimal, because counting zeroes in values like 0b01000000000000000000000000000000 is error-prone and painful. 0x40000000 is
so much easier.

PORTD = 0x40 ;

MarkT:
Or

PORTD = 0b01000000 ;

Since the language can already specify binary values.

Personally I'd use hexadecimal, because counting zeroes in values like 0b01000000000000000000000000000000 is error-prone and painful. 0x40000000 is
so much easier.

PORTD = 0x40 ;

Thanks for the suggestion! Maybe that could fix some issues I'm having with assigning bytes to an array!

Speaking of which... How should I convert the binary value into a valid hex?

legowave440:
Speaking of which... How should I convert the binary value into a valid hex?

You don't need to convert anything
The binary value 0b00001100 is at the same time the decimal value 12 and the hex value 0A.

...R

So I can assign PORTD to an integer? [decimal value]

You can assign decimal, hex, octal, or binary to PORTD, or indeed any numeric variable.

Five dogs are five dogs regardless of which number base you use to express the concept of five.

Thanks! Solved!