home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP3 / QUAD0.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-02  |  5.3 KB  |  209 lines

  1. /***********************************************************************
  2.     
  3. FILE
  4.     quad0.c  -  quadrature position measurement routines
  5.  
  6. ENTRY ROUTINE
  7.     quad_getp  -  get position
  8.  
  9. PRIVATE ROUTINE
  10.     pulse  -  parallel port interrupt service routine
  11.  
  12. INITIALIZATION ROUTINE
  13.     quad_init  -  initialize this module
  14.  
  15. REMARKS
  16.     This module uses bits 0 and 1 of the IBM-PC parallel port for the
  17.     2 quadrature channels.  The parallel port interrupt service routine
  18.     is set up using the xignal package; hence you need to link with
  19.     xignal.lib
  20.  
  21. LAST UPDATE
  22.     26 January 1988
  23.  
  24.     Copyright(c) 1985-1988  D.M. Auslander and C.H. Tham
  25.  
  26. ***********************************************************************/
  27.  
  28. /***********************************************************************
  29.                             I M P O R T S
  30. ***********************************************************************/
  31.  
  32. #include "envir.h"
  33.  
  34. #include "inout.h"
  35. #include "xignal.h"
  36.  
  37.  
  38. /***********************************************************************
  39.                         P R I V A T E     D A T A
  40. ***********************************************************************/
  41.  
  42. static int oldstate;        /* previous state of quadrature input */
  43. static int curstate;        /* current quadrature state */
  44. static int pcount;          /* position indicator */
  45. static int error;           /* error flag, initialized to false */
  46. static double pscale;       /* count to displacement scaling factor */
  47.  
  48. #define PHASEMASK   0x03    /* mask for bits 0 and 1 on parallel port */
  49.  
  50. #define STATE00     0x00    /* quadrature states */
  51. #define STATE01     0x01
  52. #define STATE10     0x02
  53. #define STATE11     0x03
  54.  
  55. #define INPORT      0x3BE   /* parallel input port address */
  56.  
  57.  
  58. /***********************************************************************
  59.                     E N T R Y    R O U T I N E S
  60. ***********************************************************************/
  61.  
  62. /*----------------------------------------------------------------------
  63. FUNCTION
  64.     QUAD_GETP  -  read position value
  65.  
  66. SYNOPSIS
  67.     double quad_getp()
  68.  
  69. RETURNS
  70.     incremental position in inches
  71.  
  72. LAST UPDATE
  73.     26 January 1988
  74.         change to double return type
  75. ----------------------------------------------------------------------*/
  76.  
  77. double quad_getp()
  78. {
  79.     int istat;      /* interrupt status */
  80.     int count;      /* copy of pcount */
  81.  
  82.  
  83.     if (error)
  84.     {
  85.         printf("quadrature state transistion error\n");
  86.  
  87.         error = 0;                  /* reset error flag */
  88.     }
  89.     
  90.     istat = disable();              /* begin critical section */
  91.  
  92.     count = pcount;
  93.  
  94.     if (istat)                      /* exit critical section */
  95.         enable();
  96.  
  97.     return(count * pscale);
  98. }
  99.  
  100.  
  101.  
  102. /***********************************************************************
  103.                   P R I V A T E    R O U T I N E
  104. ***********************************************************************/
  105.  
  106. /*---------------------------------------------------------------------
  107. PROCEDURE
  108.     PULSE  -  quadrature pulse input interrupt service routine
  109.  
  110. SYNOPSIS
  111.     called by parallel port interrupt service routine
  112.  
  113. REMARKS
  114.     implements figure 3.10 in main text
  115.  
  116. LAST UPDATE
  117.     18 April 1985
  118. ----------------------------------------------------------------------*/
  119.  
  120. static void pulse()
  121. {
  122.  
  123.     oldstate = curstate;                /* update state variables */
  124.  
  125.     curstate = in(INPORT) & PHASEMASK;  /* read quadrature signal */
  126.  
  127.     switch (oldstate)
  128.     {
  129.         case STATE00:
  130.  
  131.             if (curstate == STATE01)
  132.                 --pcount;
  133.             else if (curstate == STATE10)
  134.                 ++pcount;
  135.             else
  136.                 error = 1;
  137.             
  138.             break;
  139.         
  140.         case STATE01:
  141.  
  142.             if (curstate == STATE11)
  143.                 --pcount;
  144.             else if (curstate == STATE00)
  145.                 ++pcount;
  146.             else
  147.                 error = 1;
  148.             
  149.             break;
  150.  
  151.         case STATE11:
  152.  
  153.             if (curstate == STATE10)
  154.                 --pcount;
  155.             else if (curstate == STATE01)
  156.                 ++pcount;
  157.             else
  158.                 error = 1;
  159.             
  160.             break;
  161.         
  162.         case STATE10:
  163.  
  164.             if (curstate == STATE00)
  165.                 --pcount;
  166.             else if (curstate == STATE11)
  167.                 ++pcount;
  168.             else
  169.                 error = 1;
  170.     }
  171.  
  172. }
  173.  
  174.  
  175. /***********************************************************************
  176.             I N I T I A L I Z A T I O N    R O U T I N E S
  177. ***********************************************************************/
  178.  
  179. /*---------------------------------------------------------------------
  180. PROCEDURE
  181.     QUAD_INIT  -  initialize
  182.  
  183. SYNOPSIS
  184.     void quad_init(scale)
  185.     double scale;
  186.  
  187. PARAMETERS
  188.     scale  -  displacement per quadrature count
  189.  
  190. LAST UPDATE
  191.     26 January 1988
  192.         calling routine specifies scaling factor
  193. ----------------------------------------------------------------------*/
  194.  
  195. void quad_init(scale)
  196. double scale;
  197. {
  198.  
  199.     pscale = scale;
  200.  
  201.     pcount = 0;
  202.     error = 0;
  203.  
  204.     curstate = oldstate = in(INPORT) & PHASEMASK;
  205.  
  206.     xignal(XIGPRN, pulse);  /* install pulse() as service routine */
  207.  
  208. }
  209.