Saturday, December 16, 2017

3. Stepper motor interface

PartB
3.Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.
Theory:
Stepper motor:Stepper motors are the motors that move in discrete steps or convert electrical pulses into rotatory motion. They have multiple coils(4coils) that are organized in groups called "phases"(stators named as A,B,C and D). By energizing each phase in sequence, the motor will rotate, one step at a time.



fig: Interfacing diagram of Stepper motor with LPC1768 

fig: Wave Drive sequence


//Stepper motor

#include <LPC17xx.h>

int main( void )
{
                int i=0, n=10000,j;
                SystemInit();                        // System initialization
                SystemCoreClockUpdate(); // Clock initialization

                // Function Selection
                LPC_PINCON->PINSEL4 = 0X00000000;   // PORT 2, 12th bit as zero, P2.12 as GPIO

                // set Direction
                LPC_GPIO2->FIODIR = 0x0000000f;        // p2.0 to p2.3 as output

               
                for(j=0;j<50;j++)
                {
                                                                               
                                LPC_GPIO2->FIOSET = 0X00000008;    //set p2.3
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits

                                  LPC_GPIO2->FIOSET = 0X00000004; //set p2.2
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits

                                  LPC_GPIO2->FIOSET = 0X00000002; //set p2.1
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits

                                  LPC_GPIO2->FIOSET = 0X00000001; //set p2.0
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits
                                }                                                                                                                                      }
************************************************************************
To rotate stepper motor in opposite direction (reverse the order 8421 to 1248)
#include <LPC17xx.h>

int main( void )
{
                int i=0, n=10000,j;
                SystemInit();                        // System initialization
                SystemCoreClockUpdate(); // Clock initialization

                // Function Selection
                LPC_PINCON->PINSEL4 = 0X00000000;   // PORT 2, 12th bit as zero, P2.12 as GPIO

                // set Direction
                LPC_GPIO2->FIODIR = 0x0000000f;        // p2.0 to p2.3 as output

               
                for(j=0;j<50;j++)
                {
                                                                               
                                LPC_GPIO2->FIOSET = 0X00000008;    //set p2.3
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits

                                  LPC_GPIO2->FIOSET = 0X00000004; //set p2.2
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits

                                  LPC_GPIO2->FIOSET = 0X00000002; //set p2.1
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits

                                  LPC_GPIO2->FIOSET = 0X00000001; //set p2.0
                                for( i = 0; i < n; i++ );        // delay
                                  LPC_GPIO2->FIOCLR = 0X0000000f; //clear all the bits
                                }                                                                                                                                      }
*********************************************************************************
for Optimal/ Efficient coding make use of Shift operator (this requires less memory and hence high performance)

#include <LPC17xx.H>

void clock_wise(void);
void anti_clock_wise(void);
unsigned long int var1,var2;
unsigned int i=0,j=0,k=0;

int main(void)
{
SystemInit();
SystemCoreClockUpdate();

LPC_PINCON->PINSEL4 = 0x00000000; //P2.0 to P2.3 GPIO
LPC_GPIO2->FIODIR = 0x0000000F; //P2.0 to P2.3 output

while(1)
{
for(j=0;j<50;j++)            //50 times in Clock wise Rotation
clock_wise();

for(k=0;k<65000;k++);         //Delay to show  anti_clock Rotation

for(j=0;j<50;j++)           //50 times in  Anti Clock wise Rotation
anti_clock_wise();

for(k=0;k<65000;k++);         //Delay to show clock Rotation
//End of while(1)
//End of main

void clock_wise(void)
{
var1 = 0x00000001;          //For Clockwise
    for(i=0;i<=3;i++)          //for A B C D Stepping
{
   LPC_GPIO2->FIOCLR =  0X0000000F;
   LPC_GPIO2->FIOSET =  var1;
var1 = var1<<1;         //For Clockwise
        for(k=0;k<15000;k++);  //for step speed variation
    }
}

void anti_clock_wise(void)
{
var1 = 0x0000008;       //For Anticlockwise
    for(i=0;i<=3;i++)        //for A B C D Stepping
    {

    LPC_GPIO2->FIOCLR =  0X0000000F;
LPC_GPIO2->FIOSET =  var1;
var1 = var1>>1;       //For Anticlockwise
        for(k=0;k<15000;k++);  //for step speed variation
    }
}

*********************************************************************************
/*stepper motor rotation based on push button status*/

 #include <LPC17xx.h>

int main( void )
{
  unsigned int key;
  SystemInit();                                      // System initialization
  SystemCoreClockUpdate();              // Clock initialization

// Function Selection
 LPC_PINCON->PINSEL4 = 0X00000000;   // PORT 2 GPIO functionality selection

// set Direction of [P2.0],[P2.1],[P2.2],[P2.3]th pin as output by passing 1
// set Direction of 11th pin as input by passing 0
 LPC_GPIO2->FIODIR = 0x0000000F;

// reading key status continuously
while( 1 )
          {
               Key = LPC_GPIO2->FIOPIN; //reading status of Port2 and storing it in variable key
               key = key & 0x00000800 ;    //bitwise and operation, as I need to check only 11th bit value

                                   if(key == 0x00000800)   //KEY IS NOT PRESSED   if(key & 0x00008000)
                                                 {
                                                     // Do nothing     
                                                 }
                                   else                   
                                                 {                                                               
                                                   for(j=0;j<50;j++)     //50 times in Clock wise Rotation
                                                   clock_wise();
          
                                                  }                                           
                   }                          
}

void clock_wise(void)
{
 var1 = 0x00000001;          //For Clockwise
    for(i=0;i<=3;i++)          //for A B C D Stepping
 {
    LPC_GPIO2->FIOCLR =  0X0000000F;
    LPC_GPIO2->FIOSET =  var1;
 var1 = var1<<1;         //For Clockwise
        for(k=0;k<15000;k++);  //for step speed variation
    }

}
*********************************************************************************


Explanation:




Download:Control Stepper motor (Wave Drive)

5 comments:

  1. How to change the speed of the rotation ,by pressing keys which has speeds associated with them.please help.

    ReplyDelete
    Replies
    1. hello dhyey, you can try by interfacing 2 push button to any of the 2 GPIO port pin in pull up mode and keep on monitoring its status (whether pressed or not), when pressed you can make use of ifelse statement to execute the instruction (passing values 8,4,2,1 to port pins) with different delay values.

      Delete
  2. I also have similar question as "Dhyey Pandya" before. But i have solved the problem now.
    OYOSTEPPER

    ReplyDelete
    Replies
    1. Hello Alan James,thank for leaving a comment and if you could post your solution [code/algorithm] would be great as it can help more people.

      Delete
  3. i have assigned different speeds for different keys pressed. now i want to display the speed of the stepper motor on the lcd display. how should i make it so that i display the speed of stepper motor for the key pressed

    ReplyDelete

lab record

TECHNICAL SPECIFICATIONS of LPC1768 (15-16 of ARMCTXM3) ******************************************************************************...