Hello everyone,

                        

         Feels good to get back to my favorite pastime: designing hardware/software

         projects for the ZX81. This time we mix a little hardware and software and

         squeeze more performance out of two simple chips than you might think

         possible, all of which echoes the simplicity of the original ZX81 design.

 

                        A PROPORTIONAL JOYSTICK INTERFACE

                                    1996 - wilf rigter - Revised 2005

        

         WHAT IT IS

        

         This 2 chip (74HC138 and 74HC245) joystick interface provides 8 analog

         inputs with 5 bit accuracy and is suitable for connecting 2 proportional

         (IBM/APPLE) joysticks complete with left/right fire button inputs.

         It can also be used for making 8 analog measurements which use resistive

         transducers such as thermistors, CdS photocells, pressure sensitive __

         resistors, servo position encoders, potentiometers, etc.

         In this project, the 245 bi-directional buffer is used as a threshold

         detector to measure the time constant of a simple resistor and capacitor

         network. This CMOS digital logic chip is not an analog device but its

         accuracy is sufficient for a joystick interface and similar applications.

         I used a SN74HC245 chip made by TI. Other chips may require different

         RC values.

         

 

 

         HOW THE HARDWARE WORKS

        

         The 74HC138 is used as a conventional I/O decoder and I used Q6 for

         address F5 in this application but any other decoder output except Q0

         can be used.

        

         A 3300 pf capacitor is connected between a HC245 input and ground and a

         joystick potentiometer (100K) or switch is connected from the input and

         the joystick common line to +5V. A 4.7K series resistor is sets the

         minimum value for R. Most joysticks also include a ZERO adjustment pot.

 

         The joystick resistance is measured by first discharging the capacitor

         by writing a zero to the output port and then sampling the inputs 32

         times with a fast software loop and recording the samples in memory.

         The RC values are approximate and should be adjusted to match the

         joystick pot values and 245 chip. For example a 40K pot required a

         0.01 uF cap and a minimum 2K series resistor.

        

         If a switch is used instead of a pot you will of course only be able to

         read a two state output. Light Dependent Resistors and thermistors will

         need calibration and linearization. Some experimentation may be required

         to give the desired results.

                 

         HOW THE SOFTWARE WORKS

        

         The hardware and sampling software are tightly coupled in this simple

         hardware circuit. The interface is designed to be used in the SLOW mode

         and a special software routine is required to sample during a time when

         it cannot be interrupted by the Non Maskable Interrupt (NMI). This is

         accomplished by hooking the I/O sampling routine into the ZX video service

         routines.

       

         The annotated listing 1 shows how SAMPLE is embedded between the video

         service routines right after DPILE processing and before NMI is enabled.

         The VIDEO1 routine can only be started indirectly by calling START to

         change the video routine vector in the IX register to the start of the

         VIDEO1 routine.

        

 Note the use of the INIR instruction to transfer 32 samples from the I/O

         port to the system variable Printer Buffer (PRBUFF). The VIDEO1 routine

         has a constant execution time.

        

         The DECODE software executes in a variable time and must be called from

         the application program. DECODE converts the stored samples to analog values

         0 to 31 which are then stored into a variable array of Character values

         representing the time constant of each input. The 32 samples in the PRBUPF

         are scanned S times and bits are tested with a bit mask in C until the bit

         is 1 or the loop counter in B is zero. In either case the value in B is

         stored in the corresponding variable byte. The decoded analog values can

         be used by BASIC or ML programs for example:

          

 

         10 DIM A$(8)

         20 RAND USR 16518

         30 RAND USR 16516

         40 PRINT AT 0,0; A$

         50 GOTO 30

        

         If you would like to experiment, you can store the analog values directly

         to the screen by replacing the instruction LD HL, (4010) at the start of

         DECODE with LD DE, (400C) and using the following BASIC code.

        

         10 DIM A$(8)

         20 RAND USR 16518

         30 RAND USR 16516

         40 GOTO 30

        

         And finally an example of positioning software which assumes that the

         joystick X and Y pots are connected to input 0 and 1:

        

         10 DIM AS (8)

         20 RARD USR 16518

         30 RAND USR 16516

         40 PLOT (CODE A$(l)),(CODE A$(2))

         50 GOTO 40

                

                                 LISTING 1

                      I/O SAMPLER ROUTINE 1996 WILF RIGTER

        

        

     CONVERT       JR DECODE      ;CALL WITH RAND USR 16516 TO DECODE SAMPLES

 

     START         LD IX,VIDEO1 ;CALL WITH RAND USR 16518 TO START SAMPLE

                   RET           ;BY INTERCEPTING THE ZX VIDEO ROUTINE

      

     VIDEO1        LD A,R       ;THE VIDEO / SAMPLING ROUTINE

                   LD BC,l901   ;24 LINES OF VIDEO

                   LD A,F5      ;DELAY

                   CALL 2B5     ;JP TO DFILE

                                 

     SAMPLE        XOR A         ;NOW WE DO THE ACTUAL SAMPLING

                   OUT 5F,A       ;DISCHARGE THE CAPS

                   LD HL,40C3    ;USE THE PRINTER BUFFER @ 403C

                   LD BC,205F    ;TO STORE 32 BYTES

                   INIR           ;AT 150,000 SAMPLES/S

                         

     VIDEO2        CALL 292       ;NOW THE APPLICATION CODE

                   CALL 220       ;AND VSYNC

                   LD IX,SAMPLE  ;RESTORE SAMPLE VECTOR IN IX

                   JP 2A4        ;RETURN TO APPLICATION

        

                                  ;SCAN AND CONVERT THE SAMPLE BYTES

     DECODE                       ;INTO 8 ANALOG VALUES BETWEEN 0-31

                   LD HL,(4010)  ;START OP BASIC VARIABLES

                   LD DE,0006    ;SKIP THE HEADER

                   ADD HL,DE      ;FIRST ELEMENT OF AS ARRAY

                   LD C,01       ;SET UP BIT MASK BIT 0

     LOOP1         LD DE,403C    ;START OF PRINTER BUFFER

                   LD B,1F       ;VALUE COUNTER

     LOOP2         LD A, (DE)    ;GET SAMPLE

                   AND C         ;COMPARE BIT

                   JR Z LOOP3    ;RECORD VALUE IF BIT IS 1

                   INC DE        ;NEXT SAMPLE BYTE

                   DJNZ LOOP2    ;LAST SAMPLE BYTE?

     LOOP3         LD A,B        ;RECORD VALUE

                   LD (HL),A;    STORE TO VARIABLE

                   INC HL        ;NEXT VARIABLE

                   RLC C         ;NEXT BIT

                   JR NC LOOP1   ;ANY MORE BITS

                   RET       ;RETURN IF LAST BIT DONE

       

        

         THE CREDITS

        

         This project is simplified version of Fred Nachbaur’s Proportional

         Joystick Interface (PSI) in one of his excellent ZX BREADBOARDER

         articles written up in 1989 in ZXAPPEAL, the Vancouver Sinclair

         User Group newsletter

     

         Fred’s design uses 3 chips including a 556 type timer to measure

         2 analog voltages and two switch inputs.