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

Thread Starter

MLCC

Joined Aug 15, 2008
8
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?
 

Papabravo

Joined Feb 24, 2006
21,228
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:
Rich (BB code):
    MOV  A,P2      ; Move P2 (SFR @ 0xA0) to ACC (SFR @ 0xE0)
    ANL  A,#(1<<3) ; Mask off all bits except bit 3
    JNZ  ITS_A_ONE
ITS_A_ZERO:
    ...
    ...
ITS_A_ONE:
Now since the bits in the Special Function Registers whose addresses are divisible by 8 are directly addressable you can also do the following
Rich (BB code):
    JB  P2.3,ITS_A_ONE
ITS_A_ZERO:
    ...
    ...
ITS_A_ONE:
You can also get the reverse logic with
Rich (BB code):
    JNB  P2.3,ITS_A_ZERO
ITS_A_ONE:
    ...
    ...
ITS_A_ZERO:
You can also use the carry flag as a 1-bit accumulator
Rich (BB code):
    MOV  C,P2.3
    JC   ITS_A_ONE
ITS_A_ZERO:
    ...
    ...
ITS_A_ONE:
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?
 
Top