//#define TEST /**************************************************************************** Module steps12.c Description This is a module to drive a 2-phase stepper motor using the CMPE118 LM298 dual H-Bridge Module. The module uses a periodic interrupt to act as a heartbeat. The stepper is then advanced at a multiple of that frequency. Notes This module requires the termio.c file also. STEPS12_Init currently uses the TERMIO_Init function to properly initialize the PLL clk. (24Mhz) History When Who What/Why -------------- --- -------- 02/01/06 13:34 rjk Began Coding ****************************************************************************/ /*----------------------------- Include Files -----------------------------*/ #include /* common defines and macros */ #include /* derivative information */ #include #include #include /******* GLOBALS ****************************/ #define PERIOD 188 #define MAX_STEPS 4 // total number of step states for driving FULL STEP static unsigned char StepDirection; static long StepsLeft; static int StepState; static long StepPeriod; static long Counter; static unsigned char CurrentlyStepping=0; /**************************************************************************** Function STEPS12_Init Parameters Period: Number of milliseconds between each step (must be an integer); Direction: 0 for CW or non-zero for CCW Steps: Number of steps to advance motor Returns None. Description Initializes the RTI module for OSCCLK=8MHz and sets global variables. Notes ****************************************************************************/ void STEPS12_Init(long Period, unsigned char Direction, long Steps) { TERMIO_Init(); TIOS |= TIOS_IOS4_MASK; // enable output capture TSCR1 |= TSCR1_TEN_MASK; // enable timer module TCTL1 &= ~(TCTL1_OM4_MASK|TCTL1_OL4_MASK); // disconnects output pins from TIM TSCR2 |= (TSCR2_PR0_MASK|TSCR2_PR1_MASK|TSCR2_PR2_MASK); TSCR2 &= ~(TSCR2_TCRE_MASK|TSCR2_TOI_MASK); TC4 = TCNT+PERIOD; TFLG1=TFLG1_C4F_MASK; TIE |= TIE_C4I_MASK; StepDirection=Direction; StepsLeft=Steps; StepState=1; StepPeriod=Period; Counter=1; CurrentlyStepping=1; DDRA|=0x0F; // First 4 pins on PORTA all set to be outputs. EnableInterrupts; return; } /**************************************************************************** Function STEPS12_RTI_Resp Parameters None. Returns None. Description Interrupt routine that consists of the state machine for stepping the motor in the proper order. Interrupt Routine to handle RTI. Function will be executed by rate defined in STEPS12_Init. Notes ****************************************************************************/ void interrupt 12 STEPS12_OC4_Resp(void) { TC4 += PERIOD; TFLG1=TFLG1_C4F_MASK; if(Counter>=StepPeriod) Counter=1; else { Counter++; return; } if(StepsLeft<=0) { CurrentlyStepping=0; return; } else StepsLeft--; if(StepDirection) { if(StepState>=MAX_STEPS) StepState=1; else StepState++; } else { if(StepState<=1) StepState=MAX_STEPS; else StepState--; } // Use PortA for motor direction logic // PortA0 = Motor 1 enable // PortA1 = Motor 1 direction // PortA2 = Motor 2 enable // PortA3 = Motor 2 direction // For Full Step driving both motor outputs always enabled // HINT: both motors outputs will not always be on for wave driving or // half step driving! PORTA|=PORTA_BIT0_MASK; PORTA|=PORTA_BIT2_MASK; switch(StepState) { case 1: PORTA|=PORTA_BIT1_MASK; PORTA|=PORTA_BIT3_MASK; break; case 2: PORTA|=PORTA_BIT1_MASK; PORTA&=~PORTA_BIT3_MASK; break; case 3: PORTA&=~PORTA_BIT1_MASK; PORTA&=~PORTA_BIT3_MASK; break; case 4: PORTA&=~PORTA_BIT1_MASK; PORTA|=PORTA_BIT3_MASK; break; } return; } /**************************************************************************** Function STEPS12_Stop Parameters None. Returns None. Description Stops the RTI from advancing the motor. Notes ****************************************************************************/ void STEPS12_Stop(void) { StepsLeft=0; CurrentlyStepping=0; return; } /**************************************************************************** Function STEPS12_IsStepping Parameters None. Returns 1 is StepsLeft > 1, non-zero if finished stepping. Description Used to check status of stepper motor driver. Notes ****************************************************************************/ unsigned char STEPS12_IsStepping(void) { return CurrentlyStepping; } #ifdef TEST void main(void) { while(1) { while(STEPS12_IsStepping()); // wait until the active steps are done STEPS12_Init(10,1,100); while(STEPS12_IsStepping()); STEPS12_Init(10,0,100); } return; } #endif // TEST /*------------------------------ End of file ------------------------------*/