Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Qbasic program for controlling stepper motors I wrote one

Status
Not open for further replies.

ElectroGeek_87

New Member
If anyone out ther is interested in controlling a unipolar stepper motor via the parallel port on your pc and also uses Qbasic then copy the code below into a qbasic file and have at it. The program will prompt the user for mode of operation eg. single coil excitation mode or double coil excitation mode, direction and how many clock cycles to make the delay. A good value for my "VERY ANCIENT" pentium 133mhz pc is anywhere from 45 to however long you want the delay between coil energization to be. below 45 clock cycles the stepper motor likes to malfuntion. I am using parallel printer sentronics? connector. Modify the program to your liking. Sorry I did not include comments.
pins #2 for coil 1
#3 for coil 2
#4 for coil 3
#5 for coil 4
#18(can be any ground) for ground.

I am also using a uln2803 darlington transistor array to drive the motor.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Written by Sam Bixler ;
;Date 03-10-03 ;
;For controlling a unipolar stepper motor via a parallel port ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

CLS
VarMode = 0
VarRot = 0
VarRev = 0
VarDelay = 0
int1 = 0


Main:
CLS
OUT 888, 0
DO UNTIL INKEY$ <> ""
PRINT "Enter the number of revolutions"
INPUT VarRev
VarRev = VarRev * 50
PRINT "Enter the Mode of operation"
PRINT " 1 for single coil excitation"
PRINT " 2 for double coil excitation"
INPUT VarMode
PRINT "Enter the number of delay cycles"
INPUT VarDelay
PRINT "Enter the direction of rotation"
PRINT "5 for CW 7 for CCW"
INPUT VarRot
VarMode = VarRot + VarMode
IF VarMode = 6 THEN GOTO ScCW:
IF VarMode = 7 THEN GOTO DcCw:
IF VarMode = 8 THEN GOTO ScCcw:
IF VarMode = 9 THEN GOTO DcCcw: ELSE GOTO nd:

LOOP

ScCW:
DO
GOSUB Step1:
GOSUB Step2:
GOSUB Step3:
GOSUB Step4:
VarRev = VarRev - 1
IF VarRev = 0 THEN GOTO Main:
LOOP

ScCcw:
DO
GOSUB Step4:
GOSUB Step3:
GOSUB Step2:
GOSUB Step1:
VarRev = VarRev - 1
IF VarRev = 0 THEN GOTO Main:
LOOP

DcCw:
DO
GOSUB Step5:
GOSUB Step6:
GOSUB Step7:
GOSUB Step8:
VarRev = VarRev - 1
IF VarRev = 0 THEN GOTO Main:
LOOP

DcCcw:
DO
GOSUB Step8:
GOSUB Step7:
GOSUB Step6:
GOSUB Step5:
VarRev = VarRev - 1
IF VarRev = 0 THEN GOTO Main:
LOOP

Step1:
OUT 888, 0
DO
OUT 888, 1
IF int1 = VarDelay THEN RETURN
int1 = int1 + 1
LOOP


Step2:
OUT 888, 0
DO
OUT 888, 2
IF int1 = 0 THEN RETURN
int1 = int1 - 1
LOOP

Step3:
OUT 888, 0
DO
OUT 888, 4
IF int1 = VarDelay THEN RETURN
int1 = int1 + 1
LOOP

Step4:
OUT 888, 0
DO
OUT 888, 8
IF int1 = 0 THEN RETURN
int1 = int1 - 1
LOOP

Step5:
OUT 888, 0
DO
OUT 888, 3
IF int2 = VarDelay THEN RETURN
int2 = int2 + 1
LOOP

Step6:
OUT 888, 0
DO
OUT 888, 6
IF int2 = 0 THEN RETURN
int2 = int2 - 1
LOOP

Step7:
OUT 888, 0
DO
OUT 888, 12
IF int2 = VarRev THEN RETURN
int2 = int2 + 1
LOOP

Step8:
OUT 888, 0
DO
OUT 888, 9
IF int2 = 0 THEN RETURN
int2 = int2 - 1
LOOP



nd:
OUT 888, 0
 
good one Electrogeek.

I've connected leds to my outputs and:
Single coil excitation , colockwise and counter-clockwise works perfect.

Double coil excitation apears not to work properly on my computer(p2 350mHz).
one of the leds,(line out) only energises for a very very short time compared to the others.

how many line outs do you nead for double excitation?

to see what i'm talking about, connect leds to line outs,
and run double excitation, with delays of about 10000(very slow).
you should see one of the lines flash on/off very quickly.
And use super bright leds.

well done. good program!
For what project are you going to use the stepper for?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top