DDRD PORTD for toggle LED pin 13 arduino uno

Hello,

Any reason why next code doesn't toggle the LED?

Void setup() {

DDRD = B10000000 ;
DDRD = DDRD | B10000000;
PORTD = B10000000;

}

void loop() {

PORTD = B10000000;
delay(1000);
PORTD = B00000000;
delay(1000);
}

Thanks

Any reason why next code doesn't toggle the LED?

Because it won't even compile. Post your real code, using the # icon.

Have you correctly connected an LED to Arduino Pin 7?

I am assuming an Arduino Uno R3.

Isn't pin 13 on PORTB ?

hello,

it is on portB, i was confused , i used the pinlayout of the chip it self and didn't use the portmapping layout.

thanks all.

Blink blink....

You're welcome.

BTW, you only need one of the two statements here:

    DDRB = B00100000 ;
    DDRB = DDRB | B00100000 ;

The first sets DDRB to B00100000. Bit 5 will be set to 1, all other bits will be set to 0.

The second sets bit 5 to a 1, regardless of its current state, and does not affect the rest of the bits.

If you want to unconditionally set a bit to 0 without affecting the other bits, use:

DDRB = DDRB & B11011111;

Or better still, use a shift and a NOT to specify it:

DDRB = DDRB & !( 1 << 5 );

A faster and shorter way to toggle pin 13 is this:

void setup ()
  {
  pinMode (13, OUTPUT);
  }  // end of setup

void loop ()
  {
  PINB = bit (5); // toggle D13
  delay (1000);  
  }  // end of loop
[quote author=Nick Gammon link=topic=193537.msg1430074#msg1430074 date=1381866386]
A faster and shorter way to toggle pin 13 is this:

void loop ()
  {
  PINB = bit (5); // toggle D13
  delay (1000);  
  }  // end of loop

[/quote]
OK.. I get how bit (5) works, as per #define bit(b) (1UL << (b)).

I get PINB being LED_PIN , though it's just for that purpose, and not really generic.

But what I don't get is how that statement toggles the pin. It does, obviously, but how? I would have thought it uncoditionally set LED_PIN to a 1. Is it somehow doing an xor under the covers somewhere?

PINB is normally used for input, but the datasheet defines that writing a 1 to a bit in it toggles the output register (PORTB).

Thanks Nick. I never saw anything like that on the AVRs I used to program (2313, 8515, 4433, etc.). Makes a lot of sense, since it's the SCK in SPI operation. I can't seem to find anything in the datasheet about toggling when a 1 is written, though.

Page 75, I/O ports overview.

The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register.

Also page 77:

13.2.2 Toggling the Pin

Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port.