ss_blog_claim=726e70d7c87c20ae33aa7a61f06eb8aa

Sunday, June 1, 2008

code for driving stepper motor in mikro c and ccs c compiler

code in mikroc for wave drive, half step and high torque stepping

//wave drive mode

main()
{
TRISB=0x00;
PORTB=0x00;

while(1)
{

Delay_ms(3);
PORTB=0b00001000;
Delay_ms(3);
PORTB=0b00000100;
Delay_ms(3);
PORTB=0b00000010;
Delay_ms(3);
PORTB=0b00000001;
}
}

//half step mode

main()
{
TRISB=0x00;
PORTB=0x00;

while(1)
{
Delay_ms(2);
PORTB=0b00001000;
Delay_ms(2);
PORTB=0b00001100;

Delay_ms(2);
PORTB=0b00000100;
Delay_ms(2);
PORTB=0b00000110;

Delay_ms(2);
PORTB=0b00000010;
Delay_ms(2);
PORTB=0b00000011;

Delay_ms(2);
PORTB=0b00000001;
Delay_ms(2);
PORTB=0b00001001;
}
}

//high torque stepping mode

main()
{
TRISB=0x00;
PORTB=0x00;

while(1)
{
Delay_ms(2);
PORTB=0b00001100;
Delay_ms(2);
PORTB=0b00000110;
Delay_ms(2);
PORTB=0b00000011;
Delay_ms(2);
PORTB=0b00001001;
}
}

code in ccs c compiler for driving stepper in wave drive mode

#include<16f877a.h> //including the PIC16F877A header file
#fuses HS,NOWDT,NOPROTECT,NOLVP //setting the fuses(please be careful in setting them)
#use delay(clock=20000000) //setting the clock frequency
#byte PORTB=0x06 //initializing the ports(please refer the memory organization in PIC datasheet)
#byte PORTC=0x07

int run( int r);
int temp;

int run( int r)
{
switch(r)
{
case 1:{
PORTb=0b00000001;
return(2);
}
case 2:{
PORTb=0b00000010;
return(3);
}
case 3:{
PORTb=0b00000100;
return(4);
}
case 4:{
PORTb=0b00001000;
return(1);
}
}
}

VOID main()
{
SET_TRIS_b(0X00);
PORTb=0x00;
temp=1;

while(1)
{
Delay_us(1000);
temp=run(temp);
}
}

i believe the following code snippet will be helpful to you in controlling stepper rotation

4 comments:

muthu_giri said...

i made tip122 driver circuit for stepper motor.but when it interface with pic16f877,motor can not be activate.at the same time when i checking motor seperately,condition will be good.what is the reason?

Unknown said...

Hi,I used the same circuit shown above to run a unipolar stepper of 1.8 deg step rated at 12V/0.6 Amp per phase. I used ATMega32 microcontroller to give signals.but the problem is motor is heatted up very quickly.so pls send me the reason for the same and any other circuit. pls reply at cutputhit@gmail.com.. Thanks in advance...

gablu said...

Hi ,

This is T A Bharathwaj, I believe I too worked on this project with u :P EVen though i dint help u pretty much kindly add my name :) hahah Anyways was just kidding .Informative blog keep it up !!!

Urs friendly
Bharathwaj

Unknown said...

i have 28byj-48 stepper motor . but it is just vibrating ,when change the delay to 30 it vibrates loud but not rotating , please help :(

thanks.

used this code:

void main()
{
TRISB=0x00;
PORTB=0x00;

while(1)
{
Delay_ms(36);
PORTB=0b00001100;
Delay_ms(36);
PORTB=0b00000110;
Delay_ms(36);
PORTB=0b00000011;
Delay_ms(36);
PORTB=0b00001001;
}
}