jean28 0 Posted March 16, 2014 Share Posted March 16, 2014 Hello guys, I finally got my stepper motor to move. Now, I would like to know how to make it move as fast as it can move. I tried the following code, which basically turns on each pin (one at a time): #include <msp430.h> volatile unsigned int i; // volatile to prevent optimization void init() { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P1DIR |= 0x3C; // Set direction of pins. } void delay(int val) { i = val; do i--; while(i != 0); } void counterClockwise(int speed) { P1OUT = 0x04; // Toggle P1.2 delay(speed); P1OUT = 0x08; // Toggle P1.3 delay(speed); P1OUT = 0x10; // Toggle P1.4 delay(speed); P1OUT = 0x20; // Toggle P1.5 delay(speed); } void clockwise(int speed) { P1OUT = 0x20; // Toggle P1.5 delay(speed); P1OUT = 0x10; // Toggle P1.4 delay(speed); P1OUT = 0x08; // Toggle P1.3 delay(speed); P1OUT = 0x04; // Toggle P1.2 delay(speed); } int main(void) { init(); while(1) { clockwise(169); } return 0; } The motor moves successfully, but I have a feeling it can move faster. I set the delay to 169 because that's the lowest it could get before not moving at all. Is there a way to make the motor move faster? What am I doing wrong? Thanks! Quote Link to post Share on other sites
enl 227 Posted March 16, 2014 Share Posted March 16, 2014 As mentioned in previous thread, you need to ramp up the step rate. The initial step rate that you can get away with goes down as the inertia of the load goes up. The maximum step rate goes down as the friction loss goes up. Warning: Gross oversimplification to follow: For example, the bare motor will probably go faster than you have it now by reducing the delay a little at a time, but, with no load, it isn't likely to go a lot faster. The unloaded rate is limited by the inductance of the coils, which limits the rate of change of the current -- ie: the maximum switching speed -- and friction in the motor. You might get twice the speed you have now. You might get almost no increase. Depends on what is limiting things now: friction or inductance. If you put a good size flywheel on the shaft, the motor probably won't run at all with your current code. You will need to lengthen the delay a good bit. Every few steps, the delay gets shortened a little (by a constant is not the best way, as it is reciprocal to rotational speed, but it does work within limits) until you reach the desired speed. When stopping, if you want to maintain the step synchronization, you reverse the process. If you don't care about maintaining step sync, just stop driving the thing and it will coast down, but it will take longer than a controlled stop. As to your code.... I would use a decent delay tool. An empty loop is difficult to reproduce. delay_cycles() is tied to the CPU clock. Better, use a timer interrupt. Build an array for your output states, and on each timer interrupt, increment or decrement a counter, and use the counter low order bits to pick the output state. This has the added benefit of giving a natural count of position. To speed up, you make the interrupt more frequent, to slow down, less so. A state variable of -1, 0, or 1 determine direction or stop. bluehash and jean28 2 Quote Link to post Share on other sites
jean28 0 Posted March 17, 2014 Author Share Posted March 17, 2014 As mentioned in previous thread, you need to ramp up the step rate. The initial step rate that you can get away with goes down as the inertia of the load goes up. The maximum step rate goes down as the friction loss goes up. Warning: Gross oversimplification to follow: For example, the bare motor will probably go faster than you have it now by reducing the delay a little at a time, but, with no load, it isn't likely to go a lot faster. The unloaded rate is limited by the inductance of the coils, which limits the rate of change of the current -- ie: the maximum switching speed -- and friction in the motor. You might get twice the speed you have now. You might get almost no increase. Depends on what is limiting things now: friction or inductance. If you put a good size flywheel on the shaft, the motor probably won't run at all with your current code. You will need to lengthen the delay a good bit. Every few steps, the delay gets shortened a little (by a constant is not the best way, as it is reciprocal to rotational speed, but it does work within limits) until you reach the desired speed. When stopping, if you want to maintain the step synchronization, you reverse the process. If you don't care about maintaining step sync, just stop driving the thing and it will coast down, but it will take longer than a controlled stop. As to your code.... I would use a decent delay tool. An empty loop is difficult to reproduce. delay_cycles() is tied to the CPU clock. Better, use a timer interrupt. Build an array for your output states, and on each timer interrupt, increment or decrement a counter, and use the counter low order bits to pick the output state. This has the added benefit of giving a natural count of position. To speed up, you make the interrupt more frequent, to slow down, less so. A state variable of -1, 0, or 1 determine direction or stop. Thanks! I will try it now using the clock as the delay. Let's see how it works out. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.