Game Development Reference
In-Depth Information
To read a single potentiometer we need to ensure that the multiplexer is
scanning the appropriate inputs by setting the digital output using port
Base + 3. Because the A/D converter is much slower than most PCs, we
need to ensure that the port is correctly set before continuing. We do this
using a very simple delay routine. The variable writedelay is user settable
to suit a particular machine.
void CADConvert::SetMultiplexer(short value)
value &= 0xFF;
_outp(baseaddress + 3, value);
int delay = writedelay;
while(delay) delay-;
Having set the appropriate input scanner, we can get the value of a
channel by first writing the channel we require to the output port Base
+ 2. Then we wait to see if the channel is correctly set by scanning the
A/D status at the input port Base + 2. The highest bit of this input
indicates that the card is ready for a conversion to take place. Since
the card could be offline, we check that this is not the case using a
simple counter. If a count is exceeded then it is presumed that a
hardware error has occurred.
If all is well, then we start a conversion using the output register
(Base + 1). Then we wait again for conversion to complete with the
same precaution in case the card goes offline. When the conversion is
complete, the two input registers (Base and Base + 1) contain the full
12-bit A/D value. To form the 12-bit value, we have to combine the two
values. The most significant 8 bits are in register Base + 1; if we shift
these four to the left and bitwise and them with the contents of register
Base shifted four right because the high order bits are stored in bits
4-7, then we will have the full 12 bits in a single short variable
advalue .
Table 12.2 Analogue to digital input registers
A/D 9
A/D 10
A/D 11
A/D 12
Base + 1
A/D 8
A/D 7
A/D 6
A/D 5
A/D 4
A/D 3
A/D 2
A/D 1
Search Nedrilad ::

Custom Search