-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstepperULN2003.txt
166 lines (136 loc) · 3.71 KB
/
stepperULN2003.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
\ *********************************************************************
\ Controlling a 28BYJ motor with the ULN2003 driver *
\ Filename: stepperULN2003.txt *
\ Date: 27.10.2020 *
\ Updated: 28.10.2020 *
\ File Version: 1.0 *
\ MCU: ARDUINO all models *
\ GNU General Public License *
\ FF Version: 5.0 * *
\ Forth Author: Marc PETREMANN *
\ *********************************************************************
\ ** part off pinsDefinitions.txt *************************
\ source: https://github.com/MPETREMANN11/ARDUINO-FORTH/blob/master/pinsDefinitions.txt
-defPIN
marker -defPIN
: defPIN: ( PORTx mask --- <word> | <word> --- mask port)
create
c, c, \ compile PORT and pin mask
does>
dup c@ \ push pin mask
swap 1+ c@ \ push PORT
;
\ Turn a pin on, dont change others
: high ( pinmask portadr -- )
mset
;
\ Turn a pin off, dont change others
: low ( pinmask portadr -- )
mclr
;
\ Only for PORTx bits
\ address of DDRx is one less than address of PORTx
\ Set DDRx so its corresponding pin is output.
: output ( pinmask portadr -- )
1- high
;
\ Set DDRx so its corresponding pin is input. --- unused
\ : input ( pinmask portadr -- )
\ 1- low
\ ;
\ ** end part of pinsDefinitions.txt **********************
-stepper
marker -stepper
decimal
flash
43 constant PORTD
PORTD %00000100 defPIN: IN1 \ blue wire motor
PORTD %00001000 defPIN: IN2 \ pink wire motor
PORTD %00010000 defPIN: IN3 \ yellow wire motor
PORTD %00100000 defPIN: IN4 \ orange wire motor
ram
: init.stepper ( ---)
IN1 output
IN2 output
IN3 output
IN4 output
;
\ true for Clock Wise direction
\ false for Counter Clock Wise direction
true value DIRECTION \ not used
\ pulse delay
2 value pulseDelay
:noname \ step 7
IN4 high IN3 low IN2 low IN1 high ;
:noname \ step 6
IN4 low IN3 low IN2 low IN1 high ;
:noname \ step 5
IN4 low IN3 low IN2 high IN1 high ;
:noname \ step 4
IN4 low IN3 low IN2 high IN1 low ;
:noname \ step 3
IN4 low IN3 high IN2 high IN1 low ;
:noname \ step 2
IN4 low IN3 high IN2 low IN1 low ;
:noname \ step 1
IN4 high IN3 high IN2 low IN1 low ;
:noname \ step 0
IN4 high IN3 low IN2 low IN1 low ;
flash
create 'PULSES
, , , , , , , ,
ram
\ index pointing in 'PULSES
0 value currentPulse
-cycles
marker -cycles
\ send one pulse to A4988
: execPulse ( ---)
currentPulse 2* \ convert pointer in addr offset
'PULSES + @ \ extract cfa of noname definition
execute \ execute nonamed definition
pulseDelay ms
;
\ send 8 pulses to A4988 in CW direction
: cycleCW ( ---)
0 to currentPulse
8 for
execPulse
currentPulse 1+ to currentPulse
next
;
\ send 8 pulses to A4988 in CCW direction
: cycleCCW ( ---)
7 to currentPulse
8 for
execPulse
currentPulse 1- to currentPulse
next
;
defer cycles
: setDirection ( n ---)
0<
if ['] cycleCCW is cycles
else ['] cycleCW is cycles
then
;
0 value statePORTD
: PORTDdown ( ---)
PORTD c@ to statePORTD
0 PORTD c!
;
: PORTDrestore ( ---)
statePORTD PORTD c!
;
: steps ( n ---)
PORTDrestore
dup setDirection
abs
for
cycles
next
PORTDdown
;
\ test stepper motor
init.stepper
200 steps