Jump to content
43oh

stepper motor code


Recommended Posts

I did a little research tonight and stumbled across an App Note from TI.

 

Check out MSP430 Stepper Motor Controller:

- App Note

- source code

 

There's tons of stepper motor discussions out there. The software on all of them seem to be similar but the driver hardware isn't consistently the same. So make sure you design your circuit to handle the current that your motor will require.

Link to post
Share on other sites

A lot depends on the motors you have. For example I have played (briefly) with steppers, ensuring I can drive them. All I used was a 430 and a ULN2803 (Darlington Transistor Array). The 2803 has enough pins to drive 2 of the motors I was playing with and also has the suppression diode to stop the magnetic field collapse turning me into on unhappy expermienter.

 

The motors were scavenged salvaged from an old HP printer. From the research I did they look like unipolar motors. I just had a 4 pins from the 430 taken to the inputs of the 2803. The outputs from the 2803 were used to sink current from the pins on the motor. I numbered the pins 1-4 (using a multimeter and PSU to work out the common and the pin order).

 

My code simply had a timer ISR that walked a bit down the 4 pins. To change direction I walked the bit in the opposite way.

 

As I say these were unipolar motors and so I didn't have the polarity to worry about just the sequencing and so the parts needed were minimal.

 

Some pages I found useful were...

http://www.stepperworld.com/Tutorials/pgUnipolarTutorial.htm

http://www.haydonkerk.com/Resources/StepperMotorTheory/tabid/192/Default.aspx

http://www.divms.uiowa.edu/~jones/step/types.html

http://pcbheaven.com/wikipages/How_Stepper_Motors_Work/

 

If you already have motors you want to drive, the first thing you have to do is work out what you have and how they need to be driven. If you have yet to select your motor then you need to work out what matters to you more.

 

I have looked into bipolar and unipolar only so can't comment on the other types.. but I gather that the main differences are :-

 

unipolar is easier on the hardware but has less torque for a given size.

Bipolar needs more complex hardware (H bridge drivers) but has more power for a given size of motor.

 

All the printers Ive smashed to pieces lovingly disassembled have used unipolar motors so I have a little stock of them built up.

 

Hope this helps.

 

Dan

Link to post
Share on other sites

Thanks to all of your recommendations, I have a basic pulse/timer code set up, but i am having trouble with the timing and sequence. Both steppers I have have four wires. I am a little confused at this point. I have a motor driver which can handle a two coil stepper using 4 input pulses, and i have permuted the wiring several times.

 

Edit: I fiddled about some more and was able to get them to rotate, using 10mS delays between toggling each pin on and off, i can speed it up by lowering the delay time as well.

 

#include 

// parts copied from gwdeveloper's motor control code-  Thanks

#define blue BIT0      // P1.0
#define red  BIT1      // P1.1
#define yell BIT2      // P1.2
#define white	BIT4    //P1.4
#define STANDBY BIT6   // P1.6


void DelayMs (unsigned int ms) {
while(ms--) {
	_delay_cycles(1000);
}
}

// button status
unsigned char buttonOn;

void main(void)
{
   WDTCTL = WDTPW + WDTTMSEL + WDTSSEL + WDTIS1;   // WDT as interval timer for debouncing of P1.3 button
                                       // interval mode, low speed clock, /512

  // enable button on P1.3 with interrupt
   P1OUT = BIT3;
   P1REN = BIT3;
   P1IES = BIT3;
   P1IE = BIT3;
   P1IFG = 0;      // clear P1 interrupt flags

   // rest of gpio for motor output

   P1DIR = BIT0 + BIT1 + BIT2 + BIT4 + BIT6;      // set P1 output bits for AIN1, AIN2 and STANDBY
//P1REN |= BIT2 + BIT4;

   P2SEL &= ~(BIT6 + BIT7);            // turn off xtal

   BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0;    
   DCOCTL = 0x00;               // clear DCO bits
   BCSCTL1 = CALBC1_1MHZ;         // Set DCO to calibrated 1MHz
   DCOCTL = CALDCO_1MHZ;
   BCSCTL1 |= XT2OFF + DIVA_0;
   BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1;                                        

   IFG1 &= ~(WDTIFG);            // clear WDT flags
   IE1 |= WDTIE;               // enable WDT interrupt

   buttonOn = 0;
   __enable_interrupt();           // Set global interrupt enable

   //P1OUT = BIT0+BIT1+BIT2+BIT4;

   while (1)
   {
            P1OUT ^= blue; 
            DelayMs (5);            
             P1OUT ^= red;
             DelayMs (5);
           P1OUT ^= blue;
           DelayMs (5);
             P1OUT ^= yell;
             DelayMs (5);
            P1OUT ^= red;
            DelayMs (5);
           P1OUT ^= white;
           DelayMs (5);
           P1OUT ^= yell;
           DelayMs (5);
            P1OUT ^= white;
      }

}

#pragma vector=PORT1_VECTOR
__interrupt void button_push_isr(void)
{
   P1IFG &= ~BIT3;         // clear P1.3 button flag
   P1IE &= ~BIT3;         // clear P1.3 interrupt

   IFG1 &= ~WDTIFG;
   WDTCTL = (WDTCTL & 7) + WDTCNTCL + WDTPW + WDTTMSEL;
   IE1 |= WDTIE;

  // use button to toggle motors on/off for testing
   if (buttonOn == 1) {
       buttonOn = 0;
       P1OUT &= ~STANDBY; // motor driver disable
   }
   else {
       buttonOn = 1;
       P1OUT |= STANDBY; // motor driver enable
   }
}

#pragma vector=WDT_VECTOR
__interrupt void watchdog_isr(void)
{
   IE1 &= ~WDTIE;

   P1IFG &= ~BIT3;         // clear P1.3 button flag
   P1IE |= BIT3;         // re-enable P1.3 interrupt
}

Link to post
Share on other sites

You can find which wire belongs to which coil by shorting them (disconnected from the controller) and turning the roto.

With 1 pair of wire shorted, if the roto cannot be turned, they belong to the same coil.

 

Polarity can then be determined by hooking up to the controller.

 

Looks like a bipolar motor. Can you at "least feel" any step when you power subsequent lines.

[attachment=0]msp430 stepper.gif[/attachment]

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...