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

Discussion in 'Programmer's Corner' started by icem, Jan 17, 2010.

  1. icem

    Thread Starter New Member

    Nov 15, 2009
    26
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2. #include <16F648A.h>
    3. #use delay(clock=4000000)
    4. #fuses NOWDT,INTRC_IO,NOPUT, NOPROTECT, NOLVP, NOMCLR
    5. struct Motor_pin{
    6. boolean forward:1;
    7. boolean backward:1;
    8. int unused:6;
    9. int motor_pin_output:4;
    10. int unused2:4;
    11. };
    12. int const LUTB[4]={0x03, 0x06, 0x0c, 0x09};
    13. int const LUTF[4]={0x09, 0x0c, 0x06, 0x03};
    14. static struct Motor_pin motor1;
    15. static struct Motor_pin direction;
    16. void forward_stepper(int steps)
    17. {
    18.     int i;
    19.     for(i=0;i<=steps;i++)
    20.      {   motor1.motor_pin_output=LUTF[i%4];
    21.           delay_ms(20);
    22.      }
    23. }          
    24. void backward_stepper(int steps)
    25. {
    26.    int i;
    27.    for(i=steps;i>0;i--)
    28.    {   motor1.motor_pin_output=LUTB[i%4];
    29.        delay_ms(20);
    30.    }
    31. }
    32. void variables()
    33. {
    34.    direction.forward = 1;
    35.    direction.backward = 1;
    36.    direction.motor_pin_output = 0x00;
    37. }
    38. void main ()
    39. {
    40.   int forever=1;
    41.    #byte motor1 = 0x05
    42.    #byte direction = 0x85
    43.  
    44.    variables();
    45.    while(forever)
    46.    {
    47.       if(motor1.forward == 1)
    48.          forward_stepper(100);  
    49.       else
    50.       if (motor1.backward == 1)
    51.            backward_stepper(100);
    52.    }  
    53. }
    54.  
    55.  
    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
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
  3. icem

    Thread Starter New Member

    Nov 15, 2009
    26
    0
    Thanks for your help
     
  4. pojat2175

    New Member

    Feb 28, 2012
    1
    0
    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..
     
Loading...