I have a bit-addressable var (ibase) that I am loading with a value and then rotating--placing the msb on an I/O pin (SData). The SData pin never changes--it stays high. What am I doing wrong? Thanks!
for(i=0;i<8;i++) { SClock = 0; SData = ibase^7; ibase = _irol_(ibase,1); delay(); SClock = 1; timer0_delay(5); }
This line:
SData = ibase^7;
doesn't do what you think. You've fallen into the somewhat common trap of believing that the
sbit var=bitvar^bitno
syntax extends to all expressions involving bit-addressable variables. Well, it doesn't. You need something along the lines of
SData = (ibase & 0x80) ? 1 : 0;
Wow, thanks! That did it. So that I don't continue to suffer in ignorance, just what did that do? (I understand the 0x80 mask, but what about the ? 1 : 0 syntax)?
Thanks.
"(I understand the 0x80 mask, but what about the ? 1 : 0 syntax)?"
It is called a "conditional expression" and is equivalent to:
SData = ((ibase & 0x80) != 0);
and:
SData = !!(ibase & 0x80);
Got it--nice shorthand. Thanks.
It is called the Ternary Operator - it's a standard ANSI 'C' feature.
As Dan says, the Ternary Operator is used to create a conditional expression
a = (x==y) ? b : c;
is equivalent to
if( x==y ) { a = b; } else { a = c; }
Better continue with the C classes.
When ^ is used in a C/C++ expression, it is a bit-wise xor. This is all part of the standard, while the ^ used to declare bit variables in Keil is a Keil-specific extension required since the C/C++ standards do not have support for bit variables.
Thanks to all who posted the useful comments. I have always programmed in assembler (and frankly, considered embedded C to be a bit of a bloated alternative). I'm not taking classes, just studying on my own, which is why I asked for help on the syntax.
The gratuitous "Better continue with the C classes" was undoubtedly the least helpful, because it was designed to be denigrating, not illuminating. After all, I said that I understood the concept perfectly several postings ago.
Ooohh ... nothing contentious there, then...!
;-)
But you are actually wrong on several accounts.
The sentence "Better continue with the C classes." was not designed to be denigrating.
You did use the xor construct, missing the meaning of ^ when used in a C expression, as in contrast to the Keil extension of declaring bit variables.
And no, you did not several posts up said that you understood the concept.
You did say: "Wow, thanks! That did it. So that I don't continue to suffer in ignorance, just what did that do? (I understand the 0x80 mask, but what about the ? 1 : 0 syntax)?
Thanks."
That must be read as relating to the ternary operator.
After having received a quick lesson about the ternary operator, you answered with "Got it--nice shorthand. Thanks."
Still no mention that you understood the ^ operator.
So, since only the ternary operator was described, I thought that I could be nice - yes, as in helpful - and continue what Dan Henry and And Neil started by describing the meaning of the xor operator.
I realize that "Better continue with the C classes" can be read as "You should continue with...", but if you think about it again, it can also be read as: "Since we are describing C functionality, let's continue by describing this construct, that you are also having troubles with."
It is always dangerous to start throwing punches around, unless you are really, really sure what you are doing.
Not really throwing punches; I expressed my gratitude to all--you too. Obviously, I am a neophyte on the HL side of embedded programming, or I would not have asked such an (for you) elementary question. I didn't just take the OPs suggestion and paste it, but also wanted to know _how_ it worked. The reason that I said that I thought the classes reference was gratutitous, was that my inexperience was implicit in such a fundamental question. Of course I need to learn more. Anyway, thanks again for a more complete explanation--it all helps!
View all questions in Keil forum