brandon white

Stepper motor and stand along opperation

Discussion created by brandon white on May 27, 2011
Latest reply on Jun 3, 2011 by brandon white

Im working with the MC9S12DG256B MCU and I'm having issues getting a stepper motor to spin when I want it to. I have trouble shooted my code so I know the issue is in my function that initiates stand alone opperation: init_PPL.


When the init_PPL is initiated in my main,  the stepper motor stops spinning, but you can hear it trying to spin. The init_PPL is for stand alone opperation. I have a number of other functions in the source code, but when they are initiated without init_PPL, the stepper motor spins just fine.  Here is my source code:

      /* common defines and macros */#include "derivative.h"      /* derivative-specific definitions */#include <mc9s12dg256.h>    #include <math.h>void init_PLL(void);void main(void){init_PPL(); // running the board on stand alonewhile (1){  /*******************CREATE VARIABLES*******************/int i; //Used in a for loop//This array actually contains the state values that will be placed on Port U.//State #0 corresponds to a value of 0x06, state #1 corresponds to a value of 0x02, etc.char state_array[NUM_OF_STATES] = {0x06, 0x02, 0x0A, 0x08, 0x09, 0x01, 0x05, 0x04};int steps_to_move; //The # of rotational steps the motor will make.char next_state; int a = 5;                   //Used to select the next state to put in register U.DDRB = 0xFF;                   /********************SET UP PORT U********************/DDRH = 0xFF; //Writing 0xFF to DDRU sets all bits of Port U to act as output.PTH = 0; //Init Port U by writing a value of zero to Port U./******************************************************/steps_to_move = 600; //Set the # of steps to move. An arbitrary positive # can be used.next_state = 0; //Init next_state to state 0. next_state can start from any state                //within the range of possible states in this example, 0-7PTH = state_array[next_state];                 //Init Port U to the starting state. In this example,                //since only 4 pins are needed to control the motor, only                //the lower nibble of Port U is being used. This line                //selects state 0 and places the corresponding value//(0x06) in the lower nibble of Port U.for(i = 0; i < DELAY_MAX; i++){//Wait here for a while.}while (steps_to_move > 0){if (next_state > (NUM_OF_STATES - 1)) //If next_state is greater than the highest//available state, 7, then cycle back to 0{next_state = 0;}PTH = state_array[next_state]; //Place new value in Port U. Rotation may be observedfor(i = 0; i < DELAY_MAX; i++){//Wait here for a while.}next_state++; //Increment next_state. Cycling though the states causes rotation//in one direction. Decrementing states causes opposite rotation.steps_to_move--; //Subtract 1 from the total # of steps remaining to be moved.}PORTB =  PLLCTL;delay(500);}/********************//*//The following code rotates the motor back in the opposite direction.steps_to_move = 100;while (steps_to_move > 0){if (next_state < 0){next_state = (NUM_OF_STATES - 1);}PTH = state_array[next_state];for(i = 0; i < DELAY_MAX; i++){//Wait here for a while.next_state--; }steps_to_move--;}*/} //End of Mainvoid init_PLL( ){   // initializes phase-locked loop (hardware)for 24Mhz CPU clock   asm(sei);    // for running board standalone w/out Codewarrior    CLKSEL &= ~0x80;   PLLCTL |= 0x40;   SYNR = 0x05;   REFDV = 0x01;   while((CRGFLG & 0x08) == 0){ // wait here for lock      CRGFLG |= 0x08;   }   CLKSEL |= 0x80;   asm(cli);}