Real Time Devices AD1200 User Manual page 47

Table of Contents

Advertisement

To set a single bit in a port, OR the curent value of the port with tle value b, where b = 2h,.
Example: Set bit 3 in a port. Read in the current value of the port, OR it with 8 (8 = 23), and then
write the resulting value to the port. In Pascal, ttris is programmed as:
V :: Poqt [PortAddress] ,.
V : = V O R 8 ;
P o r t [ P o r t A d d r e s s ] : = 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 culrent 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: Clearbits2,4,and6inaport.Readinthecunentvalueoftheport,ANDitwithlTl
(171 = 255 - 22 - 2n - ?i), and then write the resulting value to tre port. In C, this is programmed
as:
v = inportb(pbrt_address)
;
v = v & 1 7 1 ;
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 bis 3, 5, and 7 in a port. Read in the current value of the port, OR it with 168
(168 = 23 + 25 + 27), and then write the resulting value back o the port. In assembly language, this
is programmed as:
mov dx, PortAddress
i n a I , d x
o r a l ,
1 6 8
out dx, a1
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 bis 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 C L 9 9 ;
]"io]'Jotf,o..-"oo.."",
v),.
A final note: Don't be intimidated by ttre 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 !o read in the port, add32 (25) to that value, and then write ttre resulting value back to the port.
This works fine if bit 5 is not already set. But, what happens when bit 5 rt already set? Birs 0 to 4 will be unaffected
and we can't say for sure what happens to bits 6 and7,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 ttre programming steps for the 1200 board
functions.
4-10

Advertisement

Table of Contents
loading

This manual is also suitable for:

Ada1200

Table of Contents