/*******************************************************
FILE:     step.c

DESCRIPTION: Demo program to drive stepper motor.

REVISION HISTORY:
*******************************************************/
//--Includes

#include <16f88.h>

//--Setup commands

#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,NOWRT,NOLVP  // internal oscillator
#id 0x1234                     // 4 digit id
//#use standard_io(A)
//#use standard_io(B)
#use fast_io(A)
#use fast_io(B)
#use delay(clock=8000000)      // xx MHz resonator

//--Pin definitions

#define LED      PIN_B0   // LED for debugging
#define C1       PIN_B1   // Stepper coils
#define C2       PIN_B2
#define C3       PIN_B3
#define C4       PIN_B4

//--Global variables

char q1;
char seq[]={0x18,0x0c,0x06,0x12}; //coil step sequence

/*Stepper sequence is 1100,0110,0011,1001 for coils 1-4  with
coil 1 in right bit. Because coils are in pins b1-b4 on the
hardware, the sequence is left shifted by one place. */

/*******************************************************
  Stepping functions
*******************************************************/

/*********************************************************
    onestep(dir)
Turns stepper one step in direction dir. Dir is 0/1
for forward/reverse. The remainder function % is used
to cycle through the step sequence array.
q1 goes 0,1,2,3,0,1,2,3,... when stepping forward
and goes 3,2,1,0,3,2,1,0,... when stepping reverse.
*********************************************************/
void onestep(int8 dir)
{
    if (dir) // rev
      q1=(q1+3)%4; // next step sequence
    else      // for
      q1=(q1+1)%4;
    output_b(seq[q1]); // send to coils
}

/*********************************************************
    move(dir, nsteps, delay)
Turns stepper nsteps steps in direction dir with delay msec
between each step.
*********************************************************/
void move(int8 dir, int16 nsteps, int8 delay)
{
  int16 i;
  for (i=0;i<nsteps;i++) {
    onestep(dir);
    delay_ms(delay);
  }
}

/*********************************************************
    zero(axis)
Sets coils to "Step 1" configuration
*********************************************************/
void zero(char axis)
{
    if (axis==1) {
        q1=0;
        output_b(seq[q1]);
    }
}

/*********************************************************
    unlock(axis)
Turn off coils to motor axis
*********************************************************/
void unlock(char axis)
{
    if (axis==1)
        output_b(0);
}



/*******************************************************
  Main starts here
*******************************************************/

void main(void)
{
  int i;
  setup_oscillator(OSC_8MHZ);

//-------Set direction registers

  set_tris_a(0xFF);   // a is all inputs
  set_tris_b(0x00);   // b is all outputs


//-------Flash led a few times

  for (i=0;i<3;i++) {
    output_high(LED);
    delay_ms(100);
    output_low(LED);
    delay_ms(300);
  }
  delay_ms(1000);

//-------Make the motor dance (assume 7.5 deg/step motor)

  move(0,48,20); //360 deg forward
  delay_ms(500);
  move(1,48,20); //and reverse
  delay_ms(500);

//-------That's all folks

  sleep();

}


