Stepper motor program

Discussion in 'Programmer's Corner' started by JK-FlipFlop, Jul 8, 2010.

  1. JK-FlipFlop

    Thread Starter Member

    Jul 5, 2010
    111
    0
    I made this program to run a stepper motor by uC PIC18f4520.
    I need you to find a mistake in this program:

    #include <hitech.h>
    #include <pic18.h>
    void DELAY(void); /*Spend time function*/
    int sequence[8] = {0xc, 0x4, 0x6, 0x2, 0x3, 0x1, 0x9, 0x8}; /* running sequence for motor (half step)*/
    int N; /*number of sequence*/
    main()
    {
    TRISC=0xff;
    TRISD=0x00;
    N=0;
    while((PORTC&0x2)==0x2)
    {
    for(;((PORTC&0x1)==0x1);N++)
    {
    PORTD = sequence[N];
    DELAY();
    if(N=8)
    {
    N=0;
    }
    }
    for(;((PORTC&0x1)==0x0);N--)
    {
    PORTD = sequence[N];
    DELAY();
    if(N=0)
    {
    N=8;
    }
    }
    }
    }

    void DELAY(void)
    {
    unsigned int i;
    for(i=0;i<0x7fff;i++);
    }

    if you find any thing wrong type it.

    thanks.
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    you need to use the code tags to make the code more readable:

    Code ( (Unknown Language)):
    1. #include <hitech.h>
    2. #include <pic18.h>
    3. void DELAY(void); /*Spend time function*/
    4. int sequence[8] = {0xc, 0x4, 0x6, 0x2, 0x3, 0x1, 0x9, 0x8}; /* running  sequence for motor (half step)*/
    5. int N; /*number of sequence*/
    6. main()
    7. {
    8. TRISC=0xff;
    9. TRISD=0x00;
    10. N=0;
    11. while((PORTC&0x2)==0x2)
    12. {
    13. for(;((PORTC&0x1)==0x1);N++)
    14. {
    15. PORTD = sequence[N];
    16. DELAY();
    17. if(N=8)
    18. {
    19. N=0;
    20. }
    21. }
    22. for(;((PORTC&0x1)==0x0);N--)
    23. {
    24. PORTD = sequence[N];
    25. DELAY();
    26. if(N=0)
    27. {
    28. N=8;
    29. }
    30. }
    31. }
    32. }
    33.  
    34. void DELAY(void)
    35. {
    36. unsigned int i;
    37. for(i=0;i<0x7fff;i++);
    38. }

    Where is the configuration for the pic?
    Can you attach a circuit schematic to see if code matches hardware?


    B. Morse
     
  3. JK-FlipFlop

    Thread Starter Member

    Jul 5, 2010
    111
    0
    The code only need to show the number of the sequence on PORT D and than by transistors driver going to the motor.

    example of working code:

    #include <hitech.h>
    #include <pic18.h>
    void T(void);
    main()
    {
    TRISC=0xff;
    TRISD=0x00;
    while((PORTC&0x2)==0x2)
    {
    while((PORTC&0x1)==0x1)
    {
    PORTD=0xc;
    T();
    PORTD=0x4;
    T();
    PORTD=0x6;
    T();
    PORTD=0x2;
    T();
    PORTD=0x3;
    T();
    PORTD=0x1;
    T();
    PORTD=0x9;
    T();
    PORTD=0x8;
    T();
    }

    while((PORTC&0x1)==0x0)
    {
    PORTD=0xc;
    T();
    PORTD=0x8;
    T();
    PORTD=0x9;
    T();
    PORTD=0x1;
    T();
    PORTD=0x3;
    T();
    PORTD=0x2;
    T();
    PORTD=0x6;
    T();
    PORTD=0x4;
    T();
    }
    }
    }

    void T(void)
    {
    while((PORTC&0x2)!=0x2);
    if((PORTC&0x2)==0x2)
    {
    unsigned int i;
    for(i=0;i<0x7fff;i++);
    }
    }
     
    Last edited: Jul 8, 2010
  4. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Well, I guess you do not need any help then......
     
  5. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    When you count up, you count from 0 to 7, testing for 8 as the overflow, setting N=0 in that case.

    So counting down, don't you want to count from 7 to 0, testing for -1 as the underflow, then set N=7?
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    How about this?

    Code ( (Unknown Language)):
    1.  
    2.   byte incr=0, n=0;
    3.  
    4.   while(1)
    5.   {
    6.     if (portc & 2)
    7.       incr = 1;
    8.     else if (portc & 1)
    9.       incr = 255;
    10.     else
    11.       incr = 0;
    12.  
    13.     n += incr;
    14.     portd = sequence[n & 7];
    15.  
    16.     delay();
    17.   }
    18.  
     
Loading...