
Site Search
SECTION II - BIT MANIPULATION / INPUT OUTPUT PROGRAMMING IN 8051:
SECTION II - BIT MANIPULATION / INPUT OUTPUT PROGRAMMING IN 8051:
Bit Manipulation is a powerful feature of an 8051.
Ways of Accessing the Entire 8 bits data:
Example # 1:
Lets examine an example in which the entire 8 bits of Port 1 data are accessed.
BACK: MOV A,#55H |
---|
MOV P1,A |
ACALL DELAY |
MOV A,#0AAH |
MOV P1,A |
ACALL DELAY |
SJMP BACK |
In the example above, the code toggles the every bit of Port 1 continuously. This code can be written in a more efficient manner by accessing the port directly without going through the accumulator.
BACK: MOV P1,#55H |
---|
ACALL DELAY |
MOV P1,#0AAH |
ACALL DELAY |
SJMP BACK |
Read-Modify-Write Feature of an 8051:
The 8051 ports can be accessed by the read-modify-write technique. This feature saves many lines of code by combining in a single instruction. The first action is reading the port, the second is modifying the port, and the third is writing to the port.
Example # 2:
MOV P1,#55H | ;Load Port 1 with 55H or 01010101 in Binary |
---|---|
AGAIN: XRL P1,#0FFH | ;EX-OR Port 1 with 1111 1111 or |
ACALL DELAY | ;Wait |
SJMP AGAIN | ;Repeat the action again |
The code above first places 01010101 [binary] into Port 1. Then the instruction "XRL P1,#0FFH" performs an XOR logic operation on Port 1 with 1111 1111 [Binary], and then writes the result back into Port 1. XOR of 55H and FFH gives AAH, and the XOR of AAH and FFH gives 55H.
Addressability of Ports as a Single Bit:
This feature is used when it is need to access only 1 or 2 bits of the port instead of the entire 8 bits. This is a power ful feature of 8051, that the capability to access the single bit and individual bits of 8051 I/O ports without altering the rest of the bits in that port.
Example # 3:
Lets examine the code in which the code toggles the Port bit P1.3 continuously.
BACK: CPL P1.3 | ;Compplement Port 1 bit 3 i.e P1.3 |
---|---|
ACALL DELAY | ;wait |
SJMP BACK | ;go back to instruction first instruction again |
Another way of writing the above program is:
AGAIN: SETB P1.3 | ;Set the Bit 3 of Port 1 only to high i.e P1.3 |
---|---|
ACALL DELAY | ;Wait |
CLR P1.3 | ;Clear the Bit 3 of Port 1 to Low i.e P1.3 |
ACALL DELAY | ;Wait |
SJMP AGAIN | ;Repeat |
Addressability of Ports as a Single-Bit:
PORT 0 [P0] | PORT 1 [P1] | PORT 2 [P2] | PORT 3 [P3] | PORT BIT |
---|---|---|---|---|
P0.0 | P1.0 | P2.0 | P3.0 | D0 |
P0.1 | P1.1 | P2.1 | P3.1 | D1 |
P0.2 | P1.2 | P2.2 | P3.2 | D2 |
P0.3 | P1.3 | P2.3 | P3.3 | D3 |
P0.4 | P1.4 | P2.4 | P3.4 | D4 |
P0.5 | P1.5 | P2.5 | P3.5 | D5 |
P0.6 | P1.6 | P2.6 | P3.6 | D6 |
P0.7 | P1.7 | P2.7 | P3.7 | D7 |
Example # 4:
Lets examine the code to perform the following tasks: (1) Keep monitoring the Port 1 bit 3, P1.3 until it becomes high. (2) As P1.3 becomes high, Write 55H value to P1. (3) Send a High-to-Low pulse to P2.0.
SETB P1.3 | ;Make Port 1 bit 3 i.e P1.3 an input |
---|---|
MOV A,#55H | ;Load accumulator with value 55H |
AGAIN: JNB P1.3,AGAIN | ;Get out of the loop when P1.3=1, means high |
MOV P1,A | ;Copy value in accumulator i.e 55H to Port 1 |
SETB P2.0 | ;Make/Set P2.0 High |
CLR P2.0 | ;Make/Clear P2.0 low i.e High-to-Low |
The instruction "JNB P1.3,AGAIN", mean jumps to the target 'AGAIN' if no bit is set or if bit P1.3 is low, and stays in the loop as long as P1.3 becomes low. When P1.3 becomes high, it gets out of the loop, writes 55H to Port 1, and creates a High-to-Low pulse by the sequence of instructions SETB and CLR.