logo elektroda
logo elektroda
X
logo elektroda

Why does my if(led1==1) statement get skipped and else executed in LED display code?

54 16
ADVERTISEMENT
  • #1 21668185
    Usman Saleem
    Anonymous  
  • ADVERTISEMENT
  • #2 21668186
    Steve Lawson
    Anonymous  
  • #3 21668187
    Mark Harrington
    Anonymous  
  • #4 21668188
    Bob Loy
    Anonymous  
  • #5 21668189
    AB Ahmad
    Anonymous  
  • ADVERTISEMENT
  • #6 21668190
    Steve Lawson
    Anonymous  
  • ADVERTISEMENT
  • #7 21668191
    Usman Saleem
    Anonymous  
  • #8 21668192
    Steve Lawson
    Anonymous  
  • #9 21668193
    Usman Saleem
    Anonymous  
  • #10 21668194
    Steve Lawson
    Anonymous  
  • #11 21668195
    Steve Lawson
    Anonymous  
  • #12 21668196
    Usman Saleem
    Anonymous  
  • #13 21668197
    Usman Saleem
    Anonymous  
  • #14 21668198
    Pieter Kruger
    Anonymous  
  • ADVERTISEMENT
  • #16 21668200
    Steve Lawson
    Anonymous  
  • #17 21668201
    george gonzalez
    Anonymous  

Topic summary

The discussion addresses a problem in C code for an 8051 microcontroller using Keil uVision, where an if statement checking if a bit-addressed LED pin (led1) equals 1 is unexpectedly skipped, causing the else block to execute instead. The led1 variable is defined as a single bit (sbit) mapped to P3.0, representing an LED connected to pin 1.1. Despite assigning led1=1, the if condition (if(led1==1)) does not behave as expected. Possible causes include the nature of bit-addressed I/O pins on the 8051, where writing to a port bit may not be reliably read back due to hardware configuration or the difference between PORT and LAT registers in some microcontrollers. Suggestions include masking the bit with a bitwise AND (if((led1 & 1) == 1)) or assigning led1 to an integer variable before comparison to handle integer promotion issues. The importance of posting complete, properly formatted code and verifying LCD initialization and display routines was emphasized. Reviewing the generated assembly (.LST) file to understand compiler behavior on bit variables was recommended. The problem likely stems from reading a hardware port bit that does not reflect the written value, common in memory-mapped I/O on 8051 devices.
Summary generated by the language model.
ADVERTISEMENT