Professional Documents
Culture Documents
Stepper Motor Control With An MC68HC11E9 Microcontroller: Freescale Semiconductor
Stepper Motor Control With An MC68HC11E9 Microcontroller: Freescale Semiconductor
1 Introduction
This note provides basic implementation details and procedural information to design and assemble a
stepper motor system. The controller discussed here is the MC68HC11E9, an 8-bit Freescale microcontroller (MCU). There are many embedded control applications supported by the M68HC11 Family.
The note consists of a general description and gives highlights of implementing a basic stepper motor
system application. A step-by-step hardware assembly section is included to promote ease of construction should one desire to build a similar system.
To simplify the application, the software was generated on the Freescale M68HC11EVM evaluation module (EVM). The program created with the EVM is shown in 6 Listing. The program runs in addresses
$C000 through $C1CC. It is meant to be used as a guide and can be modified to support a variety of
stepper motor control applications. Some modules will require no changes for use. For convenience, a
copy of the code is available through Freeware Data Services. The Freeware BBS can be accessed by
modem at (512) 891-3733, or via the World Wide Web at http://freeware.sps.mot.com.
The EVM comes with an on-board monitor called EVMbug11 that supports software development. This
evaluation system provides easy I/O interfacing to external hardware and offers the user an inexpensive
programming solution for devices with OTP, EPROM and EEPROM non-volatile memory.
Evaluation of the A0, A1, A8, E0, E1, E9 or 811E2 versions of M68HC11 microcontroller devices is supported when using the EVM. The microcontroller that resides on the EVM for this application is the
MC68HC811E1 version.
5V
PORTB[6:0]
7-SEGMENT
DISPLAY
VRH
A/D INPUT
PORTA0
R1
IR
DETECTOR/EMITTER
POSITION BAR
VRL
S1
PORTC[3:0]
IRQ
STEPPER MOTOR
S2
PORTD0
SEQUENCE LEDS
CW CCW LEDS
3 Hardware Development
3.1 Motor Description
The motor coil assemblies operate at voltage levels ranging from +5 to +24 volts. The motor has a single
rotor which is connected to a shaft at the center of the assembly. There are multiple coils surrounding
the rotor. A total of 100 steps are required for one complete revolution of the shaft. Each step increments
by 3.6. For this application, a wheel is attached to the shaft, but for other applications, the wheel could
be replaced by a gear, a pulley, a belt or a timing mechanism.
AN1285/D
The hardware required to control the stepper motor varies significantly from one application to the next.
Below is a list of components used to implement the system interfaced to an EVM:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
One EVM;
One stepper motor;
One ULN-2075B motor driver IC (optional for enhanced drive);
One 25 K potentiometer (A/D input control);
Two SPDT switches (power and CW or CCW turn control);
One SPDT switch MOM (position control and single-step);
One seven-segment display (display stepper motor delay $0F);
One infrared detector and emitter (position control);
Seven LEDs (sequence, power and CW or CCW indicators);
Two 1-inch square mounting boards for the IR pair;
One project assembly board (4-inch x 6-inch);
One power terminal strip;
Two wirewrap sockets;
Four NPN and four PNP transistors (optional for increased motor drive).
SEGMENT
PORTB BIT #
4 Software Development
All software routines for this application are implemented in assembly language. The EVM supports the
software routine implementation. P&E Microsystems IASM11 software was used for code development,
assembly, debug, and for programming the EEPROM memory.
Program execution occurs in the order shown in Figure 3. To take advantage of modular software techniques, several jump to subroutine (JSR) instructions are used. Each JSR has a corresponding return
from subroutine (RTS) instruction. By using this method, the program has a smooth and efficient flow.
Debugging software errors is simplified by using the modular approach as well.
The following paragraphs describe the subroutines that initialize the system, control the stepper movement, calculate speed, and output digital readouts. Each heading represents an individual stand-alone
module.
AN1285/D
START
INITIALIZE
READ A/D
COMPUTE SPEED
DISPLAY
ALIGN
TURN
AN1285/D
$1039
BIT 0
ADPU
CSEL
IRQE1
DLY1
CME
CR11
CR01
BIT 7
RESET:
NOTES:
1. Can be written only once in first 64 cycles out of reset in normal modes, or at any time in special modes.
A value of $A0 is written to the A/D control register (ADCTL), enabling the scan mode and setting the
conversion complete flag, as shown below.
$1030
BIT 7
BIT 0
CCF
SCAN
MULT
CD
CC
CB
CA
RESET:
To simplify the design, keyboard inputs can be used to provide the necessary value directly to the A/D
converter rather than using a potentiometer.
4.4 COMSPD
This routine reads A/D result register 1 (ADR1) to determine the speed value being input by the potentiometer. ADR1 is an 8-bit register that contains one of 256 possible values. The value in the register is
complemented so that the highest value (F) represents the longest delay or slowest turning speed.
To obtain the highest resolution of result register content, four consecutive LSRA instructions are executed. Following these four shifts, a value in the range 0 to F remains in the lower nibble of accumulator
A. The value is stored in ATEMP2 for later use. To insure that the latest converted value is represented,
the ADCTL register is set up so that the result register is continuously being scanned during program
execution.
4.5 DISPLAY
The A/D conversion value is retrieved from location ATEMP2. DISPLAY calls the COMPDIS subroutine,
which determines the number to be displayed. When the polling routine finds the appropriate match, the
data to turn on the segments for that particular number is stored in PORT B. The information from PORT
B is routed to the seven-segment display for visual monitoring.
4.6 ALIGN
The ALIGN routine controls the actual alignment of the motor wheel to a known starting point, as shown
in Figure 4. This configuration illustrates an easy method of controlling wheel alignment.
An infrared (IR) emitter and detector are used to establish proper alignment of the wheel. The motor
wheel has an extension connected to its topside. As the wheel turns, the extension breaks the invisible
IR beam between the emitter and detector. When this occurs during the very first revolution, the interrupt flag (I) is set, the wheel stops turning and the letter S for STOP is displayed on the seven-segment
display. The wheel is now aligned to a known starting point, and the program is waiting for the interrupt
to be serviced by the appropriate routine. When the program continues to cycle through the main subroutines, the ALIGN routine is bypassed.
AN1285/D
MOTOR WHEEL
5V
5V
IR DETECTOR
IR EMITTER
IR STATUS TO MCU
STATOR COILS
A THRU D
FROM MCU
PORT
OPTIONAL
DRIVER
CIRCUITS
A
B
C
D
AN1285/D
CW Rotation
CCW Rotation
Four PORT C pins are used to drive the inputs to the stepper motor coils. Direct connection from the
MCU to the motor is fine for applications that require minimal drive. But for applications that require increased current drive capabilities, enhanced circuits are necessary.
One way to increase drive current is to use a motor controller IC designed specifically for that purpose.
An example of this device is the ULN2075B driver IC. Each IC contains four individual high-current Darlington switch and driver circuits. One IC satisfies the MCU to the stepper motor interfacing requirements.
Another approach is to use discrete components to form a push/pull amplifier. Each amplifier consists
of an NPN and a PNP transistor. The amplifier is arranged to generate a 12 volt output pulse to one
input of the motor in response to a +5 volt pulse coming from PORT C. Figure 6 shows the amplifier
configuration for one motor coil.
12V
05V PULSE
012V PULSE
FROM MCU
TO MOTOR COIL
AN1285/D
AN1285/D
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0001
0002
0003
0004
0005
0006
0007
C000
C000
C003
C006
C009
C00C
C00F
C012
C015
BDC017
BDC034
BDC03D
BDC04E
BDC058
BDC0D2
BDC0EF
20EC
C017
C01A
C01C
C01E
C020
CE1000
86FF
A707
8600
A709
C022
C024
C026
C028
C02A
C02C
C02E
C030
C032
C033
C605
D705
8610
9706
8600
A700
9704
9707
0E
39
C034
C036
C038
C03A
C03C
A608
8401
A708
9700
39
C03D
C03F
C041
C043
C045
C049
C04B
C04D
10
8690
A739
86A0
A730
18CE0026
1809
26FC
39
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
ADCTL
PORTA
PORTB
PORTCDR
PORTC
PORTDDR
PORTD
RESREG
ADON
ATEMP
ATEMP2
ATEMP3
ATEMP4
ATEMP5
TIMER
COUNTER
FLGIRQ
EQU
EQU
EQU
EQU
EQU
EQU
EQU
EQU
EQU
RMB
RMB
RMB
RMB
RMB
RMB
RMB
RMB
ORG
START
NEXT
$30
$0
$4
$7
$3
$9
$8
$31
$39
1
1
1
1
1
1
1
1
$C000
JSR INIT
JSR DIRECTION
JSR READAD
JSR COMSPD
JSR DISPLAY
JSR ALIGN
JSR TURN
BRA NEXT
;INITALIZE ROUTINE
;DIRECTION ROUTINE
;READ A/D ROUTINE
;COMPUTE SPEED ROUTINE
;7-SEGMENT DISPLAY ROUTINE
;POSITION CONTROL ROUTINE
;STEPPER MOTOR TURN ROUTINE
;INITALIZE ROUTINE
LDX #$1000
LDAA #$FF
;SET PORTC FOR OUTPUT
STAA PORTCDR,X
;TO TURN MOTOR
LDAA #$00
;SET PORTD FOR INPUT
STAA PORTDDR,X
;TO CONTROL MOTOR TURN
;DIRECTION
LDAB #5
;SET TIMER FOR # OF TIMES TO
STAB TIMER
;ACTIVATE EACH COIL
LDAA #10
;SET COUNTER FOR # OF STEPS
STAA COUNTER
;20 = 1 REVOLUTION
LDAA #$00
;SET PORTA FOR
STAA PORTA,X
;IR EMITTER DETECTOR
STAA ATEMP5
STAA FLGIRQ
CLI
RTS
INIT
DELAY
LDAA #$90
STAA ADON,X
LDAA #$A0
STAA ADCTL,X
LDY #$26
DEY
BNE
DELAY
RTS
AN1285/D
C04E
C050
C051
C052
C053
C054
C055
A631
43
44
44
44
44
9701
C057 39
C058
C05A
C05D
C05F
9601
BDC060
A704
39
C060
C062
C064
C066
C068
C06A
C06C
C06E
C070
C072
C074
C076
C078
C07A
C07C
C07E
C080
C082
C084
C086
C088
C08A
C08C
C08E
C090
C092
C094
C096
C098
C09A
C09C
C09E
C0A0
8100
273E
8101
273D
8102
273C
8103
273B
8104
273A
8105
2739
8106
2738
8107
2737
8108
2736
8109
2735
810A
2734
810B
2733
810C
2732
810D
2731
810E
2730
810F
272F
20BE
C0A2 86C0
MATCH
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
LDAA RESREG,X
COMA
LSRA
LSRA
LSRA
LSRA
STAA ATEMP2
RTS
DISPLAY
COMPDIS
DOWN0
;COMPARE A = 0
;COMPARE A = 01
;COMPARE A = 02
;COMPARE A = 03
;COMPARE A = 04
;COMPARE A = 05
;COMPARE A = 06
;COMPARE A = 07
;COMPARE A = 08
;COMPARE A = 09
;COMPARE A = 0A
;COMPARE A = 0B
;COMPARE A = 0C
;COMPARE A = 0D
;COMPARE A = 0E
;COMPARE A = 0F
;END OF POLL ROUTINE
;DISPLAY VALUE ON 7-SEG DISPLAY IF
;VALUE = 0
C0A4
C0A5
C0A7
C0A8
C0AA
C0AB
C0AD
C0AE
C0B0
39
86CF
39
8692
39
8686
39
868D
39
AN1285/D
118
119
120
121
122
123
124
125
126
DOWN1
DOWN2
DOWN3
DOWN4
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
#$CF
;VALUE = 1
#$92
;VALUE = 2
#$86
;VALUE = 3
#$8D
;VALUE = 4
11
86A4
39
86A1
39
86CE
39
8680
39
868C
39
8688
39
8680
39
86F0
39
8683
39
86B0
39
86B8
39
C0D2
C0D4
C0D6
C0D8
C0DA
C0DC
C0DE
C0E0
C0E2
C0E4
C0E6
C0E8
C0EA
C0EC
C0ED
C0EE
A600
8401
8100
2702
2613
9607
8100
2702
260B
86A4
A704
8610
9706
0E
3E
39
C0EF
C0F2
C0F4
C0F6
C0F8
C0FA
C0FC
C0FE
C100
7A0006
9606
8100
2702
2607
8610
9706
2001
39
C101 9600
C103 2653
C105 2700
C107
C109
C10B
C10E
C10F
C111
C113
12
D605
8601
BDC1A9
5A
26F8
D605
8603
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
DOWN5
DOWN6
DOWN7
DOWN8
DOWN9
DOWNA
DOWNB
DOWNC
DOWND
DOWNE
DOWNF
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
LDAA
RTS
#$A4
;VALUE = 5
#$A1
;VALUE = 6
#$CE
;VALUE = 7
#$80
;VALUE = 8
#$8C
;VALUE = 9
#$88
;VALUE = A
#$80
;VALUE = B
#$F0
;VALUE = C
#$83
;VALUE = D
#$B0
;VALUE = E
#$B8
;VALUE = F
NEX1
WAIT
TURN
BB
BBB
CW
CW1
CW3
LDAA PORTA,X
ANDA #$01
CMPA #$00
BEQ NEX1
BNE TURN
LDAA FLGIRQ
CMPA #$00
BEQ WAIT
BNE TURN
LDAA #$A4
STAA PORTB,X
LDAA #10
STAA COUNTER
CLI
WAI
RTS
;INTERRUPT TO BE SERVICED
AN1285/D
BDC1A9
5A
26F8
D605
8602
BDC1A9
5A
26F8
D605
8606
BDC1A9
5A
26F8
D605
8604
BDC1A9
5A
26F8
D605
860C
BDC1A9
5A
26F8
D605
8608
BDC1A9
5A
26F8
D605
8609
BDC1A9
5A
26F8
39
C158
C15A
C15C
C15F
C160
C162
C164
C166
C169
C16A
C16C
C16E
C170
C173
C174
C176
C178
C17A
C17D
C17E
C180
C182
C184
C187
C188
C18A
C18C
C18E
C191
C192
D605
8601
BDC1A9
5A
26F8
D605
8609
BDC1A9
5A
26F8
D605
8608
BDC1A9
5A
26F8
D605
860C
BDC1A9
5A
26F8
D605
8604
BDC1A9
5A
26F8
D605
8606
BDC1A9
5A
26F8
AN1285/D
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
CW2
CW6
CW4
CWC
CW8
CW9
CCW
CCW1
CCW9
CCW8
CCWC
CCW4
CCW6
JSR DELAY1
DECB
BNE CW3
LDAB TIMER
LDAA #$02
JSR DELAY1
DECB
BNE CW2
LDAB TIMER
LDAA #$06
JSR DELAY1
DECB
BNE CW6
LDAB TIMER
LDAA #$04
JSR DELAY1
DECB
BNE CW4
LDAB TIMER
LDAA #$0C
JSR DELAY1
DECB
BNE CWC
LDAB TIMER
LDAA #$08
JSR DELAY1
DECB
BNE CW8
LDAB TIMER
LDAA #$09
JSR DELAY1
DECB
BNE CW9
RTS
13
D605
8602
BDC1A9
5A
26F8
D605
8603
BDC1A9
5A
26F8
39
C1A9
C1AB
C1AD
C1AF
C1B2
C1B4
C1B6
C1B8
C1BA
C1BC
C1BE
C1C0
C1C2
C1C4
C1C8
C1CA
C1CC
A703
9601
9702
18DE02
1809
26FC
A600
8401
8101
270E
2600
8698
A704
18CE0FFF
1809
26FC
39
FFF2
FFF2 FFF4
FFF4 7C0007
FFF7 3B
14
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
LDAB TIMER
LDAA #$02
JSR DELAY1
DECB
BNE CCW2
LDAB TIMER
LDAA #$03
JSR DELAY1
DECB
BNE CCW3
RTS
CCW2
CCW3
DELAY1
COUNT
SEG
ZZ
NOSEG
;DELAY ROUTINE
STAA PORTC,X
LDAA ATEMP2
STAA ATEMP3
LDY ATEMP3
DEY
BNE COUNT
LDAA PORTA,X
ANDA #$01
CMPA #$01
BEQ NOSEG
BNE SEG
LDAA #$98
STAA PORTB,X
LDY #$FFF
DEY
BNE ZZ
RTS
INC FLGIRQ
RTI
AN1285/D
AN1285/D
15
E-mail:
support@freescale.com
USA/Europe or Locations Not Listed:
Freescale Semiconductor
Technical Information Center, CH370
1300 N. Alma School Road
Chandler, Arizona 85224
+1-800-521-6274 or +1-480-768-2130
support@freescale.com
Europe, Middle East, and Africa:
Freescale Halbleiter Deutschland GmbH
Technical Information Center
Schatzbogen 7
81829 Muenchen, Germany
+44 1296 380 456 (English)
+46 8 52200080 (English)
+49 89 92103 559 (German)
+33 1 69 35 48 48 (French)
support@freescale.com
Japan:
Freescale Semiconductor Japan Ltd.
Headquarters
ARCO Tower 15F
1-8-1, Shimo-Meguro, Meguro-ku,
Tokyo 153-0064
Japan
0120 191014 or +81 3 5437 9125
support.japan@freescale.com
Asia/Pacific:
Freescale Semiconductor Hong Kong Ltd.
Technical Information Center
2 Dai King Street
Tai Po Industrial Estate
Tai Po, N.T., Hong Kong
+800 2666 8080
support.asia@freescale.com
For Literature Requests Only:
Freescale Semiconductor Literature Distribution Center
P.O. Box 5405
Denver, Colorado 80217
1-800-441-2447 or 303-675-2140
Fax: 303-675-2150
LDCForFreescaleSemiconductor@hibbertgroup.com
AN1285/D