DM5210 analog input module 4-9 RTD Embedded Technologies, Inc.
To set a single bit in a port, OR the current value of the port with the value b, where b = 2
bit
.
Example: Set bit 3 in a port. Read in the current value of the port, OR it with 8 (8 = 2
3
), and then
write the resulting value to the port. In Pascal, this is programmed as:
V := Port[PortAddress];
V := V OR 8;
Port[PortAddress] := V;
Setting or clearing more than one bit at a time is accomplished just as easily. To clear multiple bits in a port,
AND the current value of the port with the value b, where b = 255 - (the sum of the values of the bits to be cleared).
Note that the bits do not have to be consecutive.
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:
v = inportb(port_address);
v = v & 171;
outportb(port_address, v);
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:
mov dx, PortAddress
in al, dx
or al, 168
out dx, al
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:
v = inportb(port_address);
v = v & 199;
v = v | 40;
outportb(port_address, v);
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.
Now that you know how to clear and set bits, we are ready to look at the programming steps for the 210/5210
module functions.
Artisan Technology Group - Quality Instrumentation ... Guaranteed | (888) 88-SOURCE | www.artisantg.com