BIT ADDRESSES FOR I/O AND RAM

SECTION 5.3: BIT ADDRESSES FOR I/O AND RAM
Many microprocessors such as the 386 or Pentium allow programs to access registers and I/O ports in byte size only. In other words, if you need to check a single bit of an I/O port, you must read the entire byte first and then manipulate the whole byte with some logic instructions to get hold of the desired single bit. This is not the case with the 8051. Indeed, one of the most important features of the 8051 is the ability to access the registers, RAM, and I/O ports in bits instead of bytes. This is a very unique and powerful feature for a microprocessor made in the early 1980s. In this section we show address assignment of bits of I/O, register, and memory, in addition to ways of programming them.
Bit-addressable RAM
Of the 128-byte internal RAM of the 8051, only 16 bytes are bit-addressable. The rest must be accessed in byte format. The bit-addressable RAM locations are 20H to 2FH. These 16 bytes provide 128 bits of RAM bit-addressability since 16×8= 128. They are addressed as 0 to 127 (in decimal) or 00 to 7FH. Therefore, the bit addresses 0 to 7 are for the first byte of internal RAM location 20H, and 8 to OFH are the bit addresses of the second byte of RAM location 21H, and so on. The last byte of 2FH has bit addresses of 78H to 7FH. See Figure 5-1 and Example 5-11. Note that internal RAM locations 20 – 2FH are both byte-addressable and bit-addressable.
Example 5-11











In order to avoid confusion regarding the addresses 00 – 7FH, the following two points must be noted.
  1. The 128 bytes of RAM
    have the byte addresses
    of 00 – 7FH and can be
    accessed in byte size
    using various address
    ing modes such as
    direct and register-indi
    rect, as we have seen in
    this chapter and previ
    ous chapters. These
    128 bytes are accessed
    using byte-type instruc
    tions.
  2. The 16 bytes of RAM
    locations 20 – 2FH also
    have bit addresses of 00
    - 7FH since 16 x 8 =
    128(00-7FH). In order
    to access these 128 bits
    of RAM locations and
    other bit-addressable
    space of 8051 individu
    ally, we can use only
    the single-bit instruc
    tions such as SETB.
    Figure 5-1. 16 Bytes of Internal RAM.
    Table 5-2 provides a list
    Note: They are both bit- and byte-accessible.
    of single-bit instruc
    tions. Notice that the single-bit instructions use only one addressing mode and
    that is direct addressing mode. In the first two sections of this chapter we
    showed various addressing modes of byte-addressable space of the 8051,
    among them indirect addressing mode. It must be noted that there is no indi
    rect addressing mode for single-bit instructions.
Table 5-2: Single-Bit Instructions








I/O port bit addresses
As we discussed in Chapter 4, the 8051 has four 8-bit I/O ports: PO, PI, P2, and P3. We can access either the entire 8 bits or any single bit without altering the rest. When accessing a port in a 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. See Figure 5-2. For example, “SETB PI. 5″ sets high bit 5 of port 1. Remember that DO is the LSB and D7 is the MSB.


As we mentioned earlier in this chapter, every SFR register is assigned a byte address and ports PO -P3 are part of the SFR. For example, PO is assigned byte address 80H, and PI has address of 90H as shown in Figure 5-2. While all of the SFR registers are byte-addressable some of them are also bit-addressable. The PO – P3 are among this category of SFR registers. From Figure 5-2 we see that the bit addresses for PO are 80H to 87H, and for PI are 90H to 97H, and so on.
Notice that when code such as “SETB PI. 0″ is assembled, it becomes “SETB 9OH” since Pl.O has the RAM address of 90H. Also notice from Figures 5-1 and 5-2 that bit addresses 00 – 7FH belong to RAM byte addresses 20 – 2FH, and bit addresses 80 – F7H belong to SFR of PO, ICON, PI, SCON, P2, etc. The bit addresses for PO – P3 are shown in Table 5-3. and discussed next.
Figure 5-2. SFR RAM Address (Byte and Bit)


Bit memory map
From Figures 5-1 and 5-2 and Table 5-3 once again notice the following facts.
  1. The bit addresses 00 – 7FH are assigned to RAM locations of 20 – 2FH.
  2. The bit addresses 80 – 87H are assigned to the PO port.
  3. The bit addresses 88 – 8FH are assigned to the TCON register.
  4. The bit addresses 90 – 97H are assigned to the PI port.
  5. The bit addresses 98 – 9FH are assigned to the SCON register.
  6. The bit addresses AO – A7H are assigned to the P2 port.
  7. The bit addresses A8 – AFH are assigned to the IE register.
  8. The bit addresses BO – B7H are assigned to the P3 port.
  9. The bit addresses B8 – BFH are assigned to IP.


  1. The bit addresses CO – CFH are not assigned.
  2. The bit addresses DO – D7H are assigned to the PSW register.
  3. The bit addresses D8 – DFH are not assigned.
  4. The bit addresses EO – E7H are assigned to the Accumulator register.
  5. The bit addresses E8 – EFH are not assigned.
  6. The bit addresses FO – F7H are assigned to the B register.
Table 5-3: Bit Addresses for AH Ports

Example 5-12








Registers bit-addressability
While all I/O ports are bit-addressable, that is not the case with registers, as seen from Figure 5-1. Only registers A, B, PSW, IP, IE, ACC, SCON, and TCON are bit-addressable. Of the bit-addressable registers, we will concentrate on the familiar registers A, B, and PSW. The rest will be discussed in future chapters.
Now let’s see how we can use bit-addressability of registers such as A and PSW. As we discussed in Chapter 2, in the PSW register two bits are set aside for the selection of the register banks. See Figure 5-3. Upon RESET, bank 0 is selected. We can select any other banks using the bit-addressability of the PSW as was shown in Chapter 2. The bit addressability of PSW also eliminates the need for instructions such as JOV (Jump if OV=1). See Example 5-14.
Examine the next few examples of bit-addressabilty to gain a better understanding of this important feature of the 8051.


Figure 5-3. Bits of the PSW Register


Example 5-13
Example 5-14
While there are instructions such as JNC and JC to check the carry flag bit (CY), there are no such instructions for the overflow flag bit (OV). How would you write code to check OV?
Solution:
The OV flag is PSW.2 of the PSW register. PSW is a bit-addressable register; therefore, we can use the following instruction to check the OV flag.



Example 5-15
Write a program to see if the RAM location 37H contains an even value. If so, send it to P2. If not, make it even and then send it to P2.


Example 5-16
Assume that bit P2.3 is an input and represents the condition of a door. If it goes high, it means that the door is open. Monitor the bit continuously. Whenever it goes high, send a low-to-high pulse to port PI.5 to turn on a buzzer.

Example 5-17
The status of bits PI.2 and PI.3 of I/O port PI must be saved before they are changed. Write a program to save the status of PI. 2 in bit location 06 and the status of PI. 3 in bit
location 07.



Example 5-18
Write a program to save the status of bit PI.7 on RAM address bit 05. Solution:





Example 5-19
Write a program to get the status of bit pin PI.7 and send it to pin P2.0. Solution:

Using BIT directive
The BIT directive is a widely used directive to assign the bit-addressable I/O and RAM locations. The BIT directive allows a program to assign the I/O or RAM bit at the beginning of the program, making it easier to modify them. Examine the next few examples to see how we use the BIT directive.
Example 5-20
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 low-to-high pulse to port PI.5 to turn on a buzzer.

This is similar to Example 5-16, except the use of BIT directive allows us to assign the OVEN_HOT and BUZZER bit to any port. This way you do not have to search the program for them.


Example 5-21
An LED is connected to pin PI.7. Write a program to toggle the LED forever. Solution:

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


Example 5-23
Assume that RAM bit location 12H holds the status of whether there has been a phone call or not. If it is high, it means there has been a new call since it was checked the last time. Write a program to display “New Messages” on an LCD if bit RAM 12H is high. If it is low, the LCD should say “No New Messages”
Solution:






Using EQU directive
We can also use the EQU directive to assign addresses, as shown in the next few examples. Notice that in Example 5-24 the ports are defined by their names, while in Example 5-25, they are defined by their addresses.
Example 5-24
A switch is connected to pin P1.7. Write a program to check the status of the switch and make the following decision.
  1. If SW = 0, send “NO” to P2.
  2. If SW = 1, send “YES” to P2.


Example 5-25
A switch is connected to pin PI .7. Write a program to check the status of the switch and
make the following decision.
(a) IfSW = 0, send’0′toP2.
(b)IfSW- l,send’l'toP2.
Use EQU to designate the I/O ports.
Solution:


Next post:

Previous post: