Saturday, December 11, 2010

Stepper Motor Programming

Full Step Sequence:
I am using PIC16F628A with uln2003 to drive unipolar stepper motor. Adjusting the delay will increase or decrease the speed of the motor. Here just for demonstration i have taken some delay, you can change it as you want. If you want to change the direction of stepper motor, just reverse the step sequence (means 9,3,6, and C etc). 

Code:

void delay();

void main(){
   TRISB=0;
           while(1){

                PORTB = 0x0C;
                delay();
                PORTB = 0x06;
                delay();
                PORTB = 0x03;
                delay();
                PORTB = 0x09;
                delay();
        }
}

void delay(){
        int i;
        for(i=0;i<3000;i++);
                
}

Schematic:
PIC16F628A Stepper Motor Driver
Half Step Sequence:
Delay is same and main routine is like:


Code:
void main(){
      while(1){



                PORTB = 0x08;
                delay();
                PORTB = 0x0C;
                delay();
                PORTB = 0x04;
                delay();
                PORTB = 0x06;
                delay();
                PORTB = 0x02;
                delay();
                PORTB = 0x03;
                delay();
                PORTB= 0x01;
                delay();
                PORTB= 0x09;
                delay();
}
      }


2-Wire Connection:
Delay routine is same. Schematic is here.


Code:
void main(){
        while(1){
                PORTB = 0x03;
                delay();
                PORTB = 0x01;
                delay();
                PORTB = 0x00;
                delay();
                PORTB = 0x02;
                delay();
        }
} 

9 comments:

  1. hi,
    thank god i found ur blog. im desperately wanna know how you do this. can you explain why you assigned a specified hex value to port B? how can i know what hex value that i have to assign?

    ReplyDelete
  2. how many wire attached to your stepper motor actually? from the schematic, it looks like there are 6 wires. is it?

    ReplyDelete
  3. where should i connect the wire of the stepper motor actually? now im confuse!

    ReplyDelete
  4. I think you are not paying attention. These values are reflecting the mode of stepper motor.
    Read this again.
    http://electronicseverywhere.blogspot.com/2010/12/stepper-motor-stepping-modes.html

    yes i have 6 wire stepper motor.
    2 of them are for vcc and rest of them are for phase selection(or mode selection).

    ReplyDelete
  5. hurm sorry. i miss that part. what do u mean by r1(2) value= 5 and bc value =9 ? it is the vcc for the pic and driver right?

    bout the mode of the stepper motor, do they have different configuration for different stepper motor?

    what type of header do u have? i mean what should i write at the #include xxxx?

    sorry to ask the stupid ques again. i really need to know. i am sorry...

    ReplyDelete
  6. These are the 5v and 9v; 5v for mc and 9v for my motor (as my motor required 9v).

    These are different modes for stepper motors but all these are applicable on others as well; i am talking about 6 wire motors, i did not try 8 wire motors yet.

    This is just the portion of the code, it depends on the type of compiler you are using. Check my programming tutorials for more info.
    http://electronicseverywhere.blogspot.com/search/label/PIC%20programming%20in%20C

    Currently me using MPLAB and HITECH.

    ReplyDelete
  7. the input to my microcontroller, is it always has to be into RA5/MCLR??

    ReplyDelete
  8. No; MCLR is used to reset the pic if low signal is applied that is why you need to pull-up this pin.
    also it only applies when you are using MCLR feature instead of RA5 as the i/o pin, read the config bits in the datasheet for more detail.

    ReplyDelete