📄 stepper.4th
字号:
\ Stepper code by Steven Sarns ... placed in the Public Domain
\ Timer 0 is used as prescaler from 1 MHz down to lower freq.
\ The output of timer 0 is sent to gated timer 1. The gate is
\ produced by timer 2 operating in a counter mode. Counter 2
\ is loaded with the number of steps. Timer 1 is loaded with the
\ step rate. The stepper can be stopped by shutting down the
\ prescaler.
\ Bit 1, port A is the direction control bit
\ Bit 0, port A is the stepper motor enable
62500 constant clock
\ CLOCK is the prescaled input frequency to timer1.
0000 constant timer0
timer0 1+ constant timer1
timer0 2+ constant timer2
timer0 3 + constant control
\ 0000H is the hardware location of the 8253. Its various registers are
\ at that address plus offsets +1, +2 and +3 from that address.
0256 constant pia
\ 0100H is location of the output port
variable port0
\ PORT0 is a memory image of the output port
\ initialization of counter timer0
hex
: init_prescale ( ---) 36 control pc! ;
: set_prescale ( divisor ---) 100 /mod swap timer0 pc! timer0 pc! ;
\ timer0 is prescaler, mode 3, 2 byte load, binary.
\ split divisor into hi byte, lo byte. stuff into timer0.
: init_freq ( ---) 76 control pc! ;
: set_freq ( divisor ---) 100 /mod swap timer1 pc! timer1 pc! ;
\ timer1 is frequency generator, mode 3, 2 byte load, binary
\ split & stuff, low byte first
: init_cntr ( ---) b0 control pc! ;
: set_cntr ( counts --- ) 100 /mod swap timer2 pc! timer2 pc! ;
\ timer2 is step counter, mode 0, 2 byte load, binary
\ output goes low when count value is loaded
\ output goes high when terminal count is reached
\ output is inverted and gates timer1 off when tc is reached
: set_step_rate ( speed ---) clock 0 rot um/mod nip set_freq ;
\ steps/sec = clock / n, solve for n
: read_cntr ( --- count ) 0 control pc! timer2 dup pc@ swap pc@ 100 * + ;
\ READ_CNTR latches, then reads contents of step count counter
\ This can be done while moving and used to recover from aborted move
\ pia memory image manipulations
hex
: enable ( f --- ) if port0 @ 1 or
else port0 @ 0e and
then port0 ! ;
\ ENABLE sets prescaler gate bit in memory image of pia port
: dir ( f --- ) if port0 @ 2 or
else port0 @ 0d and
then port0 ! ;
\ DIR sets direction bit in memory image of pia port
: out ( ---) port0 @ pia pc! ;
\ OUT sends memory image of pia port to pia port
: cw ( ---) 1 dir out ;
: ccw ( ---) 0 dir out ;
\ CW/CCW sets direction and sends to pia
: start! ( ---) 1 enable out ;
: stop! ( ---) 0 enable out ;
\ START!/STOP! enables counts to stepper and sends to pia
: done? ( --- f ) read_cntr 0= ;
\ tests if move has completed.
: setup ( --- )
init_prescale 010 set_prescale
init_freq 800 set_freq
init_cntr 001 set_cntr
00 port0 ! cw start! ;
\ adjust prescaler for maximum stepper motor rate
\ frequency is adjusted by application for step speed
: go ( speed +-dist ---)
setup dup
if cw else ccw then
\ set for clockwise or counter-clockwise in image
swap set_step_rate
\ set speed to frequency divider
start! abs set_cntr ;
\ set direction to motor control, start move!
\ End of Stepper Code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -