Texas Instruments MSC1210 User Manual

Page 207

Advertising
background image

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

16-13

8052 Assembly Language

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 (00

H

through 7F

H

) that may be used by the user’s pro-

gram as bit variables. Additionally, bits 80

H

through FF

H

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 20

H

. This corresponds to a user-de-

fined bit because all bits between 00

H

and 7F

H

are user bits. It is clear that bit 20

H

is the 32nd user-defined bit because these 128 user bits reside in internal RAM
at the addresses of 20

H

through 2F

H

. Each byte of Internal RAM by definition

holds 8 individual bits, so bit 20

H

would be the lowest bit of Internal RAM 24

H

.

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 20

H

is actually the low bit of internal

RAM address 24

H

. That is because bits 00

H

-07

H

are internal RAM address 20

H

,

bits 08

H

-0F

H

are internal RAM address 21

H

, bits 10

H

-17

H

are internal RAM ad-

dress 22

H

, bits 18

H

-1F

H

are internal RAM address 23

H

, and bits 20

H

-27

H

are

internal RAM address 24

H

.

The second example, SETB 80h, is similar to SETB 20h. Of course, SETB 80h
sets bit 80

H

. However, remember that bits 80

H

-FF

H

correspond to individual

bits of SFRs, not Internal RAM. Thus, SETB 80h actually sets bit 0 of SFR 80

H

,

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 80

H

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.

Advertising