/* Contents of file "STEPMOT.PLD" */

Name       STEPMOT;  /* Remember... it is BEST to make Name and filename match up */
Partno     0001;
Date       10/25/2000;
Revision   01;
Designer   KEH;
Company    RHIT;
Assembly   EC130;
Location   Terre Haute, IN;
Device     G20V8A;

/******************************************************************/
/*  Stepping Motor Controller                                     */
/*  (Up/Down 2-bit Gray Code Counter                              */
/*   Example of using "Sequence" Statement to design State Machine*/
/******************************************************************/
/*  Allowable Target Device Types:  G20V8A                        */
/******************************************************************/

/**  Inputs  **/

Pin 1        =  CLK    ;      /*     CLOCK (10 Hz)               */
Pin 2        =  DIR    ;      /*     DIRECTION                   */
Pin 3        =  START  ;      /*     START                       */

/**  Outputs  **/

Pin 22       =  !Q1       ;      /*  Coil A                        */
Pin 21       =  !Q0       ;      /*  Coil B                        */
Pin 20       =  !Q1B      ;      /*  Coil C                        */
Pin 19       =  !Q0B      ;      /*  Coil D                        */
Pin 18       =  REVOLUTION;      /*  This output pulses once per mag field revolution */
/** Declarations and Intermediate Variable Definitions **/

field my_state = [Q1..0];  /* "field" is a keyword used to define the state variables 
                               collectively referred to as "my_state".
                               -- in this example, only 4 states need be defined, so
                               only two state variables Q1 and Q0 are created. */

$define NE 'b'00       /* Define states using "define" keyword..give states meaningful names.  */
$define SE 'b'01       /* The 'b' prefix indicates binary number follows.                      */
$define SW 'b'11       /* The state names refer to direction of motor's magnetic field vector, */
$define NW 'b'10       /* NE = northeast, SE = southeast, SW = southwest, NW = northwest.      */

                       /*** NOTE -- THERE ARE NO SEMICOLONS ON $define STATEMENT!!  ******/

sequence my_state      /* The "sequence" key word is used to create a state machine in CUPL. It identifies
			  the field "my_state" (defined above) as the collection of state variables. */
{  
     present NE   if (!START) next NE;      /* Start with state NE because it is the reset state = 'b'00 */
                                            /* If START is not asserted, remain in state NE.             */
                  if (START & DIR) next SE; /* If START is asserted and DIR = 1, rotate mag fld clockwise */
                  if (START & !DIR) next NW;/* If START is asserted and DIR = 0, rotate mag fld CCW */
                  out REVOLUTION;           /* Moore type output handled with "out" keyword.    */

     present SE   if (!START) next SE;      /* Remain in this state if START is not asserted */
                  if (START & DIR) next SW; /* If START is asserted and DIR = 1, rotate mag fld CW */
                  if (START & !DIR) next NE;/* If START is asserted and DIR =0, rotate mag fld CCW */


     present SW   if (!START) next SW;
                  if (START & DIR) next NW;
                  if (START & !DIR) next SE;


     present NW   if (!START) next NW;
                  if (START & DIR) next NE;
                  if (START & !DIR) next SW; 
}

/**  Logic Equations  **/

Q1B = !Q1;
Q0B = !Q0;