Help on c programming, using PIC16F648A to control a RS stepper motor

Thread Starter

icem

Joined Nov 15, 2009
26
Im basically writing a c code to control a rs 440-420 stepper motor using a PIC16F648A. The task is to have two switch which will control the direction of the motor (clockwise/anticlockwise).

i have written all of the code, the genernal idea is when the clockwise switch is 1(active high) the motor will turn clockwise and when the anticlockwise is high it will go anticlockwise and when both are zero the motor will not turn.

the code works up to when i want the motor to turn anticlockwise it goes clockwise but when i want it to go clockwise it turns clockwise. theys no problems with the circuit.

This is my c code.

Rich (BB code):
#include <16F648A.h>
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO,NOPUT, NOPROTECT, NOLVP, NOMCLR
struct Motor_pin{
boolean forward:1;
boolean backward:1;
int unused:6;
int motor_pin_output:4;
int unused2:4;
};
int const LUTB[4]={0x03, 0x06, 0x0c, 0x09};
int const LUTF[4]={0x09, 0x0c, 0x06, 0x03};
static struct Motor_pin motor1;
static struct Motor_pin direction;
void forward_stepper(int steps)
{
    int i;
    for(i=0;i<=steps;i++)
     {   motor1.motor_pin_output=LUTF[i%4];
          delay_ms(20);
     }
}           
void backward_stepper(int steps)
{
   int i;
   for(i=steps;i>0;i--)
   {   motor1.motor_pin_output=LUTB[i%4];
       delay_ms(20);
   }
}
void variables()
{
   direction.forward = 1;
   direction.backward = 1;
   direction.motor_pin_output = 0x00;
}
void main ()
{
  int forever=1;
   #byte motor1 = 0x05
   #byte direction = 0x85
  
   variables();
   while(forever)
   {
      if(motor1.forward == 1)
         forward_stepper(100);  
      else 
      if (motor1.backward == 1)
           backward_stepper(100);
   }   
}
i have attached the datasheets for the motor and the PIC device.
If u can help in any way i really appreciate it.
Thank you
 

Attachments

THE_RB

Joined Feb 11, 2008
5,438
You have 2 arrays for the different directions, and you have 2 code functions for different directions, one does ++ and one does --.

So the 2 code functions are opposite and cancel the opposite arrays.

If you just make both functions use LUTF[] it should work fine, bit it is still not coded very well.

You should change it to use a global variable STEP which always = the step the motor is at, and use that variable in both forward and backward functions. And just use one array for the 4 step sequence.
 

pojat2175

Joined Feb 28, 2012
1
hi icem,

i want to make the project same as you..you said that there is no problem with the circuit..unfortunately i didnt have the circuit...and i need it as soon as possible...can you help me by giving the circuit diagram?

thank you for your cooperation..
 
Top