// digitalWrite(Step_Pin , !digitalRead(Step_Pin));
PIND =(PIND > 0b00000010);
I am trying to toggle the GPIO 2 pin. I am trying to toggle PD1 0b00000010 with the xor ( > ).
So what am I doing wrong?
I am using the Itsy Bisy 32U4 3V.
Thank you for sharing your wisdom.
[sterretje edit]
Moved closing code tag so question was readible.
That will work fine, even though it's weird, because it compiles to an SBI instruction... PIND |= 0b11; is likely to fail, though, because it will actually read the register, do the OR, and write the new value... C really doesn't understand "memory" that doesn't become 1 when you write a 1 to it.
Yes, three separate instructions (including the XOR) versus writing PIND (one instruction, relying on the XOR hidden in the hardware behind PINx writing)
PORTD = PORTD ^ 0b00000010;
1c2: 8b b1 in r24, 0x0b ; 11
1c4: 8c 27 eor r24, r28
1c6: 8b b9 out 0x0b, r24 ; 11
PORTD = PORTD ^ 0b00000010;
1c8: 8b b1 in r24, 0x0b ; 11
1ca: 8c 27 eor r24, r28
1cc: 8b b9 out 0x0b, r24 ; 11
PORTD ^= 0b00000010;
1ce: 8b b1 in r24, 0x0b ; 11
1d0: 8c 27 eor r24, r28
1d2: 8b b9 out 0x0b, r24 ; 11
PIND = 0b00000010;
1d4: c9 b9 out 0x09, r28 ; 9
PIND = 0b00000010;
1d6: c9 b9 out 0x09, r28 ; 9
PIND = 0b00000010;
1d8: c9 b9 out 0x09, r28 ; 9
Yes. It makes one wish for the days (avr-gcc 3.x? A long time ago!) when you had to use sbi(regX, bit); instead of the compiler folk saying "oh, we'll just optimize the typical C expression." (note that SBI does NOT do an actual read/modify/write cycle on the whole register.) (even worse, I believe that many modern environments, including Arduino, re-implement sbi() as:
which we've noticed does NOT always do the same thing!
This sort of thing causes problems with other registers where "writing a one bit" has effects other than setting the destination bit to one. Notably, some "clear interrupt" registers clear a bit when a one is written, so a statement like:
myuart->intflags |= UART_ERRINT_F;
will unexpectedly clear more than just the error interrupt flag.