Stepper motor and stand along opperation

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Stepper motor and stand along opperation

817 Views
JackAlice
Contributor I

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);}
Labels (1)
0 Kudos
Reply
3 Replies

651 Views
kef
Specialist I

init_PLL() seems multiplying your bus clock frequency 3 times. Since step delays in your code are done with for loops(which compiler may optimized away and you get no delay), then no wonder they get to short after init_PLL() call.

 

0 Kudos
Reply

651 Views
JackAlice
Contributor I

Actually a better question:

How can I adjust the for loops to work with that frequency?

0 Kudos
Reply

651 Views
JackAlice
Contributor I

Which register controls the bus frequency?

0 Kudos
Reply