home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
-
- FILE
- quad0.c - quadrature position measurement routines
-
- ENTRY ROUTINE
- quad_getp - get position
-
- PRIVATE ROUTINE
- pulse - parallel port interrupt service routine
-
- INITIALIZATION ROUTINE
- quad_init - initialize this module
-
- REMARKS
- This module uses bits 0 and 1 of the IBM-PC parallel port for the
- 2 quadrature channels. The parallel port interrupt service routine
- is set up using the xignal package; hence you need to link with
- xignal.lib
-
- LAST UPDATE
- 26 January 1988
-
- Copyright(c) 1985-1988 D.M. Auslander and C.H. Tham
-
- ***********************************************************************/
-
- /***********************************************************************
- I M P O R T S
- ***********************************************************************/
-
- #include "envir.h"
-
- #include "inout.h"
- #include "xignal.h"
-
-
- /***********************************************************************
- P R I V A T E D A T A
- ***********************************************************************/
-
- static int oldstate; /* previous state of quadrature input */
- static int curstate; /* current quadrature state */
- static int pcount; /* position indicator */
- static int error; /* error flag, initialized to false */
- static double pscale; /* count to displacement scaling factor */
-
- #define PHASEMASK 0x03 /* mask for bits 0 and 1 on parallel port */
-
- #define STATE00 0x00 /* quadrature states */
- #define STATE01 0x01
- #define STATE10 0x02
- #define STATE11 0x03
-
- #define INPORT 0x3BE /* parallel input port address */
-
-
- /***********************************************************************
- E N T R Y R O U T I N E S
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- FUNCTION
- QUAD_GETP - read position value
-
- SYNOPSIS
- double quad_getp()
-
- RETURNS
- incremental position in inches
-
- LAST UPDATE
- 26 January 1988
- change to double return type
- ----------------------------------------------------------------------*/
-
- double quad_getp()
- {
- int istat; /* interrupt status */
- int count; /* copy of pcount */
-
-
- if (error)
- {
- printf("quadrature state transistion error\n");
-
- error = 0; /* reset error flag */
- }
-
- istat = disable(); /* begin critical section */
-
- count = pcount;
-
- if (istat) /* exit critical section */
- enable();
-
- return(count * pscale);
- }
-
-
-
- /***********************************************************************
- P R I V A T E R O U T I N E
- ***********************************************************************/
-
- /*---------------------------------------------------------------------
- PROCEDURE
- PULSE - quadrature pulse input interrupt service routine
-
- SYNOPSIS
- called by parallel port interrupt service routine
-
- REMARKS
- implements figure 3.10 in main text
-
- LAST UPDATE
- 18 April 1985
- ----------------------------------------------------------------------*/
-
- static void pulse()
- {
-
- oldstate = curstate; /* update state variables */
-
- curstate = in(INPORT) & PHASEMASK; /* read quadrature signal */
-
- switch (oldstate)
- {
- case STATE00:
-
- if (curstate == STATE01)
- --pcount;
- else if (curstate == STATE10)
- ++pcount;
- else
- error = 1;
-
- break;
-
- case STATE01:
-
- if (curstate == STATE11)
- --pcount;
- else if (curstate == STATE00)
- ++pcount;
- else
- error = 1;
-
- break;
-
- case STATE11:
-
- if (curstate == STATE10)
- --pcount;
- else if (curstate == STATE01)
- ++pcount;
- else
- error = 1;
-
- break;
-
- case STATE10:
-
- if (curstate == STATE00)
- --pcount;
- else if (curstate == STATE11)
- ++pcount;
- else
- error = 1;
- }
-
- }
-
-
- /***********************************************************************
- I N I T I A L I Z A T I O N R O U T I N E S
- ***********************************************************************/
-
- /*---------------------------------------------------------------------
- PROCEDURE
- QUAD_INIT - initialize
-
- SYNOPSIS
- void quad_init(scale)
- double scale;
-
- PARAMETERS
- scale - displacement per quadrature count
-
- LAST UPDATE
- 26 January 1988
- calling routine specifies scaling factor
- ----------------------------------------------------------------------*/
-
- void quad_init(scale)
- double scale;
- {
-
- pscale = scale;
-
- pcount = 0;
- error = 0;
-
- curstate = oldstate = in(INPORT) & PHASEMASK;
-
- xignal(XIGPRN, pulse); /* install pulse() as service routine */
-
- }
-