' ========================================================================= ' File....... StepperSXB ' Purpose.... Run a bipolar stepper motor ' Author..... David Mcanulty ' E-mail..... stepperSX28program@hellspark.com ' Started.... 01-16-2010 ' Updated.... Never ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' ' Runs a stepper motor provided to me by scott ' '------{ Device Settings ]------------------------------------------------- DEVICE SX28,TURBO,BANKS8,OSCHS3,SYNC,OPTIONX FREQ 50_000_000 ID "STEPPER" ' -----[ I/O Definitions ]------------------------------------------------- Coils VAR RB ' RB.0->RB.3 connect to L293D chip Speed VAR RA.0 ' Pot circuit IO (speed control) LtMode VAR RA.1 ' mode select input ' -----[ Constants ]------------------------------------------------------- LoSpeed CON 1 ' lo end of POT reading 'Scale CON $0163 ' 1.3868 with */ Scale CON $0030 ' 1.3868 with */ ' -----[ Variables ]------------------------------------------------------- rawSpd VAR Word ' speed input from POT delay VAR Word ' time between patterns btnVar VAR Byte ' workspace for BUTTON modeSelect VAR Byte ' selected mode maxSteps VAR Byte ' offset into patterns rndVal VAR Byte ' workspace for RANDOM testPot VAR Byte ' 1 for POT testing idx VAR Byte ' Index for subs ' -----[ Initialization ]------------------------------------------------- PROGRAM Start watch coils, 4, UBIN watch rawSpd, 16, UDEC watch Speed, 8, UDEC watch LtMode, 1, UBIN watch ModeSelect, 8, UDEC watch maxSteps, 8, UDEC watch rndVal, 8, UDEC watch delay, 16, UDEC watch idx, 8, UDEC ' -----[Subs ]------------------------------------------------------------ Read_Speed SUB 0 ModeClockwise SUB 0 ModeCounterclockwise SUB 0 ' -----[ Program Code ]---------------------------------------------------- Start: '######## coils = %00000000 ' start with outputs off Tris_A = %0011 ' Make ra.0 and ra.1 inputs for pot Tris_B = %11000000 ' make rb.0 -> rb.5 outputs idx = 1 TestPot = 0 ' 1 for POT testing IF TestPot = 1 THEN ' for reading raw pot value DO GOSUB Read_Speed PAUSE 50 LOOP ENDIF Main: GOSUB Read_Speed ' read speed pot 'delay = (rawSpd - LoSpeed) */ Scale + 50 ' calc delay (50-1000 ms) delay = rawSpd - LoSpeed ' calc delay (50-1000 ms) delay = delay */ Scale ' calc delay (50-1000 ms) 'delay = delay + 50 ' calc delay (50-1000 ms) 'delay = 1000 PAUSE delay ' wait between patterns Switch_Check: ''BUTTON LtMode, 0, 255, 0, btnVar, 0, Show ''BUTTON Pin,DownState, Delay, Rate, Workspace, TargetState, Address IF LtMode = 0 THEN Pause 10 IF ltMode = 0 THEN 'modeSelect = modeSelect + 1 // 5 ' yes, update mode var modeSelect = modeSelect + 1 ' yes, update mode var modeSelect = modeSelect // 5 ' yes, update mode var ENDIF ENDIF 'modeSelect = 0 Show: Branch modeSelect, ModeClockwise, ModeCounterclockwise GOTO Show END ' -----[ Gosubs ]----------------------------------------------------- Read_Speed: HIGH Speed ' charge cap PAUSE 1 ' for 1 millisecond RCTIME Speed, 1, rawSpd ' read the Pot RETURN ModeClockwise: READ PatternClockwise, maxSteps ' get steps in sequence IF idx <= maxSteps THEN ' check idx range READ PatternClockwise + idx, coils ' get step pattern INC idx ' point to next step PAUSE delay ELSE idx = 1 ' reset idx if needed ENDIF GOTO Show ModeCounterclockwise: READ PatternCounterclockwise, maxSteps ' get steps in sequence IF idx <= maxSteps THEN ' check idx range READ PatternCounterclockwise + idx, coils ' get step pattern INC idx ' point to next step PAUSE delay ELSE idx = 1 ' reset idx if needed ENDIF GOTO Show ' -----[ EEPROM Data ]----------------------------------------------------- PatternClockwise: DATA 4 '111222 COIL # 'abeabe Terminal A | Terminal B | Enable DATA %101000 '1 DATA %000101 '2 DATA %011000 '3 DATA %000011 '4 PatternCounterclockwise: DATA 4 '111222 COIL # 'abeabe Terminal A | Terminal B | Enable DATA %101101 '1 DATA %101011 '2 DATA %011011 '3 DATA %011101 '4