NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Basic Electronics » Stepper Motor Direction Control
July 11, 2012 by Ralphxyz |
By popular demand lets start a new thread on stepper motor direction control :-) Continued on from my Stepper Motor Programing thread, which has been very informative and interesting. Ralph |
---|---|
July 11, 2012 by Ralphxyz |
Here is my initial "untested" direction code:
I "plan" on using a CW and CCW function to be called with button press. I would like the motor to stay on as long as the button is pressed. Of course turning the motor on with first press and stopping the motor with next press would also be a good option. I need a rainy day to test my code. Ralph |
July 12, 2012 by Ralphxyz |
Well CCW Full Step Mode works!! Ralph |
July 12, 2012 by Ralphxyz |
All three modes work! Ralph |
July 12, 2012 by sask55 |
Hi Ralph At the risk of causing more confusion, I would suggest a new approach to establish directional control in any of the three motor modes in either direction. You could make use of a one dimensional array variable to hold the output pin conditions for each possible step. By doing this you can slightly reduce the amount of code and makes it easy to pick up the sequence in the correct spot after a stop and restart. I also would combine all three motor control modes into one sequence of steps. I will attempt to explain Dimension a variable
Assign each element in that array a value corresponding to the motor steps in your #define statements. Make the order of the array elements the same as the order of the half stepping mode. something like this
set up and int variable to hold the step value that is current.
use the step variable to select what element of steps you want to set. ie what step to send to the controller
by moving thru the elements of this array counting upwards or downwards the motor will move in either of the two directions. set up and int variable to determine the direction the motor will spin.
set up a button to toggle the value of direct to ether 1 or -1 each time you push the button. Each time the button is pushed do
Increment the variable "step" by the value of the variable "direct" to go to the next motor step in the sequence CCW or CW.
By using a loop to repeat the same PORTB = ((PORTB |co_pins) & steps[step] );
line, over and over after each increment of "step" you can step either way true the half step mode and easily start where you left off after a stop. The 8 steps in the half step mode are just a combination of the other two modes overlapping each other. by using only the even numbered elements in the steps array we have full stepping by using only the odd elements we have wave stepping. by adding a few of more lines of code you could set up a button to select the mode you wish to run. Each time you push the button you would toggle a variable to one of three values. the three values would correspond to one of the three modes. By selecting the full step mode you would have the code skip over all the odd numbered elements in the array. by selecting the wave mode you would skip over all of the even elements in the array , half mode would send each element of the array to the controllor I am not sure this is very clear. I will try to set up a nerd kit board next week if I get the time. I will write a sample code to do both the direction control and the speed setting and post it when I can. I make way too many typos and simple syntax errors to post a code without trying to compile and run it first. Darryl |
July 12, 2012 by sask55 |
already I noticed a error
should be
Darryl |
July 12, 2012 by sask55 |
also
should be
|
July 12, 2012 by live4the1 |
What would be the best way to write the code for the stop button that would allow the CCW or CW buttons to start where the motor stopped? Would interrupts need to be used? |
July 12, 2012 by sask55 |
live4the1 No interrupts are not required, they would be better especially for very slow motor speeds. With slow motor speeds the MCU will be doing relatively long delay functions and therefore will not be looping back as often to check the status of the buttons. so there could be some short delays and the MCU may actually miss quick button pushes when the motor in turning very slowly. With the method I have proposed there is really only one button to control CW or CCW. Each time that button is pressed it would reveres the motor direction. This could be done when the motor is stopped or when it is turning. You could use two buttons but they are both kind of doing the same thing ie changing the direction. The way I intend to write the code there will be five push buttons A CW / CCW button that toggles between the two direction with each push A mode button that toggles thru the three possible stepper drive modes A exponentially increasing speed increase button. and an exponentially increasing speed decrease button. The reason I have proposed using a array variable to hold the motor steps is to make restarting in the same place very easy. All the steps would be sequentially numbered in the correct order. There is a number in a variable that holds the number of the step that you are on. It is just a matter of moving up one step or down to turn CW or CCW. Darryl |
July 16, 2012 by live4the1 |
I believe that
should be
because of
I learned this after many tries and finally saw it. |
July 21, 2012 by sask55 |
live4the1 I just got back to this thread again. As you have pointed out I made a number of errors in my code examples earlier in this thread. It is very apparent that I should not try and post bits and pieces of code using only the notepad type text editor. After I have written some code using the programmers notepad I usually kind of run thru the logic and step order in my mind and correct things like variable overflows, variable types required, count errors and things like that. Doing a quick conceptual overview where I am mostly thinking about how to achieve an objective does not result in correct code details. Sorry about the errors. Darryl |
July 21, 2012 by live4the1 |
Don't be sorry, I enjoyed working through it and actually finding the problem. |
Please log in to post a reply.
Did you know that you can adjust most wire strippers to make it easier to strip insulation faster? Learn more...
|