0

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.

Phil Wetzel
  • 135
  • 4
Dimo
  • 51
  • 1
  • 6
  • Looks okay. Do you have the assembler output? – Jon Mar 23 '15 at 17:26
  • You are actually testing 4 pins, not 2. Are you sure you are comparing like with like, that the other 2 inputs are low? If you want to test 2 pins the code should be `if((P1 & 0xa0) == 0xa0)` – Weather Vane Mar 23 '15 at 19:12
  • What's the type of `ORange`? Can you include the declaration? If it isn't at least 8 bits your second `if` will never be true. – Phil Wetzel Mar 24 '15 at 10:53

1 Answers1

0

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.

Dimo
  • 51
  • 1
  • 6