The following does not work (ACC is always 0, despite out[0] is 255): ACC0 = out[0] & 1 == 1 ? 1 : 0; ACC1 = out[0] & 2 == 2 ? 1 : 0; out[0] &= 0xFC; send_string(" acc1"); send_byte(ACC); if (ACC0) out[0] |= 2; if (ACC1) out[0] |= 1;
Thank you, I've solved it this way: temp1 = out[0] & 0xFC; switch (out[0] & 3) { case 1: out[0] = temp1 | 2; break; case 2: out[0] = temp1 | 1; };
Well, there's the straightforward way:
bit temp; // save bit 0 temp = out[0] & 1; // bit 0 = bit 1 out[0] &= ~1; out[0] |= (out[0] & 2) ? 1 : 0; // bit 1 = temp out[0] &= ~2; out[0] |= temp ? 2 : 0;
bit tempBit; // temporary swap area bdata temp; sbit temp0 = temp^0; sbit temp1 = temp^1; temp = out[0]; tempBit = temp0; temp0 = temp1; temp1 = tempBit; out[0] = temp;
temp0 = temp0 ^ temp1; temp1 = temp0 ^ temp1; temp0 = temp0 ^ temp1;
PS Is something up with the source code highlighting stuff? It was highlighted in the preview, but not in the final post!
Well, for starters, do you really want to do this...
ACC0 = out[0] & 1 == 1 ? 1 : 0;
send_byte(ACC);
I can't help but think you are assuming that you can utilize the ACCumulator from C, which of course you can, but it is extremely ill-advised to do so. "ACC" is an SFR defined in regxx.h for the microcontroller's accumulator register -- the single most frequently used register! To assume that ACC contains anything predictable after a call to send_string() is sheer lunacy. In this case, it may happen to always be zero if only because (and this is a pure guess on my part) send_string() may return after encountering the NUL-terminator of the string you are passing it and having checked for zero using the accumulator before returning.
View all questions in Keil forum