Processor: 8051 (AT89LP4052)
for (i=0; i<8; i++){ if ((P1 >> i) & 1){ //if the channel is high (idle) P1mask &= ~(1 << i); //clear the bit chan_counter[i] = 0xFF; //reset the channel counter } }
In the debugger, when added to the Watch1 ((P1 >> i) & 1) shows 0 correctly when I switch off the port1 pins; but the code inside the {} still runs!
I can't figure out why this is not working. Another part of my code I am using
if ((P1 | P1mask) == 0xFF) {
and it works fine.
Any tips on getting this if statement to work? Thanks!
Actually I tried that, and in the debugger the variable P1_Copy was fixed at its initial value (I set a breakpoint the line after P1_Copy = P1;), where P1 itself would change to reflect the actual live value. I thought that very odd.
I would:
Check the define of P1
Look at the disassembly (and step thru it) to see what's really happening.
Here is (a snippet of) my definitions:
/* Byte Registers */ sfr P1 = 0x90; /* Port 1 */ /* Bit Definitions */ /* P1 0x90 */ sbit P1_0 = P1^0; /* */
OK, here is what I have done to solve the problem:
I simplified the code by doing away with the for-loop and make 8 if statements. Each looks like this:
if (P1_0) { P1mask &= 0xFE; chan_counter[0] = 0xFF; }
However... this suffers the same problem, the Watch window says that P1_0 is 0, but it still evaluates the if as true, grrr. The assembly is simple and looks correct:
102: if (P1_0) { C:0x088C 309006 JNB P1_0(0x90.0),C:0895 103: P1mask &= 0xFE; C:0x088F 5310FE ANL P1mask(0x10),#0xFE 104: chan_counter[0] = 0xFF; C:0x0892 7508FF MOV chan_counter(0x08),#0xFF 105: }
So, I thought I need to test this on the hardware, to test the simulator, and lo and behold it works as intended perfectly.
IS THIS A BUG IN THE SIMULATOR?! Caveat: I used SDCC to compile for the hardware.
Dan