help understanding code
ispear
Posts: 20
Hi,
I am working on a project at kansas state university that uses stepper motors. I have acquired many stepper motor drivers and have the code that works with those drivers but I am having trouble understanding it so I can adapt it. The drivers are very simple: wake the board by sending a high to SleepPin, choose direction by sending high or low to DirPin, and send PWM to StepPin to set speed. The following is the code that works but I am having trouble adapting. it uses a limit switch, a direction switch and a 10K pot to determine speed. What specifically is this segment I've put in BLUE doing?
Many thanks,
-Isaac
I am working on a project at kansas state university that uses stepper motors. I have acquired many stepper motor drivers and have the code that works with those drivers but I am having trouble understanding it so I can adapt it. The drivers are very simple: wake the board by sending a high to SleepPin, choose direction by sending high or low to DirPin, and send PWM to StepPin to set speed. The following is the code that works but I am having trouble adapting. it uses a limit switch, a direction switch and a 10K pot to determine speed. What specifically is this segment I've put in BLUE doing?
VAR long time long temp long Stack[20] long CurrentDir CON _xinfreq = 5_000_000 _clkmode = xtal1 + pll16x SleepPin = 22 ResetPin = 23 PFD = 22 EnablePin = 24 DirSwitch = 7 LimitSwitch = 6 DirPin = 17 StepPin = 18 MS1Pin = 21 MS2Pin = 20 MS3Pin = 19 Forward = 0 Reverse = 1 PUB Main Cognew(StepMotor,@stack[0]) Cognew(RCMeasure,@stack[10]) PUB StepMotor dira[StepPin] := 1 dira[DirPin] := 1 dira[MS1Pin] := 1 dira[MS2Pin] := 1 dira[SleepPin] := 1 outa[MS1Pin] := 1 outa[MS2Pin] := 1 outa[MS3Pin] := 1 repeat if (temp := time) > 350 'If the pot is turned all the way down... if (ina[LimitSwitch] == 0) 'Make sure the Limit Switch is not triggered... [COLOR=#020FC0][B]outa[SleepPin] := 1 CurrentDir := outa[DirPin] := ina[DirSwitch] outa[StepPin] := 1 waitcnt(clkfreq/temp + cnt ) outa[StepPin] := 0 waitcnt(clkfreq/temp + cnt ) else 'wait for a direction change... repeat while (ina[DirSwitch] == CurrentDir) 'do nothing...[/B][/COLOR] else outa[SleepPin] := 0 PUB RCMeasure ctra[30..26] := 000 ' Set mode to "POS detector" ctra[5..0] := 16 ' Set APIN to 16 (P16) frqa := 1 ' Increment phsa by 1 for each clock tick repeat dira[16] := outa[16] := 1 ' Set pin to output-high waitcnt(clkfreq/100_000 + cnt) ' Wait for circuit to charge phsa~ ' Clear the phsa register dira[16]~ ' Pin to input stops charging circuit repeat 22 waitcnt(clkfreq/60 + cnt) time := (phsa - 624) #> 0 waitcnt(clkfreq/2 + cnt)
Many thanks,
-Isaac
Comments
In the if:
1. set the sleep PIN to 1
2. copy the direction switch to the direction PIN and store it in CurrentDir for later usage (in the else)
3. create a pulse on the step pin
-Isaac