SECTION 6.4: ROTATE INSTRUCTION AND DATA SERIALIZATION
In many applications there is a need to perform a bitwise rotation of an operand. In the 8051 the rotation instructions RL, RR, RLC, and RRC are designed specifically for that purpose. They allow a program to rotate the accumulator right or left. We explore the rotate instructions next since they are widely used in many different applications. In the 8051, to rotate a byte the operand must be in register A. There are two type of rotations. One is a simple rotation of the bits of A, and the other is a rotation through the carry. Each is explained below.
Rotating the bits of A right or left
RR A ;rotate right A

In rotate right, the 8 bits of the accumulator are rotated right one bit, and bit DO exits from the least significant bit and enters into D7 (most significant bit). See the code and diagram.


In rotate left, the 8 bits of the accumulator are rotated left one bit, and bit D7 exits from the MSB (most significant bit) and enters into DO (least significant bit). See the code and diagram.

Rotating through the carry
There are two more rotate instructions in the 8051. They involve the carry flag. Each is shown next.


In RRC A, as bits are rotated from left to right, they exit the LSB to the carry flag, and the carry flag enters the MSB. In other words, in RRC A the LSB is moved to CY and CY is moved to the MSB. In reality, the carry flag acts as if it is part of register A, making it a 9-bit register.


In RLC A, as bits are shifted from right to left they exit the MSB and enter the carry flag, and the carry flag enters the LSB. In other words, in RCL the MSB is moved to CY (carry flag) and CY is moved to the LSB. See the following code and diagram.

Serializing data
Serializing data is a way of sending a byte of data one bit at a time through a single pin of microcontroller. There are two ways to transfer a byte of data serially:
- Using the serial port. In using the serial port, programmers have very limited
control over the sequence of data transfer. The details of serial port data trans
fer are discussed in Chapter 10.
- The second method of serializing data is to transfer data one bit at a time and
control the sequence of data and spaces in between them. In many new generations of devices such as LCD, ADC, and ROM, the serial versions of these devices are becoming popular since they take less space on a printed circuit board. We discuss this important topic next.
Serializing a byte of data
Serializing data is one of the most widely used applications of the rotate instruction. We showed in Chapter 5 how the CY flag status can be moved to any pin of ports PO - P3. Using that concept and the rotate instruction, we transfer a byte of data serially (one bit at a time). Repeating the following sequence 8 times will transfer an entire byte, as shown in Example 6-28.

Example 6-29 shows how to bring in a byte of data serially one bit at a time. We will see how to use these concepts in Chapter 13 for a serial ADC chip.
Example 6-28
Write a program to transfer value 41H serially (one bit at a time) via pin P2.1. Put two highs at the start and end of the data. Send the byte LSB first.
Solution:

Example 6-29
Write a program to bring in a byte of data serially one bit at a time via pin P2.7 and save it in register R2. The byte comes in with the LSB first.

Single-bit operations with CY
Aside from the fact that the carry flag (CY) is altered by arithmetic and logic instructions, in the 8051 there are also several instructions by which the CY flag can be manipulated directly. These instructions are listed in Table 6-4.
Of the instructions in Table 6-4, we have shown the use of JNC, CLR, and SETS in many examples in the last few chapters. The next few examples give simple applications of the instructions in Table 6-4, including some dealing with the logic operations AND and OR.
Example 6-30
Write a program to save the status of bits P1.2 and PI.3 on RAM bit locations 6 and 7, respectively.
Solution:
MOV C,P1.2 ;save status of PI.2 on CY
MOV 06 ,C ,-save carry in RAM bit location 06
MOV C,P1.3 ;save status of PI.3 on CY
MOV 07,C /save carry in RAM bit location 07
Table 6-4: Carry Bit-Related Instructions

Example 6-31
Assume that bit P2.2 is used to control an outdoor light and bit P2.5 a light inside a building. Show how to rum on the outside light and turn off the inside one.
Solution:
SETB C ;CY = 1
ORL C,P2.2 ;CY = P2.2 ORed with CY
MOV P2.2,C /turn it "on" if not already "on"
CLR C ;CY = 0
ANL C,P2.5 ;CY = P2.5 ANDed-with CY
MOV P2.5,C /turn it off if not already off
Example 6-32
Write a program that finds the number of Is in a given byte. Solution:

SWAP A
Another useful instruction is the SWAP instruction. It works only on the accumulator (A). It swaps the lower nibble and the higher nibble. In other words,
the lower 4 bits are put into the higher 4 bits, and the higher 4 bits are put into the lower 4 bits. See the diagrams below and Example 6-33.

Example 6-33
- Find the contents of register A in the following code.
- In the absence of a SWAP instruction, how would you exchange the nibbles?
Write a simple program to show the process.
Solution: