home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol1 / input / joyout < prev    next >
Text File  |  1990-01-26  |  7KB  |  240 lines

  1. (c)  Copyright 1989 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice, and 
  3. is provided "as is" without warranty of any kind, either expressed or implied.  
  4. The entire risk as to the use of this information is assumed by the user.
  5.  
  6.  
  7.  
  8.                         Joystick Port Output
  9.  
  10.                       Carolyn Scheppner - CATS
  11.  
  12.  
  13.  
  14. Each Amiga joystick port has three pins that can be easily configured as 
  15. output lines and controlled through software.  This capability makes it 
  16. possible to operate an external device under program control.  
  17.  
  18. By setting the appropriate system data bit to a 1, you can set the 
  19. corresponding line in the joystick port high (approximately 4-5 volts).  
  20. Setting the data bit to 0 will set the corresponding joystick line low 
  21. (approximately 0 volts).
  22.  
  23. Both the left and right joystick ports can be used for output lines, but 
  24. the left port is reserved for the Amiga mouse.  This article will focus on 
  25. the use of the right port for output.
  26.  
  27.  
  28. The pins on the Amiga's joystick port are numbered as follows.
  29.    
  30.                          1   2   3   4   5
  31.                            6   7   8   9
  32.  
  33.                         Amiga Joystick Port
  34.  
  35.  
  36. Pins 9, 5 and 6 can be configured as output lines.  Pin 8 is ground.
  37.  
  38. Pin     Name      Output Enable Bit     Data/Control Bit
  39.  
  40. Pin 9   POT Y       POTGO bit 15          POTGO bit 14
  41. Pin 5   POT X       POTGO bit 13          POTGO bit 12
  42. Pin 6   Fire      ciaa.ciaddra bit 7    ciaa.ciapra bit 7
  43. Pin 8   Ground   
  44.  
  45.  
  46. The POT X and POT Y pins are normally potentiometer inputs.  When set to 
  47. output, these lines become digital outputs.  However, due to the large 
  48. capacitors on them, it may take up to 300 microseconds for a line to 
  49. change state.  
  50.  
  51. The POTGO bits which control the POT X and POT Y lines are allocated and 
  52. set using potgo.resource functions.  This is illustrated in the program below.
  53. First, the AllocPotBits() function is used to allocate the enable and data 
  54. bits for the two POT pins of the right joystick port.  Next, WritePotgo() is
  55. used to set the pins to output either high or low.  Finally, FreePotBits() is
  56. used to free the bits before exiting.  
  57.  
  58. Using the SetPot program shown below to toggle the POT X and POT Y lines on 
  59. a test machine, we measured 0 volts, low and approximately 3.8 volts, high on 
  60. POT X and POT Y (pins 5 and 9) relative to ground (pin 8).
  61.  
  62. The Fire pin is attached to CIA A, Port A.  Unlike the POT bits, the CIA
  63. bits for the Fire pin are manipulated directly since there is currently no 
  64. system support for allocating or setting these particular CIA bits.  Take 
  65. extra care to preserve the other bits in ciaa.ciaddra and ciaa.ciapra and not 
  66. to overwrite the 8-bit registers when setting or clearing the Fire line.  
  67.  
  68. The program SetFire.c demonstrates how to set the Fire pin on the right port to 
  69. an output line and how to toggle the line.  Running the SetFire program on a
  70. test machine, we measured approximately 34 millivolts low and 4.7 volts high 
  71. on the Fire pin (pin 6), relative to ground (pin 8).  
  72.  
  73. The ability to set up and control joystick lines as outputs makes a wide
  74. variety of special applications possible.  Potential uses include data 
  75. acquisition and control, machine control, robotics and other areas where a
  76. micro offers an inexpensive solution to real-time problems.
  77.  
  78. --------------------------------------------------------------------------
  79. /*
  80.  * SetPot.c   C. Scheppner  CBM  01/89
  81.  * Sets right joyport PotX and PotY pins (pins 5 and 9) to 0 and 1
  82.  */
  83.  
  84. #include <exec/types.h>
  85. #include <libraries/dos.h>
  86. #include <resources/potgo.h>
  87.  
  88.  
  89. #define V1_POINT_2   33
  90.  
  91. /* potgo bits */
  92. #define START_B      0   
  93. #define START_F      (1L << START_B)
  94. #define OUTRY_B      15
  95. #define OUTRY_F      (1L << OUTRY_B)
  96. #define DATRY_B      14
  97. #define DATRY_F      (1L << DATRY_B)
  98. #define OUTRX_B      13
  99. #define OUTRX_F      (1L << OUTRX_B)
  100. #define DATRX_B      12
  101. #define DATRX_F      (1L << DATRX_B)
  102.  
  103. /* We want the right port PotX and PotY direction and data bits */
  104. #define OURBITS    (OUTRY_F|DATRY_F|OUTRX_F|DATRX_F)
  105.  
  106. /* Mask and bit combinations for setting and clearing the X and Y pots 
  107.  * For mask,  affect both the direction and data bits  
  108.  * For data,  CLR = (direction out,data 0)    SET = (direction out,data 1)
  109.  */
  110. #define POTRX_MSK  (OUTRX_F|DATRX_F)
  111. #define POTRX_CLR  (OUTRX_F)
  112. #define POTRX_SET  (OUTRX_F|DATRX_F)
  113.  
  114. #define POTRY_MSK  (OUTRY_F|DATRY_F)
  115. #define POTRY_CLR  (OUTRY_F)
  116. #define POTRY_SET  (OUTRY_F|DATRY_F)
  117.  
  118. struct PotgoBase *PotgoBase = NULL;
  119.  
  120. UWORD  ourpotbits = NULL; 
  121.  
  122. main(argc,argv)
  123. int argc;
  124. char **argv;
  125.    {
  126.    ULONG delay = 500;
  127.  
  128.    if(argc > 1)
  129.       {
  130.       if(argv[1][0]=='?')
  131.          cleanexit("USAGE: SetPot  (demos setting of right joyport potx/y)\n");
  132.       }
  133.  
  134.    if(!(PotgoBase=
  135.      (struct PotgoBase *)OpenResource(POTGONAME,V1_POINT_2)))
  136.        cleanexit("Can't open potgo\n",RETURN_FAIL);
  137.  
  138.    ourpotbits = AllocPotBits(OURBITS);
  139.    if(ourpotbits != OURBITS)   cleanexit("Can't alloc potbits\n,RETURN_FAIL");
  140.  
  141.    /* We got our bits... Loop until done */
  142.    printf("SetPot sets the right port POTX and POTY pins (5,9) to 0 and 1\n");
  143.    printf("Use CTRL/C to exit...\n");
  144.    while(!(SetSignal(0,0) & SIGBREAKF_CTRL_C))
  145.       {
  146.       /* We are setting both pots, so we use both X and Y, masks and data */
  147.       printf("\nSetting right Potx and Poty to 0...  ");
  148.       WritePotgo(POTRX_CLR|POTRY_CLR, POTRX_MSK|POTRY_MSK);
  149.       Delay(delay);
  150.  
  151.       printf("Setting right Potx and Poty to 1...  ");
  152.       WritePotgo(POTRX_SET|POTRY_SET, POTRX_MSK|POTRY_MSK);
  153.       Delay(delay);
  154.       }
  155.  
  156.    printf("\n");
  157.    cleanup();
  158.    exit(0);
  159.    }
  160.  
  161. cleanexit(s,n)
  162. char *s;
  163. int n;
  164.    {
  165.    if(*s)  printf(s);
  166.    cleanup();
  167.    exit(n);
  168.    }
  169.  
  170. cleanup()
  171.    {
  172.    if(ourpotbits)   FreePotBits(ourpotbits);
  173.    }
  174.  
  175. /* end */
  176.  
  177. --------------------------------------------------------------------------
  178. /*
  179.  * SetFire.c   C. Scheppner  CBM  01/89
  180.  * Sets right mouseport FIRE pin (pin 6) to 0 or 1
  181.  */
  182.  
  183. #include <exec/types.h>
  184. #include <hardware/cia.h>
  185. #include <libraries/dos.h>
  186.  
  187. #define DELAY 500
  188.  
  189. extern struct CIA ciaa;
  190.  
  191. main(argc,argv)
  192. int argc;
  193. char **argv;
  194.    {
  195.    ULONG delay = 500;
  196.  
  197.    if(argc > 1)
  198.       {
  199.       if(argv[1][0]=='?')
  200.          cleanexit("USAGE: SetFire (demos setting joy pin 6 to 1 or 0)\n",0);
  201.       }
  202.  
  203.    printf("SetFire sets right mouseport pin 6 to ~0v and ~5v\n");
  204.    printf("To exit, use CTRL/C\n");
  205.  
  206.    /* set right fire pin to output */
  207.    ciaa.ciaddra = ciaa.ciaddra | CIAF_GAMEPORT1;
  208.  
  209.    /* loop until CTRL/C */
  210.    while(!(SetSignal(0,0) & SIGBREAKF_CTRL_C))
  211.       {
  212.       /* turn off fire pin */
  213.       ciaa.ciapra = ciaa.ciapra & (~CIAF_GAMEPORT1);
  214.       printf("\nSet to 0...");
  215.       Delay(delay);
  216.  
  217.  
  218.       /* turn on fire pin */
  219.       ciaa.ciapra = ciaa.ciapra | CIAF_GAMEPORT1;
  220.       printf("  Set to 1...");
  221.       Delay(delay);
  222.       }
  223.  
  224.    printf("\n");
  225.    /* set right fire pin back to input */
  226.    ciaa.ciaddra = ciaa.ciaddra & (~CIAF_GAMEPORT1);
  227.    exit(0);
  228.    }
  229.  
  230. cleanexit(s,n)
  231. char *s;
  232. int n;
  233.    {
  234.    if(*s)  printf(s);
  235.    exit(n);
  236.    }
  237.  
  238. /* end */
  239.  
  240.