keil c51 weird masking behaviour

159 Views Asked by At

I am trying to read two pins from a c8051f040 controller.

Reading the port directly works, but saving the same port value to a variable does not work even though the debugger shows the correct value.

// This works
if((P1 & 0xF0) == 0xa0) 
{   
    YEL_LED = 1;            //Turn on
}
else
{
    YEL_LED = 0;            //Turn off
}

// This does not work even though the debugger 
// shows the correct value 0xa0 for the var
ORange = (P1 & 0xF0);
if(ORange == 0xa0)          
{
    YEL_LED = 1;            //Turn on
}
else
{
    YEL_LED = 0;            //Turn off
}   

Is this a KEIL c51 bug or is something being optimized away.

1

There are 1 best solutions below

0
On

The variable was declared as char which is signed. It should be unsigned.

I got fooled by the debugger which was showing the the watch variable as unsigned.