8051 polling code — does it check bit 2 of Port 1? Given the following program (formatted with real newlines), determine whether it checks if bit 2 of P1 is HIGH and, if so, writes FFH to Port 3. READ: MOV A, P1 ANL A, #02H CJNE A, #02H, READ MOV P3, #0FFH

Difficulty: Medium

Correct Answer: False

Explanation:


Introduction / Context:
This question tests precise bit masking and comparison in 8051 assembly. Correctly identifying which bit is being tested requires understanding hexadecimal masks and the CJNE instruction semantics.


Given Data / Assumptions:

  • Code reads Port 1 into A.
  • Mask used is #02H (binary 0000 0010).
  • Loop repeats until CJNE finds equality with #02H.


Concept / Approach:
Bit values are: bit0 = 01H, bit1 = 02H, bit2 = 04H, etc. Applying ANL A, #02H zeroes all bits except bit1. If bit1 is high, A becomes 02H; if bit1 is low, A becomes 00H. The CJNE A, #02H, READ loops until A equals 02H (bit1 set). Therefore, the code checks bit1, not bit2.


Step-by-Step Solution:

1) After MOV A, P1, accumulator contains P1 value.2) ANL A, #02H keeps only bit1.3) If bit1 = 1, A = 02H; else A = 00H.4) CJNE A, #02H, READ branches back unless A equals 02H.5) When A = 02H, the loop exits and MOV P3, #0FFH executes.


Verification / Alternative check:
Had the intention been to test bit2, the mask should be #04H and the comparison should be #04H accordingly.


Why Other Options Are Wrong:

  • True: incorrect because the mask checks bit1, not bit2.


Common Pitfalls:
Off-by-one errors in bit masks (confusing bit numbering with 1-based human counting). Always map bit n to 1 << n: bit2 corresponds to 04H.


Final Answer:
False

More Questions from The 8051 Microcontroller

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion