A stepper gets controlled by cycling through eight states. Here’s the code I use to drive a stepper motor (extract of example from libpruio)
`
//! Set values of all four output pins.
#define PIN_OUT(a, b, c, d)
if (pruio_gpio_setValue(Io, P1, a)) {printf(“setValue P1 error (%s)\n”, Io->Errr); break;}
if (pruio_gpio_setValue(Io, P2, b)) {printf(“setValue P2 error (%s)\n”, Io->Errr); break;}
if (pruio_gpio_setValue(Io, P3, c)) {printf(“setValue P3 error (%s)\n”, Io->Errr); break;}
if (pruio_gpio_setValue(Io, P4, d)) {printf(“setValue P4 error (%s)\n”, Io->Errr); break;}
/*! \brief Make the motor move the next step.
\param Io Pointer to PruIo structure.
\param Rot Rotation direction (1 or -1).
This function sets 4 output pins for a stepper motor driver. It
remembers the last step as static variable (starting at 0 = zero) and
adds the new position to it. So the Rot parameter should either be 1 or
-1 to make the motor move one step in any direction.
*/
void
move(pruIo *Io, int Rot) {
static int p = 0;
p += Rot;
p &= Rot & 1 ? 7 : 6 ;
switch §{
case 1: PIN_OUT(1,0,0,0); break;
case 2: PIN_OUT(1,1,0,0); break;
case 3: PIN_OUT(0,1,0,0); break;
case 4: PIN_OUT(0,1,1,0); break;
case 5: PIN_OUT(0,0,1,0); break;
case 6: PIN_OUT(0,0,1,1); break;
case 7: PIN_OUT(0,0,0,1); break;
default: PIN_OUT(1,0,0,1)
}
}
`