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
  • Try breaking your code down:

    
    for (i=0; i<8; i++){
            unsigned char P1_Copy;
    
            P1_Copy = P1;
    
            if ((P1_Copy >> i) & 1){ //if the channel is high (idle)
                    P1mask &= ~(1 << i); //clear the bit
                    chan_counter[i] = 0xFF; //reset the channel counter
            }
    }
    
    

    Modify that addition and see if you can determine what is going wrong.

Reply
  • Try breaking your code down:

    
    for (i=0; i<8; i++){
            unsigned char P1_Copy;
    
            P1_Copy = P1;
    
            if ((P1_Copy >> i) & 1){ //if the channel is high (idle)
                    P1mask &= ~(1 << i); //clear the bit
                    chan_counter[i] = 0xFF; //reset the channel counter
            }
    }
    
    

    Modify that addition and see if you can determine what is going wrong.

Children