Setting, Clearing, and Moving Bits (SETB, CLR, CPL, MOV)

16.12 Setting, Clearing, and Moving Bits (SETB, CLR, CPL, MOV)

One very powerful feature of the 8052 architecture is its ability to manipulate in- dividual bits on a bit-by-bit basis. As mentioned earlier in this document, there are 128 numbered bits (00H through 7FH) that may be used by the user’s pro- gram as bit variables. Additionally, bits 80H through FFH allow access to SFRs that are divisible by 8 on a bit-by-bit basis. The two basic instructions to manipu- late bits are SETB and CLR while a third instruction, CPL, is also often used.

The SETB instruction will set the specified bit, which means the bit will then have a value of “1”, or “on”. For example:

SETB 20h

;Sets user bit 20h (sets bit 0 of IRAM address

 

;24h to 1)

 

SETB 80h

;Sets bit 0 of SFR 80h

(P0) to 1

SETB P0.0

;Exactly the same as the previous instruction

SETB C

;Sets the carry bit to

1

SETB TR1

;Sets the TR1 bit to 1

(turns on timer 1)

As illustrated by these instructions, SETB can be used in a variety of circum- stances.

The first example, SETB 20h, sets user bit 20H. This corresponds to a user-de- fined bit because all bits between 00H and 7FH are user bits. It is clear that bit 20H is the 32nd user-defined bit because these 128 user bits reside in internal RAM at the addresses of 20H through 2FH. Each byte of Internal RAM by definition holds 8 individual bits, so bit 20H would be the lowest bit of Internal RAM 24H.

Note:

It is very important to understand that bit memory is a part of internal RAM. In the case of SETB 20h, we concluded that bit 20H is actually the low bit of internal RAM address 24H. That is because bits 00H-07Hare internal RAM address 20H, bits 08H-0FHare internal RAM address 21H, bits 10H-17Hare internal RAM ad- dress 22H, bits 18H-1FHare internal RAM address 23H, and bits 20H-27Hare internal RAM address 24H.

The second example, SETB 80h, is similar to SETB 20h. Of course, SETB 80h sets bit 80H. However, remember that bits 80H-FFHcorrespond to individual bits of SFRs, not Internal RAM. Thus, SETB 80h actually sets bit 0 of SFR 80H, which is the P0 SFR.

The next instruction, SETB P0.0, is identical to SETB 80h. The only difference is that the bit is now being referenced by name rather than number. This makes the assembly language code more readable. The assembler will automatically convert P0.0 to 80H when the program is assembled.

The next example, SETB C, is a special case. This instruction sets the carry bit, which is a very important bit used for many purposes. It is also special in that there is an opcode that means SETB C. Although other SETB instructions require two bytes of program memory, the SETB C instruction only requires one.

8052 Assembly Language

16-13

 

Page 207
Image 207
Texas Instruments MSC1210 manual Setting, Clearing, and Moving Bits SETB, CLR, CPL, MOV