Controlling a unipolar stepping motor
|
The programMy program will turn the motor in both directions at
different speed in three basic modes: Single step, half step and
two-phase. Single step mode sends the pattern
|
10 REM One-Step 20 REM Control one stepper motor with the Velleman K8055 interface board 30 REM Jochen Lueg 40 REM http://roevalley.com 50 REM Limavady, May 2012 60 REM Version 1.0 70 80 ON ERROR PROCerror 90 PROCK8055_init 100 PROCinit 110 ON CLOSE PROCclose 120 PROCwindow(680,300,"Two-step") 130 OFF 140 SYS K8055_CloseDevice%,0 150 SYS K8055_OpenDevice%,0 160 170 PRINT 180 PRINT " Left - Stop - Right . . . . . . Z X C" 190 PRINT " Full step mode . . . . . . F" 200 PRINT " Two-phase full step . . . . . . T" 210 PRINT " Half step mode . . . . . . H" 220 PRINT " Fast - - - Slow . . . . . . 1 - 9" 230 PRINT 240 PRINT " Quit the progran . . . . . . Q" 250 260 REM Main loop 270 REPEAT 280 Key$=INKEY$(0):REPEAT UNTIL INKEY(0)=-1 290 IF Key$ = "z" OR Key$ = "Z" Dir$="left" :PROCreport 300 IF Key$ = "c" OR Key$ = "C" Dir$="right" :PROCreport 310 IF Key$ = "x" OR Key$ = "X" Dir$="not at all" : SYS K8055_ClearAllDigital%:PROCreport 320 IF Key$ = "f" OR Key$ = "F" Mode$="FullStep" :PROCreport 330 IF Key$ = "h" OR Key$ = "H" Mode$="HalfStep" :PROCreport 340 IF Key$ = "t" OR Key$ = "T" Mode$="TwoPhase" :PROCreport 350 IF Key$="1" Speed%=10 :PROCreport 360 IF Key$="2" Speed%=20 :PROCreport 370 IF Key$="3" Speed%=40 :PROCreport 380 IF Key$="4" Speed%=75 :PROCreport 390 IF Key$="5" Speed%=150 :PROCreport 400 IF Key$="6" Speed%=300 :PROCreport 410 IF Key$="7" Speed%=600 :PROCreport 420 IF Key$="8" Speed%=1000 :PROCreport 430 IF Key$="9" Speed%=1400 :PROCreport 440 SYS "Sleep",Speed% 450 IF Dir$<>"not at all" PROCturn 460 UNTIL Key$ = "q" OR Key$ = "Q" 470 PROCclose 480 END 490 500 510 520 DEFPROCclose 530 SYS K8055_ClearAllDigital% 540 SYS K8055_CloseDevice% 550 SYS "FreeLibrary",K8055_Board% 560 QUIT 570 ENDPROC 580 590 600 DEFPROCreport 610 LOCAL S% 620 S%=Speed% 630 IF Dir$="not at all" S%=0 640 PRINTTAB(1,11)"The motor is in ";Mode$;" mode " 650 PRINT " and turnig ";Dir$; 660 IF Dir$<>"not at all" PRINT;" at speed ";S%;" " 670 IF Dir$="not at all" PRINT;". " 680 ENDPROC 690 700 710 DEFPROCturn 720 IF Mode$="FullStep" THEN 730 IF Dir$="right" Coil%+=1:IF Coil% > 4 Coil% = 1 740 IF Dir$="left" Coil%-=1:IF Coil% < 1 Coil% = 4 750 SYS K8055_WriteAllDigital%,Fullstep%(Coil%) 760 ENDIF 770 780 IF Mode$="TwoPhase" THEN 790 IF Dir$="right" Coil%+=1:IF Coil% > 4 Coil% = 1 800 IF Dir$="left" Coil%-=1:IF Coil% < 1 Coil% = 4 810 SYS K8055_WriteAllDigital%,Twophase%(Coil%) 820 ENDIF 830 840 IF Mode$="HalfStep" THEN 850 IF Dir$="right" Coil%+=1:IF Coil% > 8 Coil% = 1 860 IF Dir$="left" Coil%-=1:IF Coil% < 1 Coil% = 8 870 SYS K8055_WriteAllDigital%,Halfstep%(Coil%) 880 ENDIF 890 900 ENDPROC 910 920 930 DEFPROCinit 940 Dir$="not at all" 950 Mode$="FullStep" 960 Speed%=2 970 Coil%=1 980 990 DIM Fullstep%(4) 1000 Fullstep%(1) = %0001 <<4 1010 Fullstep%(2) = %0010 <<4 1020 Fullstep%(3) = %0100 <<4 1030 Fullstep%(4) = %1000 <<4 1040 1050 DIM Twophase%(4) 1060 Twophase%(1) = %0011 <<4 1070 Twophase%(2) = %0110 <<4 1080 Twophase%(3) = %1100 <<4 1090 Twophase%(4) = %1001 <<4 1100 1110 DIM Halfstep%(8) 1120 Halfstep%(1) = %0001 <<4 1130 Halfstep%(2) = %0011 <<4 1140 Halfstep%(3) = %0010 <<4 1150 Halfstep%(4) = %0110 <<4 1160 Halfstep%(5) = %0100 <<4 1170 Halfstep%(6) = %1100 <<4 1180 Halfstep%(7) = %1000 <<4 1190 Halfstep%(8) = %1001 <<4 1200 1210 SYS K8055_WriteAllDigital%,0 1220 ENDPROC 1230 1240 1250 DEFPROCwindow(WindowWidth%,WindowHeight%,W$) 1260 MODE 30 1270 SYS "SetWindowPos",@hwnd%,0,0,0,WindowWidth%,WindowHeight%,6 1280 SYS "SetWindowText",@hwnd%,W$ 1290 VDU 26 1300 COLOUR 15 1310 *FONT Lucida Console,14,2 1320 ENDPROC 1330 1340 1350 DEFPROCerror 1360 PRINT REPORT$;" at line ";ERL 1370 SYS K8055_ClearAllDigital% 1380 SYS K8055_ClearAllAnalog% 1390 SYS K8055_CloseDevice% 1400 SYS "FreeLibrary",K8055_Board% 1410 END 1420 ENDPROC 1430 1440 1450 DEFPROCK8055_init 1460 SYS"LoadLibrary","K8055D.dll" TO K8055_Board% 1470 SYS"GetProcAddress",K8055_Board%,"OpenDevice" TO K8055_OpenDevice% 1480 SYS"GetProcAddress",K8055_Board%,"CloseDevice" TO K8055_CloseDevice% 1490 SYS"GetProcAddress",K8055_Board%,"ReadAnalogChannel" TO K8055_ReadAnalogChannel% 1500 SYS"GetProcAddress",K8055_Board%,"ReadAllAnalog" TO K8055_ReadAllAnalog% 1510 SYS"GetProcAddress",K8055_Board%,"OutputAnalogChannel" TO K8055_OutputAnalogChannel% 1520 SYS"GetProcAddress",K8055_Board%,"OutputAllAnalog" TO K8055_OutputAllAnalog% 1530 SYS"GetProcAddress",K8055_Board%,"ClearAnalogChannel" TO K8055_ClearAnalogChannel% 1540 SYS"GetProcAddress",K8055_Board%,"ClearAllAnalog" TO K8055_ClearAllAnalog% 1550 SYS"GetProcAddress",K8055_Board%,"SetAnalogChannel" TO K8055_SetAnalogChannel% 1560 SYS"GetProcAddress",K8055_Board%,"SetAllAnalog" TO K8055_SetAllAnalog% 1570 SYS"GetProcAddress",K8055_Board%,"WriteAllDigital" TO K8055_WriteAllDigital% 1580 SYS"GetProcAddress",K8055_Board%,"ClearDigitalChannel" TO K8055_ClearDigitalChannel% 1590 SYS"GetProcAddress",K8055_Board%,"ClearAllDigital" TO K8055_ClearAllDigital% 1600 SYS"GetProcAddress",K8055_Board%,"SetDigitalChannel" TO K8055_SetDigitalChannel% 1610 SYS"GetProcAddress",K8055_Board%,"SetAllDigital" TO K8055_SetAllDigital% 1620 SYS"GetProcAddress",K8055_Board%,"ReadDigitalChannel" TO K8055_ReadDigitalChannel% 1630 SYS"GetProcAddress",K8055_Board%,"ReadAllDigital" TO K8055_ReadAllDigital% 1640 SYS"GetProcAddress",K8055_Board%,"ResetCounter" TO K8055_ResetCounter% 1650 SYS"GetProcAddress",K8055_Board%,"ReadCounter" TO K8055_ReadCounter% 1660 SYS"GetProcAddress",K8055_Board%,"SedtCounterDebouceTime" TO K8055_SetCounterDebounceTime% 1670 1680 1690 ENDPROC 1700 |
Back to the start
|