Stepper Program for Newcomers

2014-01-04
2014-01-04
  • Thomas Henry
    Thomas Henry
    2014-01-04

    Hi all,

    I recently found the 28BYJ-48 stepper motor with driver chip on Amazon for less than $5.00. It's a great bargain, easy to use, perfect for beginners. I whipped up some Great Cow Basic code to put it through its paces. Perhaps some newcomers to the field might find it useful. The code is heavily commented, and shows the connections needed. I had to use a FOR loop in the Revolution subroutine, since REPEAT is buggy with word values.

    ;This program demonstrates bidirectional operation of the
    ;28BYJ-48 stepper motor, driven by the ULN2003 Darlington array.
    ;The 28BYJ-48 stepper motor is a 4-phase, 8-pulse, geared affair.
    ;One bipolar winding connects to pins 1 and 3, while the other
    ;connects to pins 2 and 4. Pin 5 is common to all coil taps and
    ;connects to +5V.
    
    ;It takes a sequence of 8 pulses or voltage patterns applied
    ;to the coils in various combinations to move the rotor one step.
    ;There are 512 steps in one complete rotation, therefore each
    ;is equivalent to a movement of 0.703125 degrees.
    
    ;The 8 pulses within each step should be spaced with a pause of
    ;1.2 milliseconds. Therefore, each step consumes 9.6 milliseconds,
    ;which corresponds to a frequency of about 104 Hz.
    
    ;The current drain averages around 150 mA.
    
    ;Five-pin connector:
    ;Blue   - pin 1 = B.0
    ;Pink   - pin 2 = B.1
    ;Yellow - pin 3 = B.2
    ;Orange - pin 4 = B.3
    ;Red    - pin 5 = +5V
    
    ;Power:
    ;- = ground
    ;+ = +5V
    ;leave the adjacent pin jumper in place
    
    ;----- Configuration
    
    #chip 16F88, 8            ;PIC16F88 running at 8 MHz
    #config mclr=off          ;reset handled internally
    #config osc=int           ;use internal clock
    
    ;----- Constants
    
    #define delay 1200 uS     ;1.2 mS between each pulse
    #define fwd true          ;forward flag
    #define rev false         ;reverse flag
    
    ;----- Variables
    
    dim i as byte
    dim j as word
    
    ;----- Arrays
    
    dim coils(8) as byte
    coils = b'1000'', b'1100', b'0100', b'0110', b'0010', b'0011', b'0001', b'1001'
    
    ;----- Program
    
    dir PortB out             ;only B.0 through B.3 used
    do
      repeat 2                ;do the cardinal points twice
        repeat 4              ;step off all four cardinal points
          cardinal(fwd)       ;go forward
          wait 1 S            ;wait a second
        end repeat            ;do next cardinal point
      end repeat
    
      repeat 2                ;do the cardinal points twice
        repeat 4              ;step off all four cardinal points
          cardinal(rev)       ;but in reverse this time
          wait 1 S            ;wait a second
        end repeat            ;do next cardinal point
      end repeat
    
      revolution(fwd)         ;complete revolution forward
      revolution(rev)         ;complete revolution backward
      wait 1 S
    loop                      ;and start over again
    
    ;----- Subroutines
    
    sub clockwise             ;one step clockwise
      for i = 8 to 1 step -1
        PortB = coils(i)
        wait delay
      next i
    end sub
    
    sub counterclockwise      ;one step counterclockwise
      for i = 1 to 8
        PortB = coils(i)
        wait delay
      next i
    end sub
    
    sub cardinal(direct as byte)
      repeat 128              ;128 times 0.703125 degrees is 90 degrees
        if direct = fwd then
          clockwise
        else
          counterclockwise
        end if
      end repeat
    end sub  
    
    sub revolution(direct as byte)
      for j = 1 to 512        ;512 times 0.703125 degrees is 360 degrees
        if direct = fwd then
          clockwise
        else
          counterclockwise
        end if
      next j
    end sub
    
     
  • Anobium
    Anobium
    2014-01-04

    Very nice job. Thank you.