Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Run Stepper Motor Fast

Status
Not open for further replies.

kanni1303

Full Member level 3
Joined
Jun 29, 2012
Messages
164
Helped
12
Reputation
24
Reaction score
11
Trophy points
1,298
Location
Chennai, Tamil Nadu, India
Activity points
2,708
Hi...
I am in need to run my stepper motor fast... but i don't know how to run it fast... everyone says increase the PPS but i don't know how to increase it...
here is my code... and i am using the PM42L-048 Bipolar stepper motor

Code:
#include<htc.h>
int _XTAL_FREQ=12000000;


int i;
void forl();
void revl();
void form();
void revm();
void forr();
void revr();
void DelayMs(unsigned char delay)
{
unsigned int j;
for(i=0;i<=delay;i++)
{
for(j=0;j<=1000;j++);
}
}

void port_initialize()
{
TRISB=0X00;
PORTB=0x00;
TRISC=0xFF;
TRISD=0X00;
PORTD=0x00;

#define a RC0
#define b RC1


#define l1 RB0
#define l2 RB1
#define l3 RB2
#define l4 RB3
}

void main()

{
port_initialize();
while(1)
{
 if(a>0x00)
 { 
  forl();
 }
 if(b>0x00)
 {
  revl();
 }
}
}

/*
void forl()
{
PORTB=0x08;
DelayMs(150);
PORTB=0x02;
DelayMs(150);
PORTB=0x04;
DelayMs(150);
PORTB=0x01;
DelayMs(150);
}

void revl()
{
PORTB=0x04;
DelayMs(150);
PORTB=0x02;
DelayMs(150);
PORTB=0x08;
DelayMs(150);
PORTB=0x01;
DelayMs(150);
}
*/

void forl()
{
l1=1;
l2=0;
l3=0;
l4=0;
DelayMs(150);
l1=0;
l2=0;
l3=1;
l4=0;
DelayMs(150);
l1=0;
l2=1;
l3=0;
l4=0;
DelayMs(150);
l1=0;
l2=0;
l3=0;
l4=1;
DelayMs(150);
}

void revl()
{
l1=0;
l2=1;
l3=0;
l4=0;
DelayMs(150);
l1=0;
l2=0;
l3=1;
l4=0;
DelayMs(150);
l1=1;
l2=0;
l3=0;
l4=0;
DelayMs(150);
l1=0;
l2=0;
l3=0;
l4=1;
DelayMs(150);
}
pls make me understand
 

The DelayMs(xxx) causes a delay of xxx milliseconds between steps and thats what slows it down. To make it faster you have to make the delay smaller so there is less time between steps.

Rather than editing every occurrence of '150', I suggest you add a line to the top of the code like this:
Code:
#define StepSpeed 150

then change every "DelayMs(150)" into "DelayMs(StepSpeed)". then you only have to change it once in the #define statement for it to change it everywhere else.

Brian.
 

Hello!

Some remarks about your code:

1. You have a bunch of #defines in a function. That's not the way to do it. #defines should be on top
because they are preprocessor directives, not code.

2. Better than a for loop to make a delay, you should use a timer.
The timer interrupt routine is called at regular intervals, and then in your interrupt service routine,
you can step forwards or backwards.

3. Your stepper functions (forl and backl) make 4 steps at a time. This means that if you buy
a 400 steps / rev motor, you will end up with 100 steps per / rev only.

What about this (without timer):


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define BACKWARDS 0
#define DELAY 150
 
uint8 sequence[4] = {0x08, 0x02, 0x04, 0x01};
#define  COUNT 1000
 
void main(void) {
    uint8 current_step = 0;
    uint16 i;
    initialize();
    //    Spin forwards
    while(i < COUNT) {
        PORTB = sequence[current_step];
        current_step++;
        if(current_step > 3) current_step = 0;
        delay(DELAY);
    }
   //    Spin backwards
    while(i < COUNT) {
        PORTB = sequence[current_step];
        current_step--;
        if(current_step > 3) current_step = 3; // (see (1))
        delay(DELAY);
   }
}



(1) Note that this is not a mistake: If you do current_step-- on 0, then you will get 255, and therefore
current_step will be back to 3.

Now if you use a timer interrupt, it's even simpler: you set up an interrupt so that the function timer_isr
will be called.
You should therefore declare current_step as static or global, then:


Code C - [expand]
1
2
3
4
5
void timer_isr(void) {
    current_step += step; // step is +1 or -1, depending on the direction
    if(current_step ==4) current_step = 0;
    if(current_step ==255) current_step = 3;
}



Note that in this case you don't need a delay because the function is called by a timer.

Dora.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top