How to detect if a I/O Pin is high?

Discussion in 'Programmer's Corner' started by MLCC, Aug 18, 2008.

  1. MLCC

    Thread Starter New Member

    Aug 15, 2008
    8
    0
    Hello again!

    I have a sensor and I'm trying to figure out how my 8051 microcontroller can detect it going high on one of the pins. I am using Assembly Language. Any suggestions and examples?
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,144
    1,791
    There are several methods available to you. They all revolve around reading a port register into the accumulator or the carry flag and testing the the contents of the accumulator or the carry flag to see if you have a '1' or a '0'

    For example, suppose you are interested in bit 3 of Port 2, you might use something like the following:
    Code ( (Unknown Language)):
    1.  
    2.     MOV  A,P2      ; Move P2 (SFR @ 0xA0) to ACC (SFR @ 0xE0)
    3.     ANL  A,#(1<<3) ; Mask off all bits except bit 3
    4.     JNZ  ITS_A_ONE
    5. ITS_A_ZERO:
    6.     ...
    7.     ...
    8. ITS_A_ONE:
    9.  
    Now since the bits in the Special Function Registers whose addresses are divisible by 8 are directly addressable you can also do the following
    Code ( (Unknown Language)):
    1.  
    2.     JB  P2.3,ITS_A_ONE
    3. ITS_A_ZERO:
    4.     ...
    5.     ...
    6. ITS_A_ONE:
    7.  
    You can also get the reverse logic with
    Code ( (Unknown Language)):
    1.  
    2.     JNB  P2.3,ITS_A_ZERO
    3. ITS_A_ONE:
    4.     ...
    5.     ...
    6. ITS_A_ZERO:
    7.  
    You can also use the carry flag as a 1-bit accumulator
    Code ( (Unknown Language)):
    1.  
    2.     MOV  C,P2.3
    3.     JC   ITS_A_ONE
    4. ITS_A_ZERO:
    5.     ...
    6.     ...
    7. ITS_A_ONE:
    8.  
    If your assembler does not support the dot notation for a bit in a register you can use a macro to define a mnemonic symbol which evaluates to 0xA3 which is the direct bit address of Port 2 Bit 3.

    Get the idea?
     
Loading...