This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

If statement is evaluating even though the value is 0

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!

Parents
  • 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

Reply
  • 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

Children
No data