Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Running a stepper MOTOR and STOPPING IT

Status
Not open for further replies.

gkunwar

New Member
Hi, I have the a stepper motor that runs with the following program but doesn't stop. For example, I want it to stop after 1 full rotation or 10 full rotation and then STOP. How does one do that? HELP


// Program to provide simple square wave

#include <pic.h>
#include <math.h>
#include <delay.h>

__CONFIG(HS);

// Define Terms

#define PWM_output1 RD7
#define PWM_output2 RD6
#define PWM_output3 RD5
#define PWM_output4 RD4
#define CW 1
#define CCW 0


// Routine to obtain PWM for Port D's

int main (void)
{

int i;

for(i=0;i<=1;++i)

{

PWM_output1=1;
DelayUs(1);
PWM_output1=0;
DelayUs(1);

PWM_output2=1;
DelayUs(1);
PWM_output2=0;
DelayUs(1);

PWM_output3=1;
DelayUs(1);
PWM_output3=0;
DelayUs(1);

PWM_output4=1;
DelayUs(1);
PWM_output4=0;
DelayUs(1);

// Setting the ports

TRISD = 0x00; //sets PORTD for output
PORTD = 0x00; //clear PORTD
}
}
 
There are a couple of problems with your code, the main one being what the code will do after it runs main, it can't return to DOS and so continues through memory until it gets back to zero and runs it again. The other problem is setting up the port after you use it.

Try,
Code:
int main (void) 
{     
    int i;    
    // Setting the ports
    TRISD = 0x00; //sets PORTD for output
    PORTD = 0x00; //clear PORTD

    for(i=0;i<=1;++i)    
    {        
        PWM_output1=1;
        DelayUs(1);
        PWM_output1=0;
        DelayUs(1);
        
        PWM_output2=1;
        DelayUs(1);
        PWM_output2=0;
        DelayUs(1);
        
        PWM_output3=1;
        DelayUs(1);
        PWM_output3=0;
        DelayUs(1);
        
        PWM_output4=1;
        DelayUs(1);
        PWM_output4=0;
        DelayUs(1);    
    }
    while(1);	//wait forever
}

Mike.
 
Yeah, using the while loop at the end definitely doesn't let the stepper motor move but I was wondering if one could move the motor and stop after certain full rotation. For example, run the motor and ask it to stop after 10 full rotations.
Is that possible?
 
Thanks for both your inputs, that worked well but since I need to control 8 motors with a PIC, I was wondering how to use "for" loop for each output ?

For example, the below code uses "for" loop to control all the output ports. How do you set up different "for" loop for each output, so , I could change the rotation ?

// Routine to obtain PWM for Port D's

int main (void)
{
long i;

// Setting the ports

TRISD = 0x00; //sets PORTD for output
PORTD = 0x00; //clear PORTD

for(i=0;i<=85000*3;i++)
{
PWM_output1=1;
DelayUs(1);
PWM_output1=0;
DelayUs(1);

PWM_output2=1;
DelayUs(1);
PWM_output2=0;
DelayUs(1);

PWM_output3=1;
DelayUs(1);
PWM_output3=0;
DelayUs(1);

PWM_output4=1;
DelayUs(1);
PWM_output4=0;
DelayUs(1);
}

while(1); //wait forever
}


Thanks!
 
can't "ask" the motor, the motor in this case doesn't know how far it's turned. You can count how many pulses it's gone through thus knowing how far it's turned, as long as it hasn't screwed up physically some how. For this solution, you have to figure out or know how many steps you have per revolution.
Or you could attach an encoder that would provide you feedback on how far the motor has gone through its revolution(s).

Also, see how Pommie use the nice code formatting tabs above and how nice it is to read that code? You should get used to doing the same, more likely someone will actually look at your code if it looks nicer.
 
The following lines should give you some ideas. It is not finished code.
Code:
#define MOTOR_1_FINISH 87000
#define MOTOR_2_FINISH 992470

for(i=0;i<=85000*3;i++) 
{
if ( i < MOTOR_1_FINISH )[INDENT]{
PWM_output1=1;
DelayUs(1);
PWM_output1=0;
DelayUs(1);
}[/INDENT]if ( i < MOTOR_2_FINISH )[indent]{
PWM_output2=1;
DelayUs(1);
PWM_output2=0;
DelayUs(1);
}[/INDENT]
 
Note that my code has the serious disadvantage of sending brief pulses to each stepper, and running more slowly as steppers are added.

Running all steppers independently is probably going to require a timer interrupt and much more knowledge of logic and C.
 
AFAIC you are controlling the motors via some driver so you send pulse to step a motor (you are not directly controlling the coils) ... the simplest wait to do it is something like

Code:
// some initialisation should be added here (config, tris etc)

// How many pulses per full revolution
#define REV 200

void pulseM1(){
  Motor1=1;
  DelayUs(1);
  Motor1=0;
}

void pulseM2(){
  Motor2=1;
  DelayUs(1);
  Motor2=0;
}

void pulseM3(){
  Motor3=1;
  DelayUs(1);
  Motor3=0;
}

void pulseM4(){
  Motor4=1;
  DelayUs(1);
  Motor4=0;
}

void revEND(){
   //do something after each revolution
   //for example check if B1 is high (do not forget to initialise TRISB)
   while(1){
      if (RB1) break;
      DelayUs(1);
   }
}

void main()
  int i;
  while(1){
     for (i=0;i<REV;i++){
       pulseM1();
       pulseM2();
       pulseM3();
       pulseM4();
     }
     revEnd(); //executed after the full revolution
  }
}

EDIT: depending on the C compiler used, the pulseM? functions should be made "inline" to remove the extra overhead of jump/return
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top