' ' Program: FOX Stepper Motor Program Enhanced for VFD2041 display ' Author: (c) 2007 by A Fox Consulting & Design ' http://www.afox-consulting.com ' Date: 09/27/2007 ' ' Stepper motor coils on pins 4-7 ' VFD Tx on 15, Rx on 14 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ---------- I/O Definitions ---------- _Coils VAR OUTB ' output to stepper coils _VFDTx PIN 15 _VFDRx PIN 14 ' ---------- Variables ---------- ' Stuff for the stepper motoe NumSteps CON 4 ' use 4-step sequence LastStep CON NumSteps - 1 ' last step in sequence idx VAR Word ' loop counter stpIdx VAR Nib ' step pointer stpDir VAR Bit stpCount VAR Word stpDelay VAR Nib ' delay for speed control RSeed VAR Word ' Stuff for the stepper motoe Baud CON 32 ' 19200,8,N,1 CTRL CON $FE CLRSCR CON $58 CRSRPOS CON $47 ' Position Cursor NOWRAP CON $44 ' Turn auto line-wrap off INITHBAR CON $68 DRAWHBAR CON $7C ' ---------- EEPROM Data ---------- ' __ ' ABAB ' ----- 'Unipolar Stepper Motor 'Step1 DATA %1100 'Step2 DATA %0110 'Step3 DATA %0011 'Step4 DATA %1001 'Bifilar Stepper Motor Step1 DATA %1000 Step2 DATA %0010 Step3 DATA %0100 Step4 DATA %0001 ' ---------- Main Code ---------- Setup: DIRB = %1111 ' make P4..P7 outputs PAUSE 500 ' Wait for display to become ready GOSUB Init_Display Rseed = $F438 ' Just a random seed to start with Main: ' Make a StepDelay between 0 and 5 RANDOM Rseed stpDelay = (Rseed // 6) ' Make a number of steps between 1 and 500 RANDOM Rseed stpCount = (Rseed // 499) + 1 ' Make random direction RANDOM Rseed stpDir = Rseed // 2 ' Show values on VFD GOSUB Display_Values GOSUB Display_Running FOR idx = 0 TO stpCount IF stpDir = 1 THEN stpIdx = stpIdx + 1 // NumSteps ' point to next step ELSE stpIdx = stpIdx + LastStep // NumSteps ' point to previous step ENDIF READ (Step1 + stpIdx), _Coils ' output new coil data PAUSE stpDelay ' pause between steps NEXT GOSUB Display_Paused ' Make random pause between 0 and 1 second RANDOM Rseed stpCount = (Rseed // 10) * 100 PAUSE stpCount GOTO Main ' ---------- Stepper Motor Functions ---------- ' Turn stepper clockwise one full step Step_Fwd: stpIdx = stpIdx + 1 // NumSteps ' point to next step GOTO Do_Step ' Turn stepper counter-clockwise one full step Step_Rev: stpIdx = stpIdx + LastStep // NumSteps ' point to previous step GOTO Do_Step ' Read new step data and output to pins Do_Step: READ (Step1 + stpIdx), _Coils ' output new coil data PAUSE stpDelay ' pause between steps RETURN ' ---------- VFD Functions ---------- Init_Display: SEROUT _VFDTx, Baud, [CTRL, CLRSCR] SEROUT _VFDTx, Baud, [CTRL, NOWRAP] SEROUT _VFDTx, Baud, [CTRL, INITHBAR] SEROUT _VFDTx, Baud, ["--FoxMotorVFD.bs2--", CR, LF, "Steps: Dir:", CR, LF, CR, LF, "Speed:"] RETURN Display_Values: SEROUT _VFDTx, Baud, [CTRL, CRSRPOS, 8, 2, DEC3 stpCount] SEROUT _VFDTx, Baud, [CTRL, CRSRPOS, 17, 2, DEC stpDir] SEROUT _VFDTx, Baud, [CTRL, CRSRPOS, 8, 4, DEC stpDelay] SEROUT _VFDTx, Baud, [CTRL, DRAWHBAR, 12, 4, 0, 30 - (stpDelay*5)] RETURN Display_Running: SEROUT _VFDTx, Baud, [CTRL, CRSRPOS, 1, 3, "RUNNING", CR, LF, "Speed:"] RETURN Display_Paused: SEROUT _VFDTx, Baud, [CTRL, CRSRPOS, 1, 3, "OFF ", CR, LF, " "] RETURN