Problem with stepper motor functioning

Discussion in 'The Projects Forum' started by thavamaran, Mar 27, 2009.

  1. thavamaran

    Thread Starter Active Member

    Feb 21, 2008
    Hi everyone, im currently working on stepper motor, im using UCN5804 as my
    stepper motor driver, then i connect PIC 16f877A as my indexer to run the motor. i have wrote a C program for PIC which im not sure correct or no, im really new
    with C. Here is the coding i wrote for PIC, and i attach the stepper motor datasheet, UCN5804B datasheet, and the schematic as well. please help me out.
    Stepper motor im using is PX243-01AA.

    Here the RB0 is connected to Direction Pin of UCN5804B and RB1 is connected to Step input pin of UCN5804B.

    #include <htc.h>
    #include "delay.h"

    void main (void)
    unsigned char i;
    TRISB = 0x00;

    RB0 = 1;

    for (i=0; i<60; ++i)
    RB1 = 1;
    RB1 = 0;
  2. SgtWookie


    Jul 17, 2007
    When you're posting source code, you should use the CODE blocks to preserve the formatting, like:
    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. #include <delay.h>
    3.  __CONFIG(0x3f71);
    5. void main(void)
    6. {
    7.    unsigned char i;
    8.    TRISB = 0x00;
    10.    RB0 = 1;
    12.    for (i=0; i<60; ++i)
    13.    {
    14.       RB1 = 1;
    15.       DelayUs(1000);
    16.       RB1 = 0;
    17.       DelayUs(1000);
    18.    }
    19. }
    On the "Go Advanced" button page, there is a "#" sign in the menu which inserts the code blocks.

    You had a space between "main" and (void).

    You didn't turn off the watchdog timer, and you aren't taking care of resetting it in your code. Without that, the uC will re-boot every few milliseconds, and it will seem like your code is never getting executed.

    Here's one sample config line that turns off the watchdog timer:

    Try looking at some of the sample code that came with your compiler.

    Also, post a schematic of how you have your uC connected up.
  3. thavamaran

    Thread Starter Active Member

    Feb 21, 2008
    Hi wookie, sorry for the very late reply, was very busy with some other assignments, anyway i already came out with the schematic and a bit changes in the coding.

    Code ( (Unknown Language)):
    2. #include <htc.h>
    3. #include "delay.h"
    4. __CONFIG(0x3f71);
    6. void main (void)
    7. {
    8.     unsigned char i;
    9.     TRISB = 0x00;
    11.     RB1 = 1;
    13.     while ( 1 )
    14.     {
    15.         for (i=0; i<60; ++i)
    16.         {
    17.             RB2 = 1;
    18.             DelayMs(1);
    19.             RB2 = 0;
    20.             DelayMs(1);
    21.         }
    22.     }
    23. }
    anyway as what i have found out, 0x3f71 automatically off the watchdog timer. im not pretty sure about it.

    and i also have attached the schematic. thank you.
  4. SgtWookie


    Jul 17, 2007
    The .BMP file in the ZIP attachment was nearly 2MB! I converted it to a .PNG file using MSPaint, and now it's just 33kb; it's attached.

    You don't show the diodes connected to the stepper motor wires in your schematic - where are they?

    You don't have +4v going to the stepper. Not going to work without it.

    Do you get a signal on RB2 alternating between 5v and 0v?
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    For a 16F877, sample code for one revolution:

    Code ( (Unknown Language)):
    1. [FONT=Arial][SIZE=2][COLOR=#cccccc]1: [/COLOR]#include[SIZE=+1][COLOR=BLUE]<[/COLOR][/SIZE]pic[SIZE=+1][COLOR=BLUE][B].[/B][/COLOR][/SIZE]h[SIZE=+1][COLOR=BLUE]>[/COLOR][/SIZE]                   [COLOR=GREEN][I]//include MCU head file
    2. [COLOR=#eeeeee] 2: [/COLOR][/I][/COLOR]__CONFIG[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][COLOR=PINK]0x1832[/COLOR][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]        
    3. [COLOR=#eeeeee] 3: [/COLOR]
    4. [COLOR=#eeeeee] 4: [/COLOR][COLOR=RED][B]char[/B][/COLOR] step1[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=BROWN]9[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//the first step
    5. [COLOR=#cccccc] 5: [/COLOR][/I][/COLOR][COLOR=RED][B]char[/B][/COLOR] step2[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE]0x0c[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                  [COLOR=GREEN][I]//the second step
    6. [COLOR=#eeeeee] 6: [/COLOR][/I][/COLOR][COLOR=RED][B]char[/B][/COLOR] step3[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=BROWN]6[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//the third step
    7. [COLOR=#eeeeee] 7: [/COLOR][/I][/COLOR][COLOR=RED][B]char[/B][/COLOR] step4[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=BROWN]3[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//the fourth step
    8. [COLOR=#eeeeee] 8: [/COLOR][/I][/COLOR]
    9. [COLOR=#eeeeee] 9: [/COLOR][COLOR=RED][B]void[/B][/COLOR] delay[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//delay function declare
    10. [COLOR=#cccccc]10: [/COLOR][/I][/COLOR]
    11. [COLOR=#eeeeee]11: [/COLOR][COLOR=GREEN][I]//**********************main function*************************
    12. [COLOR=#eeeeee]12: [/COLOR][/I][/COLOR][COLOR=RED][B]void[/B][/COLOR] main[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE]
    13. [COLOR=#eeeeee]13: [/COLOR][SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    14. [COLOR=#eeeeee]14: [/COLOR]  [COLOR=RED][B]char[/B][/COLOR]  loop[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//circle variable declare
    15. [COLOR=#cccccc]15: [/COLOR][/I][/COLOR]  ADCON1[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=PINK]0x7[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//set A PORT all general data PORT
    16. [COLOR=#eeeeee]16: [/COLOR][/I][/COLOR]  TRISA[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=PINK]0X00[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                     [COLOR=GREEN][I]//set A PORT OUTPUT
    17. [COLOR=#eeeeee]17: [/COLOR][/I][/COLOR]  [COLOR=RED][B]for[/B][/COLOR][SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE]loop[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=BROWN]12[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]loop[SIZE=+1][COLOR=BLUE][SIZE=+1][COLOR=BLUE]-[/COLOR][/SIZE][SIZE=+1][COLOR=BLUE]-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE]            [COLOR=GREEN][I]//circle times of stepper motor rotate one time 360/(7.5*4)
    18. [COLOR=#eeeeee]18: [/COLOR][/I][/COLOR]     [SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    19. [COLOR=#eeeeee]19: [/COLOR]         PORTA[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE]step1[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]             [COLOR=GREEN][I]//drive the first step
    20. [COLOR=#cccccc]20: [/COLOR][/I][/COLOR]         delay[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]                 [COLOR=GREEN][I]//delay
    21. [COLOR=#eeeeee]21: [/COLOR][/I][/COLOR]         PORTA[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE]step2[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]             [COLOR=GREEN][I]//drive the second step
    22. [COLOR=#eeeeee]22: [/COLOR][/I][/COLOR]         delay[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]    
    23. [COLOR=#eeeeee]23: [/COLOR]         PORTA[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE]step3[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]             [COLOR=GREEN][I]//drive  the third step
    24. [COLOR=#eeeeee]24: [/COLOR][/I][/COLOR]         delay[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]
    25. [COLOR=#cccccc]25: [/COLOR]         PORTA[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE]step4[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]             [COLOR=GREEN][I]//drive  the fourth step
    26. [COLOR=#eeeeee]26: [/COLOR][/I][/COLOR]         delay[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]
    27. [COLOR=#eeeeee]27: [/COLOR]     [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    28. [COLOR=#eeeeee]28: [/COLOR]  [COLOR=RED][B]while[/B][/COLOR][SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][COLOR=BROWN]1[/COLOR][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE]                        [COLOR=GREEN][I]//stop work
    29. [COLOR=#eeeeee]29: [/COLOR][/I][/COLOR]     [SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    30. [COLOR=#cccccc]30: [/COLOR]         PORTA[SIZE=+1][COLOR=BLUE]=[/COLOR][/SIZE][COLOR=BROWN]0[/COLOR][SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]
    31. [COLOR=#eeeeee]31: [/COLOR]     [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    32. [COLOR=#eeeeee]32: [/COLOR] [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    33. [COLOR=#eeeeee]33: [/COLOR]
    34. [COLOR=#eeeeee]34: [/COLOR][COLOR=GREEN][I]//**********************delay function***********************
    35. [COLOR=#cccccc]35: [/COLOR][/I][/COLOR][COLOR=RED][B]void[/B][/COLOR]  delay[SIZE=+1][COLOR=BLUE][B]([/B][/COLOR][/SIZE][SIZE=+1][COLOR=BLUE][B])[/B][/COLOR][/SIZE]
    36. [COLOR=#eeeeee]36: [/COLOR] [SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    37. [COLOR=#eeeeee]37: [/COLOR]    [COLOR=RED][B]char[/B][/COLOR] i[SIZE=+1][COLOR=BLUE][B],[/B][/COLOR][/SIZE]j[SIZE=+1][COLOR=BLUE][B];[/B][/COLOR][/SIZE]
    39. [COLOR=#eeeeee]39: [/COLOR]       [SIZE=+1][COLOR=BLUE][B]{[/B][/COLOR][/SIZE]
    41. [COLOR=#eeeeee]41: [/COLOR]       [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    42. [COLOR=#eeeeee]42: [/COLOR] [SIZE=+1][COLOR=BLUE][B]}[/B][/COLOR][/SIZE]
    44. [/SIZE][/FONT][FONT=Arial][SIZE=2]
    45. [/SIZE][/FONT]

    Code made pretty by:
  6. SgtWookie


    Jul 17, 2007
    He's just trying to send a clock to RB2 to cause his stepper to move.
    Your code is really for use as a generic driver for a ULN2803A IC or the like.
    It wouldn't do much for our OP, I'm afraid.
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    My bad!

    It outputs 1001, 1100, 0110, 0011, and repeats, for driver transistors on RA0-RA3.

    I'll leave it for reference for the next person searching, i guess.

    All apologies for missing the driver details!
  8. thavamaran

    Thread Starter Active Member

    Feb 21, 2008
    Hi Wookie, im extremely sorry about the size, i was rushing yest with the schematic so that i can at least show you what i did, sorry about that, on the UCN5804B site i built it according to the schematic you did for me. here it is. i haven't check the output pulse with any oscilloscope, but i suspect my crystal got toasted. im fixing a new crystal later on and i will redo again and let you know by tonight, tonight is according to malaysia:cool:.

    thatoneguy, thanks for the coding man, but too bad im only using two output pin from PIC and my driver spec is different from your coding, anyway thanks a lot.
  9. thavamaran

    Thread Starter Active Member

    Feb 21, 2008
    Hey wookie, its working, my stepper is working with the pic, at last got it spinning, thanks a lot man, thanks a lot for your time and thatoneguy, thanks to too.
  10. SgtWookie


    Jul 17, 2007
    Good news! :D
    Yeah, crystals are pretty fragile - they break mighty easy. Dropping them on a hard surface or over-driving them will kill them in a hurry.
  11. thavamaran

    Thread Starter Active Member

    Feb 21, 2008
    Thanks again wookie, just wanted to ask you a question, can i know how you are damn good in all this stuff man? im mesmerized! adioZ!