PDA

View Full Version : Stepper Motor Program



kiwipiper
- 26th November 2007, 02:57
I have got a stepper motor working nicely on a basic stamp with the following code. But am unsure of what changes should I make for the same code to work on a PIC. I understand most of what is going on but am a bit unsure about what the Phase is about and the OUTB

Phase VAR OUTB ' phase control outputs
idx VAR Byte ' loop counter
stpIdx VAR Nib ' step pointer
stpDelay VAR Byte ' delay for speed control

Steps DATA %0011, %0110, %1100, %1001

Setup:
DIRB = %1111 ' make P4..P7 outputs
stpDelay = 5 ' set step delay

Main:
FOR idx = 1 TO 200 ' one revolution
GOSUB Step_Fwd ' rotate clockwise
NEXT
PAUSE 500 ' wait 1/2 second
FOR idx = 1 TO 200 ' one revolution
GOSUB Step_Rev ' rotate counter-clockwise
NEXT
PAUSE 500 ' wait 1/2 second
GOTO Main
END

' -----[ Subroutines ]-----------------------------------------------------

Step_Fwd:
stpIdx = stpIdx + 1 // 4 ' point to next step
GOTO Do_Step

Step_Rev:
stpIdx = stpIdx + 3 // 4 ' point to previous step
GOTO Do_Step

Do_Step:
READ (Steps + stpIdx), Phase ' output new phase data
PAUSE stpDelay ' pause between steps
RETURN

Archangel
- 26th November 2007, 03:10
Hi kiwipiper,
No Dirs in PBP so DIR = 1111 becomes TRISB = %0000 ' make lower B ports outputs
change NIB to BYTE as no NIBs in PBP
Phase VAR OUTB ' phase control outputs - not sure what to do here . . . what does this do in stamp lingo?
stpDelay = 5 would be stpDelay CON 5

mackrackit
- 26th November 2007, 03:15
It has been awhile since I stamped, but this should get you going with PIC BASIC.
http://www.picbasic.co.uk/forum/showthread.php?t=101&highlight=stepper

kiwipiper
- 27th November 2007, 02:56
Thanks for that, got it working not as elegantly as the first program but it does the job.
Here is the code, any suggestions on making it a bit more elegant wouild be appreciated.

DEFINE OSC 4

TRISB = %00000000

speed var word
i var byte
speed = 10

Main:
FOR i = 1 TO 50 ' one revolution
GOSUB Step_Fwd ' rotate clockwise
NEXT i
PAUSE 500 ' wait 1/2 second
FOR i = 1 TO 50 ' one revolution
GOSUB Step_Rev ' rotate counter-clockwise
NEXT i
PAUSE 500 ' wait 1/2 second
GOTO Main
END

Step_Fwd:
portb = %1001
pause speed
portb = %1100
pause speed
portb = %0110
pause speed
portb = %0011
pause speed
return

Step_Rev:
portb = %0011
pause speed
portb = %0110
pause speed
portb = %1100
pause speed
portb = %1001
pause speed
return