 <font face="courier new" size="2">
/* ////////////////////////////////////////////////////////////////////////////
                                 Handy Macros
//////////////////////////////////////////////////////////////////////////// */

#define set_motor(X) P1 = (P1 & 0xC0) | X       /* Set the motor control lines
                                                   without messing up the other
                                                   two bits of Port 1 */

/* ////////////////////////////////////////////////////////////////////////////
                           Local Function Prototypes
//////////////////////////////////////////////////////////////////////////// */

void step_the_motor(signed char);

/* ////////////////////////////////////////////////////////////////////////////
                               Module Variables
//////////////////////////////////////////////////////////////////////////// */

/*  These macros correlate the L6219 signal names with their positions within
    Port 1.  Note that here we call the two windings 'A' and 'B' instead of '1'
    and '2' like the data sheet does.  */

#define A_I0    0x01                            /* Hardware dependent */
#define A_I1    0x02                            /* Hardware dependent */
#define A_Ph    0x04                            /* Hardware dependent */
#define B_I0    0x20                            /* Hardware dependent */
#define B_I1    0x10                            /* Hardware dependent */
#define B_Ph    0x08                            /* Hardware dependent */

/*  These macros give names to the various possible settings of the current
    control signals.  The numbers in the names indicate percent of full
    current.  */

#define A_0     (A_I0 | A_I1)
#define A_33    A_I1
#define A_67    A_I0
#define A_100   0

#define B_0     (B_I0 | B_I1)
#define B_33    B_I1
#define B_67    B_I0
#define B_100   0

/*  These macros give names to the directions of current flow in the two
    windings.  */

#define A_PLUS  A_Ph
#define A_MINUS 0

#define B_PLUS  B_Ph
#define B_MINUS 0

/*  This table specifies the sequencing of the I/O lines needed to make the
    motor move.  See the data sheet for the L6219 motor driver for details.  */

#if STEP_FULL                           /* Full stepping with 100% current */
static code char sequence[] = {
    A_MINUS | A_100  | B_MINUS | B_100,
    A_PLUS  | A_100  | B_MINUS | B_100,
    A_PLUS  | A_100  | B_PLUS  | B_100,
    A_MINUS | A_100  | B_PLUS  | B_100
    };
#endif

#if STEP_HALF                           /* Half stepping with 67% current */
static code char sequence[] = {
    A_MINUS | A_0    | B_PLUS  | B_67,
    A_PLUS  | A_67   | B_PLUS  | B_67,
    A_PLUS  | A_67   | B_PLUS  | B_0,
    A_PLUS  | A_67   | B_MINUS | B_67,
    A_PLUS  | A_0    | B_MINUS | B_67,
    A_MINUS | A_67   | B_MINUS | B_67,
    A_MINUS | A_67   | B_MINUS | B_0,
    A_MINUS | A_67   | B_PLUS  | B_67
    };
#endif

#if STEP_QUARTER                        /* Quarter stepping with 67% current */
static code char sequence[] = {
    A_MINUS | A_0    | B_PLUS  | B_67,
    A_PLUS  | A_33   | B_PLUS  | B_67,
    A_PLUS  | A_67   | B_PLUS  | B_67,
    A_PLUS  | A_67   | B_PLUS  | B_33,
    A_PLUS  | A_67   | B_PLUS  | B_0,
    A_PLUS  | A_67   | B_MINUS | B_33,
    A_PLUS  | A_67   | B_MINUS | B_67,
    A_PLUS  | A_33   | B_MINUS | B_67,
    A_PLUS  | A_0    | B_MINUS | B_67,
    A_MINUS | A_33   | B_MINUS | B_67,
    A_MINUS | A_67   | B_MINUS | B_67,
    A_MINUS | A_67   | B_MINUS | B_33,
    A_MINUS | A_67   | B_MINUS | B_0,
    A_MINUS | A_67   | B_PLUS  | B_33,
    A_MINUS | A_67   | B_PLUS  | B_67,
    A_MINUS | A_33   | B_PLUS  | B_67
    };
#endif

#if STEP_SIXTH                          /* Sixth stepping with 100% current */
static code char sequence[] = {
    A_MINUS | A_0    | B_PLUS  | B_100,
    A_PLUS  | A_33   | B_PLUS  | B_100,
    A_PLUS  | A_67   | B_PLUS  | B_100,
    A_PLUS  | A_100  | B_PLUS  | B_100,
    A_PLUS  | A_100  | B_PLUS  | B_67,
    A_PLUS  | A_100  | B_PLUS  | B_33,
    A_PLUS  | A_100  | B_PLUS  | B_0,
    A_PLUS  | A_100  | B_MINUS | B_33,
    A_PLUS  | A_100  | B_MINUS | B_67,
    A_PLUS  | A_100  | B_MINUS | B_100,
    A_PLUS  | A_67   | B_MINUS | B_100,
    A_PLUS  | A_33   | B_MINUS | B_100,
    A_PLUS  | A_0    | B_MINUS | B_100,
    A_MINUS | A_33   | B_MINUS | B_100,
    A_MINUS | A_67   | B_MINUS | B_100,
    A_MINUS | A_100  | B_MINUS | B_100,
    A_MINUS | A_100  | B_MINUS | B_67,
    A_MINUS | A_100  | B_MINUS | B_33,
    A_MINUS | A_100  | B_MINUS | B_0,
    A_MINUS | A_100  | B_PLUS  | B_33,
    A_MINUS | A_100  | B_PLUS  | B_67,
    A_MINUS | A_100  | B_PLUS  | B_100,
    A_MINUS | A_33   | B_PLUS  | B_100,
    A_MINUS | A_67   | B_PLUS  | B_100
    };
#endif

/* ////////////////////////////////////////////////////////////////////////////
                               step_the_motor()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION:    This function moves the motor by one step in the specified
                direction.

NOTE:           A "step" is a full motor step (1.8°) if STEP_FULL is 1, or a
                half step (0.9°) if STEP_HALF is 1, or a quarter step (0.45°)
                if STEP_QUARTER is 1.

REVISIONS:      31 Jul 06 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */

void step_the_motor(signed char inc) {

    static signed char  index;                  /* Sequence table index */

    index += inc;                               /* Bump sequence table index */

#if STEP_FULL
    index &= 0x03;                              /* Wrap at end of table */
#endif
#if STEP_HALF
    index &= 0x07;                              /* Wrap at end of table */
#endif
#if STEP_QUARTER
    index &= 0x0F;                              /* Wrap at end of table */
#endif
#if STEP_SIXTH
    if (index > 23) index = 0;                  /* Wrap at end of table */
    if (index < 0)  index = 23;                 /* Wrap at end of table */
#endif

    set_motor(sequence[index]);                 /* Apply new pattern */
    }                                           /* End step_the_motor() */

</font> 