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 CCS Technical Support

Stepper Motors Library

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
marcoscab1166



Joined: 19 May 2019
Posts: 6

View user's profile Send private message Send e-mail Visit poster's website

Stepper Motors Library
PostPosted: Sun May 19, 2019 7:15 am     Reply with quote

Hi, I would like to share a library I've been working on. It has some advantages over others that I've looked on internet.
The main one, you can freely define the pins that you will use to control the motor.
The library works with a single function used to move the motor, and a matrix of int1 variables to generate the steps values.
Any question or suggestion will be well received.
Here is the link to the library
https://github.com/marcos1166/Stepper
Enjoy Wink

Code:

///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                            STEPPER MOTOR CONTROL                          //
//                          CREATED BY MARCOS CABRERA                        //
//                                04 APRIL 2019                              //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                               HOW TO USE IT:                              //
//                                                                           //
//  The library works basically by loading a int1 variable                   //
//  into the pins that drive the motor.                                      //
//  Its possible to work the stepper in FULL STEP, HALF STEP,                //
//  and WAVE DRIVE.                                                          //
//  The pins used to control the motor can be easily changed,                //
//  and also the matrix used to create the routines.                          //
//                                                                           //
//  The init_stepper() function just puts it on a right                      //
//  angle and set the default values for some variables,                     //
//  but its not 100% necessary.                                              //
//                                                                           //
//  The value for the pins on each step are stored                           //
//  in an array of int1 variables called 'matrix'                            //
//  and they can be easily accessed                                          //
//  by the following equation:                                               //
//                                                                           //
//   PIN STATE = MODE + DIRECTION + ( STEP * 4 ) + PIN                       //
//                                                                           //
//  On this equation, MODE and DIRECTION points                              //
//  where starts the configuration. STEP * 4 is                              //
//  used to navigate on that configuration. And                              //
//  the number of the pin access the exact value.                            //
//                                                                           //
//  For example, if I'm working in FULL STEP and                              //
//  ANTICLOCK direction, it means that the config                            //
//  will start at 0+12=12. If its the 2nd step that                          //
//  I'm on (using 0 to 3), it'll add 1*4=4.                                   //
//  That will let me with a total of 16,                                     //
//  on the start of this: '0,0,1,1,'.                                        //
//  So i just add the pin that i wanna get the value                         //
//  (again using 0 to 3), for example 1, and that way                        //
//  i get the exact value for that pin on that step                          //
//  for that configuration and direction.                                    //
//  Easy, isn't it? ;)                                                       //
//                                                                           //
//  Just a few more things.                                                  //
//  The pins can be changed on the 'stepper_pins'                            //
//  variable. Any digital pin can be used.                                   //
//  The int1 called inverted its to do exactly that,                         //
//  invert the state of the pins. Just in case that                          //
//  you use a driver like ULN2803.                                           //
//  Turn it to 0 to use it in normal states.                                 //
//                                                                           //
//  The speed set use a simple delay_ms.                                     //
//  As a recommendation, keep it between 2 and 10 ms.                         //
//  But it depends on what motor driver you use.                             //
//                                                                           //
//  Now thats all, enjoy it, and if you find                                 //   
//  something to improve, feel free to share it.                             //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#define FULL_STEP  0
#define HALF_STEP  28
#define WAVE_DRIVE 88
#DEFINE CLOCKWISE  0
#define ANTICLOCK  12

int1 inverted = 1;
int step=0;
int speed = 10;
int mode = 0;
int32 stepper_pins[4] = PIN_C4, PIN_C5, PIN_C6, PIN_C7;

int1 matrix[116]=  1,1,0,0,   //FULL STEP    0
                   0,1,1,0,
                   0,0,1,1,
                   1,0,0,1,   //shared row for both directions 12
                   0,0,1,1,
                   0,1,1,0,
                   1,1,0,0,   //END OF FULL STEP
                   1,0,0,0,   //INIT HALF STEP     28
                   1,1,0,0,
                   0,1,0,0,
                   0,1,1,0,
                   0,0,1,0,
                   0,0,1,1,
                   0,0,0,1,
                   1,0,0,1,   //shared row for both directions 56
                   0,0,0,1,
                   0,0,1,1,
                   0,0,1,0,
                   0,1,1,0,
                   0,1,0,0,
                   1,1,0,0,
                   1,0,0,0,   //END OF HALF STEP
                   1,0,0,0,   //WAVE DRIVE STEP    88
                   0,1,0,0,
                   0,0,1,0,
                   0,0,0,1,   //shared row for both directions 100
                   0,0,1,0,
                   0,1,0,0,
                   1,0,0,0;   //                   112
               
               
void init_stepper(){
   output_bit(stepper_pins[0], 1);
   output_bit(stepper_pins[1], 0);
   step=0;
   speed = 2;
   delay_ms(10);
}

void move_stepper(int direction, int16 steps_count){
      int index;
      int16 counter1, counter2;
      for( counter1=1; counter1<=steps_count; counter1++ ){
         for(counter2=0;counter2<4;counter2++){
            if((mode==HALF_STEP)&&(direction==12)){
               direction=28;
            }
            index=mode+direction+(step*4)+counter2;
            if(inverted){
               output_bit( stepper_pins[counter2], !matrix[index] );
            }  else{
               output_bit( stepper_pins[counter2], matrix[index] );
            }
         }
         if(mode==HALF_STEP){
            if(step==7){
               step=0;
            }  else{
               step++;
            }
         }  else{
            if(step==3){
               step=0;
            }  else{
               step++;
            }
         }
         delay_ms( speed );
      }
}

tinley



Joined: 09 May 2006
Posts: 67

View user's profile Send private message Visit poster's website

Thank you, minor bug
PostPosted: Fri Dec 20, 2019 10:19 am     Reply with quote

Thank you for this code. Works really well for int8 steps, but the following line should be changed for more than 255 steps:

int16 counter1, counter2;

I bought a PIC Clicker and STEPPER 3 Click. Plugged in to PC and stepper motor. Using CCS and the MicroBootloader. Thanks to this code I had project working in less than 30 minutes.

=======================
Changed both variables to 'int16' in posted code.

-Forum Moderator
=======================
marcoscab1166



Joined: 19 May 2019
Posts: 6

View user's profile Send private message Send e-mail Visit poster's website

Re: Thank you, minor bug
PostPosted: Wed Dec 25, 2019 8:51 pm     Reply with quote

tinley wrote:
Thank you for this code. Works really well for int8 steps, but the following line should be changed for more than 255 steps:

int16 counter1, counter2;

I bought a PIC Clicker and STEPPER 3 Click. Plugged in to PC and stepper motor. Using CCS and the MicroBootloader. Thanks to this code I had project working in less than 30 minutes.


Thanks for the reply and suggestion. I updated the code on Github adding those changes.
ConHarper



Joined: 20 Jan 2020
Posts: 1
Location: Banned - spammer

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 20, 2020 2:57 pm     Reply with quote

Thanks for sharing! It works awesome.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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