Texas Instruments MSC1210 User Manual

Page 208

Advertising
background image

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

16-14

Finally, the SETB TR1 example shows a typical use of SETB to set an individu-
al bit of an SFR. In this case, TR1 is TCON.6 (bit 6 of TCON SFR, SFR address
88

H

). Due to TCON’s SFR address being 88

H

, it is divisible by 8 and, thus, ad-

dressable on a bit-by-bit basis.

The CLR instruction functions in the same manner, but clears the specified bit.
For example:

CLR 20h

;Clears user bit 20h to 0

CLR P0.0

;Sets bit 0 of P0 to 0

CLR TR1

;Clears TR1 bit to 0 (stops timer 1)

These two instructions, CLR and SETB, are the two fundamental instructions
used to manipulate individual bits in 8052 assembly language.

A third bit instruction, CPL, complements the value of the given bit. The instruc-
tion syntax is exactly the same as SETB and CLR, but CPL flips (comple-
ments) the bit. If the bit is cleared, CPL sets it; likewise, if the bit is set, CPL
clears it.

Note:

An additional instruction, CLR A, exists that is used to clear the contents of
the accumulator. This is the only CLR instruction that clears an entire SFR,
rather than just a single bit. The CLR A instruction is the equivalent of MOV
A,#00h. The advantage of using CLR A is that it requires only one byte of pro-
gram memory, whereas the MOV A,#00h solution requires two bytes. An
additional instruction, CPL A, also exists. This instruction flips each bit in the
accumulator. Therefore, if the accumulator holds 255 (11111111 binary), it
will hold 0 (00000000 binary) after the CPL A instruction is executed.

Finally, the MOV instruction can be used to move bit values between any given
bit—user or SFR bits—and the carry bit. The instructions MOV C,bit and
MOV bit,C allow these bit movements to occur. They function like the MOV in-
struction described earlier, moving the value of the second bit to the value of
the first bit.

Consider the following examples:

MOV C,P0.0

;Move the value of the P0.0 line to the carry bit

MOV C,30h

:Move the value of user bit 30h to the carry bit

MOV 25h,C

;Move the carry bit to user bit 25h

These combination of MOV instructions that allow bits to be moved through
the carry flag allow for more advanced bit operations, without the need for
workarounds that would be required to move bit values if it were not for these
MOV instructions.

Note:

The MOV instruction, when used with bits, can only move bit values to and
from the carry bit. There is no instruction that allows you to copy directly from
one bit to the other bit with neither bit being the carry bit. Thus, it is often nec-
essary to use the carry bit as a temporary bit register to move a bit value from
one user bit to another user bit.

Advertising