Shop OBEX P1 Docs P2 Docs Learn Events
C programming a stepper motor — Parallax Forums

C programming a stepper motor

RbolickRbolick Posts: 1
edited 2014-09-14 09:19 in Learn with BlocklyProp
I am trying to teach students as how to connect a unipolar stepper motor (Mouser Part # 619-27964) to the Parallax Propeller Activity board using a ULN2003 darlington chip. Once connected in the breadboard I need help in programming it to step at various torques. The information on the torques are on the steppers data sheet, but I am not a very good C programmer.

Can someone help with a very simple C program( which libraries are needed and where to obtain them, and where to put then on my computer) that is documented so I can follow and make alterations in torques?
Thanks

Comments

  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-10-23 19:37
    I think I have some really simple C code for stepper motors that I did some years ago. IIRC, I used the program to communicate to PBasic and a Stamp through C. I know for a fact I have this for VB. Give me a bit to search my Server database for my older archived projects. I can't guarantee that any of it is documented but it may be a start.

    Edit: I am not sure if the attached project will help you out or not. It is in VC# and when I tried to build I got some usb errors. The file says it was created August of this year but I know that is wrong since it has been at least a couple years since I worked on it. I will try to find my notes on what I was doing and trying to accomplish so I can help you out more.
  • mnastasimnastasi Posts: 9
    edited 2014-09-13 21:18
    any progress on parallax or manufacturer release little step c code ? thanks for your help
  • dgatelydgately Posts: 1,631
    edited 2014-09-14 09:19
    mnastasi wrote: »
    any progress on parallax or manufacturer release little step c code ? thanks for your help


    Just for fun, I translated Parallax's example BS2 Stepper code (@: http://www.parallax.com/downloads/stepper-motor-basic-stamp-example-code) to C. It uses just the simple tools library in SimpleIDE. Not sure if this actually works, as I don't have the ULN chip but it's an example of BASIC to C translation for a stepper motor...
    // C version of BS2 Stepper program
    // =========================================================================
    //
    //   File...... Stepper_27964.BS2
    //   Purpose... Simple Stepper Motor Demo
    //   Author.... Parallax, Inc. (Copyright (c) 2004, All Rights Reserved)
    //   E-mail.... support@parallax.com
    //   Started...
    //   Updated... 18 MAR 2004
    //
    //   {$STAMP BS2}
    //   {$PBASIC 2.5}
    //
    // =========================================================================
    // -----[ Program Description ]---------------------------------------------
    //
    // Simple stepper motor (Parallax #27964) demonstration rotates motor one
    // full revolution clockwise, pauses, then rotates one full revolution
    // counter-clockwise. After another short pause, the process repeats.
    //
    // Connections:
    // -----------------------------------------------
    // P4 -> ULN2003.1, ULN2003.16 -> Phase 1 (Black)
    // P5 -> ULN2003.2, ULN2003.15 -> Phase 2 (Orange)
    // P6 -> ULN2003.3, ULN2003.14 -> Phase 3 (Brown)
    // P7 -> ULN2003.4, ULN2003.13 -> Phase 4 (Yellow)
    //
    // Note: Do NOT connect stepper motor wires directly to the BASIC Stamp.
    //       See Parallax documentation for complete connection schematic.
    
    #include "simpletools.h"
    
    // -----[ Global Variables ]------------------------------------------------
    
    int idx;           // loop counter
    int stpIdx;        // step pointer
    int stpDelay;      // delay for speed control
    
    //Steps   DATA    %0011, %0110, %1100, %1001
    char Steps[] = {0b0011, 0b0110, 0b1100, 0b1001};
    
    void Step_Fwd();
    void Step_Rev();
    void Do_Step();
    
    // -----[ Initialization ]--------------------------------------------------
    void Setup()
    {
      //DIRB = %1111                  // make P4..P7 outputs
      set_directions(7, 4, 16);       // make P4..P7 outputs
      stpDelay = 15;                  // set step delay, 15 msecs
    }
    
    // -----[ Program Code ]----------------------------------------------------
    int main(void)
    {
      Setup();
      
      while(1)                           // repeat forever
      {
        for (idx=1; idx < 49; idx++)     // one revolution
          Step_Fwd();                    // rotate clockwise
    
    
        pause(500);                      // wait 1/2 second
    
    
        for (idx=1; idx < 49; idx++)     // one revolution
          Step_Rev();                    // rotate counter-clockwise
    
    
        pause(500);                      // wait 1/2 second
      }  
    }
    
    // -----[ Functions ]-------------------------------------------------------
    
    // Turn stepper clockwise one full step (7.5 degrees)
    void Step_Fwd()
    {
      stpIdx = stpIdx + 1 % 4;           // point to next step
      Do_Step();
    }
    
    // Turn stepper counter-clockwise one full step (7.5 degrees)
    void Step_Rev()
    {
      stpIdx = stpIdx + 3 % 4;           // point to previous step
      Do_Step();
    }
    
    // Read new step data and output to pins
    void Do_Step()
    {
      //READ (Steps + stpIdx), Phase     // output new phase data
      set_outputs(3, 0, Steps[stpIdx]);
      pause(stpDelay);                   // pause between steps
    }
    

    dgately
Sign In or Register to comment.