Ttelmah Guest
|
|
Posted: Tue Mar 28, 2006 4:00 am |
|
|
Depends what you want to do...
There are a lot of 'types' of stepper motors, and different types of drive. The simplest, is what is normally called a 'unipolar' motor, and for this you can have just four driver transistors, one applying power to each coil on the motor. This can then be driven 'single stepping', or 'half stepping' (or with a lot of extra complexity, 'microstepping'). You then have the
question of how to 'time' the movement as well... The code can be as simple as:
Code: |
//For a simple unipolar stepper, connected to portb, using bits
//0..4. Inverted drivers, so motor 'common' connects to +ve, and
//the pins go high to turn 'on' a particular coil.
//Run in halfstep mode
#undef fullstep
//'define' fullstep, for fullstep mode
#ifdef fullstep
const int drivebits[]= { 0x09,0x0C,0x06,0x03 };
#else
const int drivebits[]= { 0x09,0x08,0x0C,0x04,0x06,0x02,0x03,0x01 };
#endif
signed int32 position=0;
void drive(void) {
#ifdef fullstep
output_b(drivebits[position & 3]);
#else
output_b(drivebits[position & 7]);
#endif
}
void MoveTo(signed int32 required) {
signed int32 direction;
if (required<position) direction=-1;
else direction=1;
while (required != position) {
drive();
//Adjust this to give the step 'rate' required
delay_us(3000);
position+=direction;
}
}
main() {
signed int32 place;
//Do your processor initialisation here
while (true) {
//Presumably have a serial 'parser' or something similar, to get
//the required value into 'place'
MoveTo(place);
}
}
|
Now, this excludes the port initialisation needed for whatever processor you are using, and the code to actually 'get' the position to move the motor to. You put a numeric value into 'place', call 'MoveTo', and the motor will step as required to get there. The step rate is controlled by the delay in 'MoveTo', and for a really sophisticated system, would require 'ramping' to accelerate/decelerate the motor. The value given (3mSc), should work well with most motors in half stepping mode.
You can 'define' 'fullstep', and then the code will full step instead of half stepping.
Hope this gives you enough to start.
Best Wishes |
|