DM5816/DM6816
RTD Embedded Technologies, Inc.
29
Example:
Clear bits 2, 4, and 6 in a port. Read in the current value of the port, AND it with 171
(171 = 255 - 2
2
- 2
4
- 2
6
), and then write the resulting value to the port. In C, this is programmed
as:
To
set
multiple bits in a port, OR the current value of the port with the value b, where b = the sum of
the individual bits to be set. Note that the bits to be set do not have to be consecutive.
Example:
Set bits 3, 5, and 7 in a port. Read in the current value of the port, OR it with 168
(168 = 2
3
+ 2
5
+ 2
7
), and then write the resulting value back to the port. In assembly language,
this is programmed as:
Often, assigning a range of bits is a mixture of setting and clearing operations. You can set or clear each
bit individually or use a faster method of first clearing all the bits in the range then setting only those bits
that must be set using the method shown above for setting multiple bits in a port. The following example
shows how this two- step operation is done.
Example:
Assign bits 3, 4, and 5 in a port to 101 (bits 3 and 5 set, bit 4 cleared). First,
read in the port and clear bits 3, 4, and 5 by ANDing them with 199. Then set bits 3 and
5 by ORing them with 40, and finally write the resulting value back to the port. In C, this
is programmed as:
A final note:
Don’t be intimidated by the binary operators AND and OR and try to use operators for which
you have a better intuition. For instance, if you are tempted to use addition and subtraction to set and
clear bits in place of the methods shown above, DON’T! Addition and subtraction may seem logical, but
they
will not work
if you try to clear a bit that is already clear or set a bit that is already set. For example,
you might think that to set bit 5 of a port, you simply need to read in the port, add 32 (2
5
) to that value,
and then write the resulting value back to the port. This works fine if bit 5 is not already set. But, what
happens when bit 5
is
already set? Bits 0 to 4 will be unaffected and we can’t say for sure what happens
to bits 6 and 7, but we can say for sure that bit 5 ends up cleared instead of being set. A similar problem
happens when you use subtraction to clear a bit in place of the method shown above.
Summary of Contents for DM5816
Page 1: ...BDM 610010027 Rev A DM5816 DM6816 Triggered User s Manual...
Page 7: ...DM5816 DM6816 RTD Embedded Technologies Inc 7...
Page 15: ...DM5816 DM6816 RTD Embedded Technologies Inc 15 Fig 1 7 Base Address Switch S1...
Page 22: ...DM5816 DM6816 RTD Embedded Technologies Inc 22...
Page 42: ...DM5816 DM6816 RTD Embedded Technologies Inc 42 P3 Connector P6 Connector...