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.0P1.0P2.0P3.0D0
P0.1P1.1P2.1P3.1D1
P0.2P1.2P2.2P3.2D2
P0.3P1.3P2.3P3.3D3
P0.4P1.4P2.4P3.4D4
P0.5P1.5P2.5P3.5D5
P0.6P1.6P2.6P3.6D6
P0.7P1.7P2.7P3.7D7

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.

More From Iamtechnical.com

Advertisement: