Stepper Motor Help

Thread Starter

ty4reel

Joined Mar 27, 2008
5
I'm having trouble getting my stepper motor to step clockwise. Can someone help me with my code to see what I'm missing?

#include <mc9s12dp256.h>

//structure definition, type definition, initialize struct - rotation sequence
struct rotate{ // holds a rotation sequence (multiple rotations)
int num_steps; // 100 = 100 steps (360 degrees if stepangle = 3.6 degrees)
int direction; // 1=CW, 0=CCW
};
typedef struct rotate rot_type;

rot_type sequence[2] = { // this table defines the 2 rotations in the sequence
{100,1}, // 100 steps CW
{50,0} // 50 steps CCW
};

//structure definition, type definition, initialize struct - coil patterns
struct state{ // a linked structure (one structure for each state)
unsigned char patrn; // coil pattern
struct state *Next[2]; // holds next state if direction = 0,1
};
typedef struct state state_type;
// state 0 is defined by the 0th structure in fsm, state 1 by the 1st, etc.
#define S0 &fsm[0]
#define S1 &fsm[1]
#define S2 &fsm[2]
#define S3 &fsm[3]

state_type fsm[4] = { // motor specific -- a structure containing 4 structures
{0x33,{S1,S3}}, // coil pattern and next state when current state is S0
{0x66,{S2,S0}}, // ditto when current state is S1, etc..
{0xCC,{S3,S1}},
{0x99,{S0,S2}}
};

//function prototypes
void delayv(void);
void load_swi_addr(void);

/************main************/
void main(){
int i, steps;
state_type *ptr; // setup a pointer to a state-type data structure...
ptr = S0; // ...and initialize it to point to state S0 in fsm
load_swi_addr( );
DDRB |= 0xF0; // PB7-PB4 output
for(i=0;i<2;i++){ // 2 rotations in the sequence
steps = sequence.num_steps;
while(steps){
PORTB = ptr->patrn;//send out coil pattern of state pointed to by ptr
ptr = ptr->Next[sequence.direction];//pt to appropriate next state
delayv( ); //wait before sending another pattern
steps--; //keep rotating until all steps in current rotation are sent
}
}
asm("swi");
}

/*********functions*********/
void delayv( ){ // adjust count for motor application
unsigned int i,j;
for(i=0;i<0x7FF;i++){
for(j=0;j<0xFF;j++){
asm("nop");
}
}
}

void load_swi_addr( ){
asm("pshx");
asm("ldx #$ef00");
asm("stx $3e76"); //loads the isr for swi into the pseudo-vector table
asm("pulx");
}
/********* end of file ***********************/
 

hgmjr

Joined Jan 28, 2005
9,029
Rich (BB code):
#include <mc9s12dp256.h>
 
//structure definition, type definition, initialize struct - rotation sequence
struct rotate{ // holds a rotation sequence (multiple rotations)
    int num_steps; // 100 = 100 steps (360 degrees if stepangle = 3.6 degrees)
    int direction; // 1=CW, 0=CCW
};
typedef struct rotate rot_type;
 
rot_type sequence[2] = { // this table defines the 2 rotations in the sequence
{100,1}, // 100 steps CW
{50,0} // 50 steps CCW
};
 
//structure definition, type definition, initialize struct - coil patterns
struct state{ // a linked structure (one structure for each state)
unsigned char patrn; // coil pattern
struct state *Next[2]; // holds next state if direction = 0,1
};
typedef struct state state_type;
// state 0 is defined by the 0th structure in fsm, state 1 by the 1st, etc.
#define S0 &fsm[0] 
#define S1 &fsm[1]
#define S2 &fsm[2]
#define S3 &fsm[3]
 
state_type fsm[4] = { // motor specific -- a structure containing 4 structures
   {0x33,{S1,S3}}, // coil pattern and next state when current state is S0
   {0x66,{S2,S0}}, // ditto when current state is S1, etc..
   {0xCC,{S3,S1}},
   {0x99,{S0,S2}}
};
 
//function prototypes 
void delayv(void);
void load_swi_addr(void); 
 
/************main************/
void main()
{
   int i, steps;
 
   state_type *ptr; // setup a pointer to a state-type data structure...
   ptr = S0; // ...and initialize it to point to state S0 in fsm
   load_swi_addr( ); 
   DDRB |= 0xF0; // PB7-PB4 output
   for(i=0;i<2;i++)
   { // 2 rotations in the sequence
      steps = sequence.num_steps;
      while(steps)
      {
        PORTB = ptr->patrn;    //send out coil pattern of state pointed to by ptr
        ptr = ptr->Next[sequence.direction];   //pt to appropriate next state
        delayv( );      //wait before sending another pattern
        steps--;    //keep rotating until all steps in current rotation are sent
      }
   }
   asm("swi");
}
 
/*********functions*********/
void delayv( )
{ // adjust count for motor application
  unsigned int i,j;
  for(i=0;i<0x7FF;i++)
  { 
     for(j=0;j<0xFF;j++)
    {
        asm("nop");
    }
  }
}
 
void load_swi_addr( )
{ 
   asm("pshx");
   asm("ldx #$ef00"); 
   asm("stx $3e76"); //loads the isr for swi into the pseudo-vector table
   asm("pulx");
} 
 
/********* end of file ***********************/



Reformatted you source code to make it more readable.

hgmjr
 
Top