computer interfacing tutorial-printer, serial, game, usb port
 

 

Circuit diagram of PC joystick interface

The following circuit diagram is from PCTIM003 document and is Copyright by K. Heidenstrom. This picture is included in this document with permission from K. Heidenstrom.

This circuit block diagram applies to all PC joystick interfaces from multi-IO cards to PC soundcards. In PC soundcards the pins 12 and 15 are used by MIDI interface which has it's own electronics separate from the joystick interface.

The 558 chip used in PC joystick adapter is a quad version of a popular 555 universal timer chip. If you are not already familar how 555 universal timer operates then it is worth to check 555 timer chip datasheet. The 555 timer is used as one-shot in the joystick adapter circuit. Because the pinout of 558 is a little bit hard to find on databooks here is 558 pinout for those whi want to hack their joystick adapter electronics:

Is there any way to get any output from joystick port ?
There is no such function as turning the joystick port pins on/off ever designed to joystick port. Joystick port is designed as input only port.

The joystick port does not need the data you send to it (it is simply discarded). The only thing in data write operation which is needed is the data strobe signal (which comes from the ISA bus when data is ready to be read by card) triggers the monostable multivibrators which are used in converting the resistance of the joystick potentiometer to pulse length.

When the multivibrators are triggered then in normal joystick poer implementations this causes one side-effect: when the monostable multivibrator is triggered then it discharges the timing capacitor by shorting it to ground for very short time. Then the capacitor is discharged by the monostable multivibrator or is charged by the current from the joystick port there is some current flowing through the joystick potentiometer input pin (that current dicreases over time until the capacitor is fully charged to +5V).

So in typical joystick port you can detect some pulses by measuring the current flowing to joystick potentiometer input pin: every time the joystick card is written the current flow into all joystick potentiometer input pins will increase to maximum value (set by external resistor and the internal resistances in the joystick card) and then starts to decrease then time goes on. That's the only way to get anything out of joystick port.

Some digital joystick with their own special digital communication protocols use this joystick port feature to send some data and/or syncronization information from computer to joystick.

Your own routines

To read the joysticks (or your slide pot positions), you must first write a byte to port 201h. It doesn't matter what value you send, as long as you perform an I/O write. This triggers the 558 timer on the game adapter.

 Game port 201h byte:
         _______________________________________________________
        |   7  |   6  |   5  |   4  |   3  |   2  |   1  |   0  |
        | but4 | but3 | but2 | but1 | stk4 | stk3 | stk2 | stk1 |
        |______|______|______|______|______|______|______|______|

The most machine-independent way to sample the game port is to use a timer. Note the time (e.g., read the countdown register in Timer 0, you need pretty fine resolution and this timer performs 65535 counts every 55 ms) just before you trigger the 558. After triggering, sit in a loop reading port 201h and examining bits 0-3. For those bits that have a joystick pot attached, you'll see them sit for a while at 0, then become 1. As each bit flips back to 1, note the time again. When all bit 0-3 have flipped back to 1, you're almost done. Compute elapsed time for each bit, and you end up with a value that is proportional to pot position.

Pots are normally 0-150k variable resistors (0-100k sometimes), and according to the IBM techref, the time is given by Time = 24.2e-6s + 0.011e-6s * R/Ohms. This equation does not accurately represent the real situation, where theare are differences in absolute components values. In reality you have to calibrate the joystick for the application you use. Thse most straighforward way to calibrate the stick for the program isto record the values the joystick gives in extreme positions and in the center position.

Buttons can be read at any time just by reading port 201h and looking at bits 4-7. No triggering is required. Button bits are normally 1; while a button is depressed, its bit will flip to 0.

More information about accurately measuring time using PC can be found at "Timing on the PC family under DOS" document written by Kris Heidenstrom. That documents comes with example source code also. All this is available in on zip packet at ftp://garbo.uwasa.fi/pc/programming/pctim003.zip.

Low-overhead joystick LEFT/RIGHT and UP/DOWN detection idea

If you simply want to detect whether the joystick is left or right of centre, or above or below centre, and don't want the overhead of locking interrupts for several milliseconds at regular intervals, you could use a fast tick interrupt to poll the joystick port. One idea would be using an interrupt at about 500 us and working cyclically through three states. On one interrupt, trigger the joysticks.On the next interrupt, read the monostable states. On the next interrupt, do nothing. On the next interrupt, you're back to the first interrupt again, so trigger the monostables again. This will give a left/right and up/down indication every 1.5 ms, with a fairly low overhead.

PREVIOUS NEXT

 

Free Software
Delphi

Lesson 1
Delphi Programming
1.1. IDE Delphi
1.2. Component


Lesson 2
Printer Port
/ LPT
1.1.Basic
1.2.Address
1.3.Port Register
1.4.8 Bit Data Input
1.5.Test Circuitry
1.6.Assignment

Lesson 3
Printer Port / LPT
Experiments

3.1.LED
3.2.Swicht
3.3.Motor Stepper
3.4.DAC
3.5.ADC
3.6.Graph Display

Lesson 4
Serial Port

4.1.Basic
4.2.Hardware
4.3.Port Register

Lesson 5
Serial Port Experiments

5.1.LED
5.2.Stepper Motor
5.3.Swicht
5.4.ADC

Lesson 6
Game Port
Joy Stick
6.1. Basic
6.2. Experiments