8051 I/O BIT MANIPULATION PROGRAMMING

SECTION 4.2: I/O BIT MANIPULATION PROGRAMMING
In this section we further examine 8051 I/O instructions. We pay special attention to I/O bit manipulation since it is a powerful and widely used feature of the 8051 family.
I/O ports and bit-addressability
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits. A powerful feature of 8051 I/O ports is their capability to access individual bits of the port without altering the rest of the bits in that port. Of the four 8051 ports, we can access either the entire 8 bits or any single bit without altering the rest. When accessing a port in single-bit manner, we use the syntax “SETB X. Y” where X is the port number 0, 1,2. or 3, and Y is the desired bit number from 0 to 7 for data bits DO to D7. For example, “SETB PI. 5″ sets high bit 5 of port 1. Remember that DO is the LSB and D7 is the MSB. For example, the following code toggles bit PI .2 continuously.



Notice that PI.2 is the third bit of PI, since the first bit is Pl.O, the second bit is P1.1, and so on. Table 4-3 shows the bits of the 8051 I/O ports. See Example 4-2 for an example of bit manipulation of I/O bits. Notice in Example 4-2 that unused portions of ports 1 and 2 are undisturbed. This single-bit addressability of I/O ports is one of most powerful features of the 8051 microcontroller and is among the reasons that many designers choose the 8051 over other microcontrollers. We will see the use of the bit-addressability of I/O ports in future chapters.
Table 4-3: Sinele-Bit Addressability of Ports






Example 4-2










Table 4-4: Single-Bit Instructions
Table 4-4 lists the single-bit instructions for the 8051. We will see the use of these instructions throughout future chapters.
Checking an input bit
The JNB (jump if no bit) and JB (jump if bit = 1) instructions are also widely used single-bit operations. They allow you to monitor a bit and make a decision depending on whether it is 0 or 1. Instructions JNB and JB can be used for any bits of I/O ports 0, 1,2, and 3, since all ports are bit-addressable. However, most of port 3 is used for interrupts and serial communication signals, and typically is not used for any I/O, either single-bit or byte-wise. This is discussed in Chapters 10 and 11. Table 4-5 shows a list of instructions for reading the ports.
Example 4-3

In this program, instruction “JNB PI . 2 , AGAIN” (JNB means jump if no bit) stays in the loop as long as P1.2 is low. When P1.2 becomes high, it gets out of the loop, writes the value 45H to port 0, and creates an H-to-L pulse by the sequence of instructions SETB and CLR.


Table 4-5: Instructions For Reading an Input Port

Example 4-4




Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it means that the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-to-low pulse to port PI.5 to turn on a buzzer.
Example 4-5
A switch is connected to pin PI .7. Write a program to check the status of SW and perform the following:
  1. If SW=0, send letter ‘N’ to P2.
  2. If SW=1, send letter ‘Y’ to P2.






Reading a single bit into the carry flag
We can also use the carry flag to save or examine the status of a single bit of the port. To do that, we use the instruction “MOV C, Px. y” as shown in the next two examples.
Example 4-6
A switch is connected to pin P1.7. Write a program to check the status of the switch and perform the following:
  1. If switch = 0, send letter ‘N’ to P2.
  2. If switch = 1, send letter ‘Y’ to P2.
    Use the carry flag to check the switch status. This is a repeat of the last example. Solution:


Example 4-7
A switch is connected to pin PI .0 and an LED to pin P2.7. Write a program to get the status of the switch and send it to the LED.

Notice in Examples 4-6 and 4-7 how the carry flag is used to get a bit of data from the port.

Reading input pins vs. port latch
In reading a port, some instructions read the status of port pins while others read the status of an internal port latch. Therefore, when reading ports there are two possibilities:
  1. Read the status of the input pin.
  2. Read the internal latch of the output port.
We must make a distinction between these two categories of instructions since confusion between them is a major source of errors in 8051 programming, especially where external hardware is concerned. We discuss these instructions briefly. However, readers must study and understand the material on this topic and on the internal working of ports that is given in Appendix C.2.
Instructions for reading input ports
As stated earlier, to make any bit of any 8051 port an input port, we must write 1 (logic high) to that bit. After we configure the port bits as input, we can use only certain instructions in order to get the external data present at the pins into the CPU. Table 4-6 shows the list of such instructions.
Reading latch for output port
Some instructions read the contents of an internal port latch instead of reading the status of an external pin. Table 4-6 provides a list of these instructions. For example, look at the “ANL PI, A” instruction. The sequence of actions taken when such an instruction is executed is as follows.
  1. The instruction reads the internal latch of the port and brings that data into the
    CPU.
  1. This data is ANDed with the contents of register A.
  2. The result is rewritten back to the port latch.
  3. The port pin data is changed and now has the same value as the port latch.
Table 4-6: Instructions Reading a Latch (Read-Modify-Write)





From the above discussion, we conclude that the instructions that read the port latch normally read a value, perform an operation (and possibly change it), then rewrite it back to the port latch. This is often called “Read-Modify-Write”.
Read-modify-write feature
The ports in the 8051 can be accessed by the read-modify-write technique. This feature saves many lines of code by combining in a single instruction all three actions of (1) reading the port, (2) modifying its value, and (3) writing to the port. The following code first places 01010101 (binary) into port 1. Next, the instruction “XLR Pi, #OFFH” performs an XOR logic operation on PI with 1111 1111 (binary), and then writes the result back into PI.


Notice that the XOR of 55H and FFH gives AAH. Likewise, the XOR of AAH and FFH gives 55H. Logic instructions are discussed in Chapter 6.



caution
We strongly recommend that you study Section C.2 (Appendix C) if you are connecting any external hardware to your 8051 system. Failure to use the right instruction or the right connection to port pins can damage the ports of your 8051 system.
SUMMARY
This chapter focused on the I/O ports of the 8051. The four ports of the 8051, PO, PI, P2, and P3, each use 8 pins, making them 8-bit ports. These ports can be used for input or output. Port 0 can be used for either address or data. Port 3 can be used to provide interrupt and serial communication signals. Then I/O instructions of the 8051 were explained, and numerous examples were given. We also showed the bit-addressability of the 8051 ports.

Next post:

Previous post: