Thursday, 4 December 2014

DC Stepper motor Interfacing

Stepper Motor

A stepper motor is a brushless, synchronous electric motor that can divide a full rotation into a large number of steps. When commutated electronically, the motor's position can be controlled precisely, without any feedback mechanism.


Figure 1 : Stepper motor function

Figure 2 : stator winding configuration

HALF STEP 8-STEP SEQUENCE


Code for stepper motor
/* connections
PORT1   P1.24 connected to SW1
PORT1   P1.25connected to SW2
PORT1   P1.26 connected to SW3
Connect PORT1  P1.16  P1.17  P1.18 to the Stepper motor*/

#include<LPC214x.h>     // Define LPC2148 Header File
#include <stdio.h>

#define SW1    24    //SW1 (P1.24)
#define SW2    25    //SW2 (P1.25)
#define SW3    26    //SW3 (P1.26)
#define COIL_A 16    //change the Stepper Motor Port!

void motor_cw(void);
void motor_ccw(void);
void delay(int);
  
unsigned char STEP[] = {0x09, 0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01};// from the above table
void main(void)
{
       unsigned char i = 0;         
   PINSEL2 &= 0xFFFFFFF3;   // P1.16 - P1.31 as GPIO
   IODIR1 = 0x000F0000;        // P1.16 - P1.19 as Output
 
   while(1)                           // Loop forever
   {
           if (!(IOPIN1 & (1<<SW1)))    //  check Switch SW1 ON/OFF
      {
         motor_cw();            // Stepper Motor clockwise
           }

      else if (!(IOPIN1 & (1<<SW2)))   //check for  Switch SW2 ON/OFF
      {
         motor_ccw();     // Stepper Motor anticlockwise
           }

      else if (!(IOPIN1 & (1<<SW3)))  // check for Switch SW3 ON/OFF
 {
             while (i < 12)
             {
            motor_cw (); // clockwise for req. angle
               i++;
            } 
     }
          else
         i = 0;
  }
}

void delay(int n)   // for delay
{
   int i,j;
   for(i=0;i<n;i++)
   {
      for(j=0;j<0x3FF0;j++)
           {;}
   }
}

 void motor_ccw(void)   // anti- clockwise rotation
{
   unsigned int i=0;

   while (STEP[i] != '\0')
   {
      IOSET1 = STEP[i] << COIL_A;
      delay(1);
      IOCLR1 = STEP[i] << COIL_A;
      delay(1);
      i++;
   }
}
  
void motor_cw(void)   // clock wise rotation
{
   int i = 7;
   while (i >= 0)
   {
      IOSET1 = STEP[i] << COIL_A;
      delay(1);
      IOCLR1 = STEP[i] << COIL_A;
      delay(1);
      i--;
   }
}

Connections:
 Output :
/* Don’t forget to keep VBAT 3v */


1 comment: