CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

stepper motor

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Don
Guest







stepper motor
PostPosted: Mon Mar 27, 2006 8:12 pm     Reply with quote

Could any one help me with code for stepper motor control??
Ttelmah
Guest







PostPosted: Tue Mar 28, 2006 4:00 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group