Port Register Manipulation

I am trying to access the digital ports directly to simply turn on an LED. I am able to do it use pinMode() and digitalWrite(), but not by directly accessing the registers.

LED.ino (284 Bytes)

Use pinMode to set it as an output. for example, pinMode (2, OUTPUT); // D2 as an output Then PORTD = PORTD | 0b00000100; // D2 high, rest unchanged, PORTD on the Uno. PORTD = PORTD & 0b11111011; // D2 low, rest unchanged

see Direct Port Manipulation in the Learning tab, under Reference I think.

If you just want to toggle it from whatever it is now: PIND = PIND | 0b00000100; // toggle D2

If you just want to toggle it from whatever it is now:

PIND = 0b00000100; // toggle D2

;)

Which arduino are you using?

OP's code

void setup() {
  // put your setup code here, to run once\
  DDRD = B11111111; // set PORTD (digital 7~0) to outputs

}

void loop() {
  PORTD = B11110000; // digital 4~7 HIGH, digital 3~0 LOW
  delay(1000);
  PORTD = B00001111; // digital 4~7 LOW, digital 3~0 HIGH
  delay(1000);

}

OP's code looks fine if you're using an Uno (for other boards, the code needs to be adjusted, since digital pin 2 isn't PD2 - that's one of the reasons we use the Arduino wrappers instead of direct port manipulation most of the time)

I am using an arduino uno. Instead of using the DDRD register to set the direction of the pins, I used pinMode() instead and it still does not work. This is a relatively easy task and I have done it with other micro-controllers. Could there be something wrong with my arduino? Also I have already looked at the reference guide on this topic.

PIND = 0b00000100; // toggle D2

I don't recall that working for me last time I tried it. Will have to reinvestigate I guess.

For UNO

//                            DDDDDDDD
//                            76543210        
#define SETd2       PORTD |= B00000100  // D2 HIGH
#define RESETd2     PORTD &= B11111011  // D2 LOW
#define TOGGLEd2     PIND  = B00000100  // D2 Toggle
#define TESTd2      (PIND  & B00000100) // D2 is tested

CrossRoads: I don't recall that working for me last time I tried it.

It did. Trust me.

PIND is read only, I am wanting to write to a pin.

jacksondelametter: PIND is read only, I am wanting to write to a pin.

PORTD is the output name.

Yes, from my source code you can see that I am using PORTD to write a high to a pin and it is not working, Anyway I can fix that?

it still does not work

How are you determining that it does not work. Do you have 8 leds hooked up and are blinking 4 at a time?

When I move the connection to a single led across pins 0:7 I can see it blinking with both your original code and the pinMode code.

void setup() {
//DDRD = B11111111;
  for (byte i = 0; i< 8; i++)
  {
  pinMode(i,OUTPUT);
  }
}
void loop() {
 PORTD = B11110000; // digital 4~7 HIGH, digital 3~0 LOW
  delay(1000);
  PORTD = B00001111; // digital 4~7 LOW, digital 3~0 HIGH
  delay(1000);
}

jacksondelametter: PIND is read only, I am wanting to write to a pin.

Nope, its read-write, and writing to it toggles the pin. See the datasheet for details. Saves having to have another register for single-instruction toggling.

I got it to work, I do not know what I did differently. Thank you

That's how it goes sometimes. Always blame yourself.

.