dileep's yard
 
HOME MYSELF E&C TUTORIAL LINKS
 
 
 
 
 
 
 
 
 


ii) Game port

Welcome once again to our little lesson on port interfacing. This time it is the game port or the joystick port. In this tutorial I will try to provide you the bare minimum information that you might need to experiment with your game port.

As usual let us see first of all the address range allocated to the game port. On your PC the game port is most likely to be having the addresses 0x200 - 0x207, where 0x means it's hexadecimal. That's 512 - 519 in decimal. The port is usually a 15 pin D-connector. The pin details are as follows

PINDESCRIPTION
1+5V
8
9
15
4GND
5
12
2BA1 Digital Inputs
7BA2
10BB1
14BB2
3AX Analog Inputs
6AY
11BX
13BY

Now we can see that aside from the power supply and ground pins the port has only two types of inputs: digital and analog. This port is a specialized one. So in order to use it for our purposes, we have to see first of all, how the joystick interacts with the port.

Let us call the two joysticks to be connected as A and B. Each joystick is intended to have two buttons apart from the "stick". Hence the two digital inputs per joystick. Here BA1 and BA2 belong to joystick A and BB1 and BB2 belong to B. These inputs are ordinary TTL types. To indicate a HIGH level all you need is to connect the 5V supply preferably through a 1k resistor the pins.
Now for the analog part. When a joystick is connected there must be some way of telling the computer the current position of the "stick" so that your flight sim can respond accordingly. Actually this is done in a very simple way. Inside the port, for each analog input there is a monostable multivibrator connected. Between each monoshot and the input pin a resistor(2.2k) is connected and a capacitor (0.01uF) is connected across the pin and the ground. This forms the timing circuit within the port. Now in the joystick potentiometers are used to indicate the position along each axis. The X axis pot. of A joystick AX is connected to pin 3 on the port and Y axis pot. to its corresponding pin. Hence what we have here is a measurement of resistance to measure the position. The other end of each pot is connected to the 5V supply.
To read a value, first we have to enable the monoshot. This is done by a write to port 0x201. The monostable puts its output HIGH. Now the capacitor starts charging through the pot and the resistor. When the voltage across it rises to 3.3V the monoshot changes state and its output goes LOW. So all we need to know is the time for which the output stayed HIGH to know the resistance of each pot and hence the position.

Now to find the time taken.If 't' is the time taken for the monoshot to change state (HI to LO) and Rp the potentiometer resistance, then
    t=k x R x C
wher k is a constant dependent upon the monoshot, R=Rp+2.2k and C=0.01uF
So approximately t=(0.01xRp + 22) microseconds and
Rp=(t-22)/0.01 with Rp in ohms.
I found the equation in Linux I/O port programming mini-HOWTO to be:   Rp = (t - 24.2) / 0.011.

So to find the position of the axes you have to poll the four inputs continuously. I mentioned a port 0x201 before. This is the only address we have to be concerned with and is that of the status port. As said we make an empty write operation to this port to start a conversion. After the monoshot goes low we read from this port. So in effect we need to continuously poll this port. The bit values are:

76543210
BB2 BB1 BA2 BA1 BY BXAY AX

The bits 0-3 belonging to the analog pins give the status of the multivibrator output with 1 indicating HIGH. For digital values also a status of 1 indicates a HIGH.
 


 

 
 
 
  Tested on IE 6.0 , Netscape 4.7, Mozilla 1.0 Best on 800x600
  mail me