1

I want to set one pin's output value, and then set a different pin's output value so that the two pins change value one after the other. However, the compiler optimises the two calls to change both pins in one go.

PORTD |= _BV(0); //set pd0 to high PORTD |= _BV(1); //set pd1 to high PORTD &= ~(_BV(1)); //set pd1 to low

The two pins go high simultaneously. I tried inserting a NOP between these but it doesn't change behaviour.

(I originally intended to include an oscilloscope screengrab to show what was happening - that's how I knew (or got the wrong impression) that the two pins were changing at the same time. But the photo wasn't shifting from my Nexus 7 for me to access it from my laptop.)

asked Dec 11, 2014 at 22:49
7
  • I can't duplicate this behavior. Which version of GCC is in use? Commented Dec 11, 2014 at 23:49
  • What application requires those pins to be turned on one after the other, but so quickly that you don't need any code to cause a delay between the two? Commented Dec 12, 2014 at 0:38
  • Cool - there's an Arduino stackexchange! Thanks to the mods for migrating the question. Commented Dec 12, 2014 at 14:58
  • It sounds like I've just got myself confused here, since the behaviour I've seen isnt repro-able. Commented Dec 12, 2014 at 14:59
  • (by others, that is). Commented Dec 12, 2014 at 15:26

1 Answer 1

7

I certainly can't duplicate what you are saying. The small code snippet:

void setup() {
 PORTD |= _BV(0);
 PORTD |= _BV(1);
 PORTD &= ~(_BV(1));
}
void loop() {
}

compiles into:

000000a6 <setup>:
 a6: 58 9a sbi 0x0b, 0 ; 11
 a8: 59 9a sbi 0x0b, 1 ; 11
 aa: 59 98 cbi 0x0b, 1 ; 11
 ac: 08 95 ret
000000ae <loop>:
 ae: 08 95 ret

That is clearly two separate bit sets followed by a bit clear.

I am using UECIDE that is set to use GCC 4.3.2

If I switch to GCC 4.8.1 instead, the code generated is identical.

Even turning on full -O3 optimization I still get the same results.

So how are you determining that it's happening at the same time?

answered Dec 12, 2014 at 0:37
4
  • 5
    Which makes sense since the memory location has been declared as volatile. Commented Dec 12, 2014 at 0:39
  • I think I'm using Arduino IDE 1.06 but I will need to check when I get home. Commented Dec 12, 2014 at 15:09
  • I observed the two pins changing on an oscilloscope, but I will have to take a step back and do a test with the Arduino Micro removed from the project I'm working on. Sounds like I'm just mistaken. Commented Dec 12, 2014 at 15:11
  • Same code generated here with avr-gcc 5.4.0. Commented Oct 25, 2019 at 14:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.