Then yeah, I'd have to write assembly in source. I have no need so great....
and for most codes it would not make such a difference anyway
One FYI, when the write to PINx is done, all the 1 bits "written" toggle PINx bits whatever their state and all 0 bits don't. The contents of PINx will reflect pin states, you can toggle but not set those bits. I know, it uses PINx to toggle PORTx as a feature.
Yes using |= when you have more than one bit to flip will likely lead to bugs
I've been reading this thread with interest, mostly the last 9 posts.
I don't think I've ever used |= on the PINx registers to toggle more than one I/O pin but I always use it when writing to PORTB and PORTD registers because the Clock is on PB6, PB7 and RX/TX are on PD0, PD1.
I don't know if that matters but I do it anyway.
The PINx reg is different as a feature.
On the Uno, every port has some 'used' pins unless I don't begin Serial.
When reading asm code from compiler its better to mention compiler name and version and what optimization was used.
Microchip xc compilers for example generate very different code depending you paid for the license or no. One time i was almost injecting asm code in c to force it to use 1 cycle instructions vs 2 or 4.
yes for PORTD or PORTB it's a good practice
I wonder if it's touchy if you use the Serial line though
We know PORTD covers pin D0 to D7 and pins D0 and D1 are related to Serial (one is an input, the other one an output)
say you want to turn on pin 6 and 7 and not impact what's on the Serial line, you would do
PORTD |= 0b11000000;
which will translate into
in r24,0xb
ori r24,lo8(-64)
out 0xb,r24
It does feel possible that an interrupt arriving after the first instruction and modifying bit 0 and/or 1 but that the OR using the old value would overwrite what's actually there later...
are pins 0 and 1 somehow "protected" when Serial is activated?
Thanks, I thought so.
I have no Idea, that's why I've always done it that way just in case they're not.
well the point is that if they are not protected there is still a risk then, even with |= as the operation is not atomic
Is there a better way?
I don't know why but every time I post with a quote it doesn't show up and then I have to edit.
well you could always block interruptions whilst you mess around with the bits
cli();
PORTD |= 0b11000000;
sei();
that will translate in assembly into
cli
in r24,0xb
ori r24,lo8(-64)
out 0xb,r24
sei
because if you quote the full text (or most of the post) the forum software considers it's a bad practice and removes it
Maybe that's why the PINx write was made since the out port value is atomic?
Possibly indeed
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.