-
AN3353 3-Axis Stepper Motor Control Using an 8-Bit PIC®
Microcontroller
Introduction
Author: Maria Loida Canada, Microchip Technology Inc.
Three-axis control applications, such as on a CNC machine,
robotics and dispensing machines, are widely used inthe industry.
Most often, each motor has a dedicated controller that facilitates
its speed control and sets its movementlimitations. The use of
multiple controllers in the development of the control system
implies a higher cost.
This project is created to develop a solution that can control
up to three motors simultaneously. Utilizing a single PIC®
MCU with its Core Independent Peripherals (CIPs), driving the
motors can be performed without additional burden tothe core. The
developed cost-effective solution provides an accurate linear motor
movement. The PIC® device can beused solely with the positions data
embedded in its firmware or can be used as a slave for the
applications requiringmore sophisticated control.
© 2020 Microchip Technology Inc. DS00003353A-page 1
-
Table of Contents
Introduction.....................................................................................................................................................1
1.
Overview.................................................................................................................................................
3
2. Stepper Motor
Control.............................................................................................................................4
2.1. Control
Overview..........................................................................................................................42.2.
Drive Circuit and Control
Process................................................................................................
52.3. 16-Bit High Resolution PWM for Control
Signal...........................................................................62.4.
Data
Transfer................................................................................................................................6
3. Stepper Motor Control
Characteristics....................................................................................................
7
3.1. Torque
Consideration...................................................................................................................
73.2. Stepping
Rate...............................................................................................................................7
4. Step Mode
Implementation.....................................................................................................................
8
4.1. Full-Step
Drive..............................................................................................................................84.2.
Half-Step
Drive...........................................................................................................................
114.3.
Microstepping.............................................................................................................................14
5. Firmware Flow
Diagram........................................................................................................................
16
6. 3-Axis Control
Performance..................................................................................................................
18
7.
Conclusion............................................................................................................................................
20
8. Appendix A:
Schematics.......................................................................................................................
21
9. Appendix B: MPLAB® Code Configurator (MCC) Peripheral
Initialization.............................................23
10. Appendix C: Source Code
Listing.........................................................................................................
25
The Microchip
Website.................................................................................................................................26
Product Change Notification
Service............................................................................................................26
Customer
Support........................................................................................................................................
26
Microchip Devices Code Protection
Feature................................................................................................
26
Legal
Notice.................................................................................................................................................
26
Trademarks..................................................................................................................................................
27
Quality Management
System.......................................................................................................................
27
Worldwide Sales and
Service.......................................................................................................................28
AN3353
© 2020 Microchip Technology Inc. DS00003353A-page 2
-
1. OverviewThis application note describes a practical solution
for controlling three motors independently. This application uses
asingle PIC18F-Q43 device to control the drive signal being fed to
the driver of stepper motors in 3-axes. With the useof a single
8-bit microcontroller, the implementation cost is substantially
reduced.
This application has the following key features:
• Full-step, half-step, and microstepping (1/4 and 1/16) modes•
Configurable steps/coordinate resolution• Speed and direction
control of each motor• Up to three motors simultaneously
controlled• Motor control using Core Independent Peripherals
(CIPs)
The interconnection of peripherals to control the signals used
for driving the motors is shown in Figure 1-1. The CIPsused in this
design are the new 16-bit high-resolution PWM, Complementary
Waveform Generator (CWG) and DirectMemory Access (DMA). The
full-bridge driver is used for bipolar stepper motor control. The
integration of on-chipperipherals like TMR0 and a conventional PWM
with the firmware, enables the system to reliably perform
3-axiscontrol with minimum software overhead.
Figure 1-1. 3-Axis Motor Control Block Diagram
FULL BRIDGEDRIVER
(WINDING A)
FULL BRIDGEDRIVER
(WINDING B)
Winding B
Winding A
TMR0
I/O
PIC18FXXQ43
VMOTOR
FULL BRIDGEDRIVER
(WINDING A)
FULL BRIDGEDRIVER
(WINDING B)
I/O
Winding B
FULL BRIDGEDRIVER
(WINDING A)
I/O
FULL BRIDGEDRIVER
(WINDING B)
STEPPERMOTOR 3
STEPPERMOTOR 1
VMOTOR
FIRMWARE(Step Mode Selection,
Step Count, Stepping Algorithm)
CWG1
CWG3
PWM3
CWG2
PWM2
16-bit PWM3
16-bit PWM2
4
PWM1
Step RateGenerator
Winding B
Winding A
STEPPERMOTOR 2
16-bit PWM1
1.8°/Step
1.8°/Step
1.8°/Step
DMA Controller
4
4
MEMORY(PWM Modulation)
Note: For this application, Leadshine 42HS03 motors were
used.
AN3353Overview
© 2020 Microchip Technology Inc. DS00003353A-page 3
-
2. Stepper Motor ControlA stepper motor is a type of motor that
rotates in discrete steps. It divides a full rotation into a number
of equal stepsand moves through it, one at a time. It converts the
input digital pulses into mechanical shaft rotation. It can be
drivento rotate a specific number of steps and stop precisely when
triggered to stop. For an in-depth discussion about thefundamentals
of stepper motors, refer to AN907: Stepping Motors
Fundamentals.
2.1 Control OverviewFigure 2-1 shows a block diagram of the
generic system used for controlling the three stepper motors. TMR0
acts asa step rate generator, which is primarily responsible for
controlling the speed of the motors. Every time the TMR0 rollsover,
the stepping sequence in the firmware is loaded to the CWG and GPIO
registers, while loading the PWM valuesthrough the DMA. Bipolar
motor control circuit, which is composed of two H-bridge drivers,
is used for driving eachmotor in a clockwise or counterclockwise
direction. The drive signal for each motor is a combination of CWG
andGPIO signals along with the 16-bit PWM output. Lastly, the
firmware dictates the limitation of movement, dependingon the
specified position.Figure 2-1. 3-Axis Motor Control Diagram
FIRMWARE(Stepping
Algorithm)
TMR0(Step Rate Generator)
PWM3
16-bit PWM3
16-bit PWM1
CWG3
16-bit PWM2
CWG1PWM1
CWG2PWM2
M1 H-BRIDGE DRIVER
(WINDING A)
M1 H-BRIDGE DRIVER
(WINDING B)
M2 H-BRIDGE DRIVER
(WINDING A)
M2 H-BRIDGE DRIVER
(WINDING B)
M3 H-BRIDGE DRIVER
(WINDING A)
M3 H-BRIDGE DRIVER
(WINDING B)
A
A’
A
A’
A
A’
B
B’
B
B’
B
B’
PROGRAM
MEMORY
I/O
I/O
I/O
DMA2
DMA3
DMA1
DMA4
DMA5
DMA6
Change in CWG1 Mode Signal
Change in CWG2 Mode Signal
Change in CWG3 Mode Signal
Current Modulation
Current Modulation
Current Modulation
Current Modulation
Current Modulation
Current Modulation
4
4
4
4
4
4
Change in drive signal mode
Change in drive signal mode
Change in drive signal mode
Drive CircuitsControl Signals
Data Transfer
AN3353Stepper Motor Control
© 2020 Microchip Technology Inc. DS00003353A-page 4
http://ww1.microchip.com/downloads/en/Appnotes/00907a.pdf
-
2.2 Drive Circuit and Control ProcessThe rotating magnetic field
and varying magnetic pole polarity (North/South) on the stator
causes the rotor to spin.The magnetic field and magnetic polarity
variation are produced by electrically energizing the two stepper
motorwindings (Winding A and B). The energization is controlled by
the CWG and PWM signals through the use of an H-bridge circuit.
Figure 2-2 depicts the current flow through the half-bridge circuit
during Forward and Reverse mode.The naming convention of Forward
and Reverse mode are adopted from the modes of CWG and will be
usedthroughout the document to indicate the state of Winding
mode.
When Winding A is in Forward mode, the current is flowing
through MOSFETs Q1 and Q4 down to ground, whileensuring that Q2 and
Q3 are OFF. Otherwise, when it operates in Reverse mode, the
current flows through Q2 andQ3, while keeping Q1 and Q4 OFF. The
same principle applies to the MOSFETs Q5, Q6, Q7 and Q8 on Winding
B.The switching of the MOSFETs in Winding B is implemented through
the use of CWG Forward and Reverse-FullBridge modes, while the
MOSFETs in Winding A are controlled by GPIO output and 16-bit PWM.
Thus, the drivemethod for each motor is a combination of firmware
and peripheral drive, to assure that all the motors are exposed
tosimilar control factors. Refer to TB3118: Complementary Waveform
Generator, for more details about the CWGperipheral. For the drive
circuits used, refer to section Appendix A: Circuit
Schematics.Figure 2-2. Drive Circuits and Control Signals
Vmotor
Winding AA A’
Q1
Q2
Q3
Q4
LATxy1
LATxy2(16-bit PWM)
LATxy3
LATxy4(16-bit PWM)
Vmotor
Winding BB B’
Q5
Q6
Q7
Q8
CWGxA
CWGxB
CWGxC
CWGxD
FORWARD MODE REVERSE MODE
AN3353Stepper Motor Control
© 2020 Microchip Technology Inc. DS00003353A-page 5
http://ww1.microchip.com/downloads/en/appnotes/90003118a.pdf
-
2.3 16-Bit High Resolution PWM for Control SignalThe 16-bit
Pulse-Width Modulator can produce a high-resolution modulation at
low frequency. In this application, thedrive resolution will be
mainly determined by the step mode implementation. The switching
frequency must be highenough to operate beyond the audio frequency.
The arrangement was made by choosing the PWM to operate in 10-bit,
having a 62.5 kHz switching frequency, which is within the range of
typical driver switching frequency.
The 16-bit PWM is used for driving the low-side MOSFETs of
Winding A for all motors. It is connected to both low-side MOSFETs
through Peripheral Pin Select (PPS), but it is never intended to
turn on both sides simultaneously.The 16-bit PWM has an independent
16-bit period timer, which is chosen to be HFINTOSC or equivalent
to 64 MHzin this application. The PWMOUT is in Left-Aligned mode.
For proper PWM operation, the registers PWMxPR andPWMxSaP1 must be
properly configured. The requested frequency can be attained by
setting up the PWMxPRregister. The value of this register is
equivalent to the number of PWM clock periods in the PWM period or
it can beexpressed as shown in Equation 2-1.
The PWMxSaP1 register determines the active period of slice “a”,
parameter 1 output. The duty cycle shown in Equation 2-2 can then
be calculated by getting the ratio of PWMxSaP1 to the PWMxPR
value.
The automatic loading of the PWMxSaP1 register is enabled
through setting the respective DMAx as the auto-loadtrigger source
in the PWMxLDS register. For more information about the 16-bit PWM,
refer to the device data sheet.Equation 2-1. PWM Period Register
Value������ = �������� ����������� ���� ��Equation 2-2. Duty Cycle
Calculation���� ����� = ������1������ × 100%
2.4 Data TransferThe Look-Up Table of modulation values is
initially stored in Programmable Flash Memory. The DMA is utilized
fortransferring the modulation values to PWM registers without the
CPU intervention, freeing up the core for doing othertasks. DMA1 is
used for transferring the values from the program memory to the
Slice 1 Output of 16-bit PWM1defined by the PWM1S1P1L and PWM1S1P1H
registers, illustrated in Figure 2-1, while DMA2 is used for
passing onthe modulation data from the program memory to the
conventional PWM1 registers: CCPR1L and CCPR1H. EachDMA channel is
tied to its specific PWM peripheral to ensure that the transfer
will simultaneously take placewhenever a transfer trigger is
received.
All the DMA channels are configured to start the data transfer
when TMR0 interrupt is triggered. The TMR0 isselected as a trigger
for starting the data transfer through the DMAnSIRQ (DMA Start
Interrupt Request SourceSelection) register. But for the interrupt
source to take effect, the SIRQEN (Start of Transfer Interrupt
RequestEnable) register of each DMA must be enabled.
This application provides a drive implementation in 1/4 and 1/16
microstepping. It is important to note that the arraysize of PWM
modulation values for 1/4 and 1/16 microstepping are different and
the respective DMA source sizeregister must be correctly
initialized to transfer all the data necessary to complete a full
step. Since the DMA isoperating in 8-bit, the DMA source size must
be equivalent to twice the number of array elements. For
1/4microstepping, the step resolution is 16, which means that the
DMA source size register must be equivalent to 32 or0x20; and for
1/16 microstepping with step resolution of 64 the DMA source size
must be 128 or 0x80. To learn moreinformation about DMA, refer to
TB3164: Direct Memory Access on 8-bit PIC® Microcontrollers and
refer to the devicedata sheet for proper peripheral
configuration.
AN3353Stepper Motor Control
© 2020 Microchip Technology Inc. DS00003353A-page 6
http://ww1.microchip.com/downloads/en/AppNotes/TB3164-Direct%20Memory-Access-on-8-bit-PIC-MCU-DS90003164B.pdf
-
3. Stepper Motor Control CharacteristicsFor proper operation,
stepper motor characteristics must be carefully considered.
Characteristics to consider aretorque, speed and stepping rate.
This section discusses about how these characteristics affect the
steppingimplementation.
3.1 Torque ConsiderationStepper motor maintains torque at
relatively low speed. The torque required to move the load must be
met by thestepper motor specifications. Stepping motor
manufacturers will specify several torques in their data sheets for
theirmotors. The torque magnitude depends on the driving technique,
stepping rate, and winding current.
3.2 Stepping RateStep rate refers to the speed at which the
hardware-firmware combination can send step pulses to the stepper
motordriver. It is expressed in PPS (Pulse Per Second) and dictates
the speed of the motor. To know more about how it isimplemented
using a PIC microcontroller, refer to the Stepping Rate section of
AN2326: High-Torque/High PowerBipolar Stepper Motor using 8-bit
PIC® MCUs.
AN3353Stepper Motor Control Characteristics
© 2020 Microchip Technology Inc. DS00003353A-page 7
http://ww1.microchip.com/downloads/en/AppNotes/00002326A.pdfhttp://ww1.microchip.com/downloads/en/AppNotes/00002326A.pdf
-
4. Step Mode ImplementationStep mode is a drive technique used
to rotate a stepper motor. It indicates the magnitude of a step
angle taken inevery energization of the stator windings. Each mode
has a corresponding stepping resolution and output torque. Thestep
modes that can be implemented in 3-axis control using a PIC18F-Q43
microcontroller are:
• Full-step Drive• Half-step Drive• Microstepping Drive (1/4 and
1/16)
The step mode implementation used in this application is
referenced in AN2326: High-Torque/High Power BipolarStepper Motor
using 8-bit PIC® MCUs. The principle of the stepping mode used is
the same, only the actualimplementation using the PIC18FXXQ43
device will be shown here. Likewise, the implementation is
applicable to allthree motors in 3-axis.
4.1 Full-Step DriveOn this drive, two phases are energized at
the same time. The drive circuit implementation is shown in Figure
4-1.The CWG controls the Winding B while the Winding A is
controlled by toggling the output of GPIO depending onstepping
algorithm. Meanwhile, Figure 4-2 illustrates the algorithm used for
stepping the motor. The individual outputstate of each pin is shown
to properly move the shaft’s position into its equivalent full-step
angle.
Figure 4-3 shows the firmware execution used in controlling the
signals. Every time the TMR0 interrupt is triggered,stepCounter
variables are incremented. stepCounter signifies that the specific
mode used is full-step and resetsto 0 whenever it reaches 4.
stepCounter2Mx are compared to their corresponding MxdesiredStep.
If the counterexceeds the desired step, the motor will be stopped
and axis_movementDone will be set. The definition of theaxis can be
x, y or z, depending on the specific motor movement it resembles.
After all theaxis_movementDone are set, the TMR0 is disabled to
make sure that it will not count and to avoid an interrupt to
beimproperly triggered. All the stepCounter2Mx are cleared to
ensure that the counting for the succeeding positionsbegins at
zero. The movementDone variable is used in the main program to
determine if all the motor movementsare finished and gives the
signal to prepare the system for another movement.
The switch case determined by the motorDir variable decides on
which direction the motors will rotate. The motorsare designed to
be driven simultaneously, so at every Interrupt event, the motorDir
will command which case toexecute. Each case contains the direction
of the motors and all the motors that used the drive table for
clockwise andcounterclockwise direction. The drive table requires a
specific set of IO and CWG for each motor.
Figure 4-1. Full-Step Drive
H-Bridge
H-BridgeCWGx
LATxy1
LATxy2
LATxy3
LATxy4
H-Bridge
H-BridgeCWGx
LATxy1
LATxy2
LATxy3
LATxy4
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 8
http://ww1.microchip.com/downloads/en/AppNotes/00002326A.pdfhttp://ww1.microchip.com/downloads/en/AppNotes/00002326A.pdf
-
Figure 4-2. Full-Step Drive Stepping Algorithm in Clockwise
Direction
Q1LATxy1
Q2 LATxy2
Q3 LATxy3
Q4 LATxy4
Q5 CWGxA
Q6 CWGxB
Q7 CWGxC
Q8 CWGxD
Win
din
g A
CWG Drive
STEP 1 STEP 2 STEP 3 STEP 4 STEP 1 STEP 2 STEP 3
Forward Forward ReverseReverse Forward Forward
ReverseReverse
Win
din
g B
Forward Reverse ForwardReverse Forward Reverse
ForwardReverseWindingMode
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 9
-
Figure 4-3. Full-Step Drive Stepping Algorithm Flowchart
Step_One:
LATxbits.LATxy1 = 0TRISxbits.TRISxy2 = 0LATxbits.LATxy3 = 1TRISxbits.TRISxy4 = 1CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Two:
LATxbits.LATxy1 = 1TRISxbits.TRISxy2 = 1LATxbits.LATxy3 = 0TRISxbits.TRISxy4 = 0CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Three:
LATxbits.LATxy1 = 1TRISxbits.TRISxy2 = 1LATxbits.LATxy3 = 0TRISxbits.TRISxy4 = 0CWGxCON0bits.MODE0 = 0 (Forward)
Step_Four:
LATxbits.LATxy1 = 0TRISxbits.TRISxy2 = 0LATxbits.LATxy3 = 1TRISxbits.TRISxy4 = 1CWGxCON0bits.MODE0 = 0 (Forward)
FULL STEP DRIVE TABLE CLOCKWISE DIRECTION
Step_One:
LATxbits.LATxy1 = 1TRISxbits.TRISxy2 = 1LATxbits.LATxy3 = 0TRISxbits.TRISxy4 = 0CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Two:
LATxbits.LATxy1 = 0TRISxbits.TRISxy2 = 0LATxbits.LATxy3 = 1TRISxbits.TRISxy4 = 1CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Three:
LATxbits.LATxy1 = 0TRISxbits.TRISxy2 = 0LATxbits.LATxy3 = 1TRISxbits.TRISxy4 = 1CWGxCON0bits.MODE0 = 0 (Forward)
Step_Four:
LATxbits.LATxy1 = 1TRISxbits.TRISxy2 = 1LATxbits.LATxy3 = 0TRISxbits.TRISxy4 = 0CWGxCON0bits.MODE0 = 0 (Forward)
FULL STEP DRIVE TABLE COUNTERCLOCKWISE DIRECTION
TMR0 Interrupt
stepCounter2M1 > M1desiredStep?
stepCounter2M1 = 0;M1stop();
x_movementDone = 1;
YES
NO
stepCounter2M2 > M2desiredStep?
stepCounter2M2 = 0;M2stop();
y_movementDone = 1;
YES
NO
stepCounter2M3 > M3desiredStep?
stepCounter2M3 = 0;M3stop();
z_movementDone = 1;
YES
NOstepCounter == 4?
stepCounter++;stepCounter2M1++;stepCounter2M2++;stepCounter2M3++;
Return
Clear stepCounterYES
NO
x_movementDone && y_movementDone && z_movementDone?
case 0: M1CWFull(); M2CWFull(); M3CWFull(); case 1: M1CCWFull(); M2CWFull(); M3CWFull(); case 2: M1CWFull(); M2CCWFull(); M3CWFull(); case 3: M1CCWFull(); M2CCWFull(); M3CWFull(); case 4: M1CWFull(); M2CWFull(); M3CCWFull(); case 5: M1CCWFull(); M2CWFull(); M3CCWFull(); case 6: M1CWFull(); M2CCWFull(); M3CCWFull(); case 7: M1CCWFull(); M2CCWFull(); M3CCWFull();
switch motorDir
TMR0_StopTimer(); stepCounter2M1 = 0;stepCounter2M2 = 0;stepCounter2M3 = 0;movementDone = 1;
YES
NO
NO
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 10
-
4.2 Half-Step DriveHalf-step drive alternates between two phases
on and a single phase on. It increases the resolution of the angle
byhalving the basic step angle, thus causing a smoother rotation
than full-step. Figure 4-4 shows the drive circuitimplementation
with the control signals coming from CWG and GPIO output. While,
Figure 4-5 illustrates the steppingalgorithm used in this drive
technique.
Figure 4-6 shows the firmware execution similar to the firmware
flow in Figure 4-3. The value of the stepCounterdoubled, which
clearly states that the algorithm used is twice as long as the
full-step algorithm. The drive table inclockwise and
counterclockwise direction for all the steps are also shown.Figure
4-4. Half-Step Drive Circuit
H-Bridge
H-BridgeCWGx
H-Bridge
H-BridgeCWGx
H-Bridge
H-BridgeCWGx
H-Bridge
H-BridgeCWGx
STEP 1 STEP 2
STEP 3 STEP 4
LATxy1
LATxy2
LATxy3
LATxy4
LATxy1
LATxy2
LATxy3
LATxy4
LATxy1
LATxy2
LATxy3
LATxy4
LATxy1
LATxy2
LATxy3
LATxy4
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 11
-
Figure 4-5. Half-Step Drive Stepping Algorithm
Q1LATxy1
Q2 LATxy2
Q3 LATxy3
Q4 LATxy4
Q5 CWGxA
Q6 CWGxB
Q7 CWGxC
Q8 CWGxD
Win
din
g A
CWG Drive
STEP 1 STEP 2 STEP 3 STEP 4 STEP 5 STEP 6 STEP 7
OFF Forward ForwardForward OFF Reverse ReverseReverse
Win
din
g B
WindingMode
Forward Forward ReverseOFF Reverse Reverse ForwardOFF
STEP 8 STEP 1 STEP 2
ForwardForward
ForwardOFF
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 12
-
Figure 4-6. Half-Step Drive Firmware Execution
TMR0 Interrupt
stepCounter2M1 > M1desiredStep?
stepCounter2M1 = 0;M1stop();
x_movementDone = 1;
YES
NO
stepCounter2M2 > M2desiredStep?
stepCounter2M2 = 0;M2stop();
y_movementDone = 1;
YES
NO
stepCounter2M3 > M3desiredStep?
stepCounter2M3 = 0;M3stop();
z_movementDone = 1;
YES
NO
stepCounter == 8?
stepCounter++;stepCounter2M1++;stepCounter2M2++;stepCounter2M3++;
Return
Clear stepCounterYES
NO
case 0: M1CWHalf(); M2CWHalf(); M3CWHalf(); case 1: M1CCWHalf(); M2CWHalf(); M3CWHalf(); case 2: M1CWHalf(); M2CCWHalf(); M3CWHalf(); case 3: M1CCWHalf(); M2CCWHalf(); M3CWHalf(); case 4: M1CWHalf(); M2CWHalf(); M3CCWHalf(); case 5: M1CCWHalf(); M2CWHalf(); M3CCWHalf(); case 6: M1CWHalf(); M2CCWHalf(); M3CCWHalf(); case 7: M1CCWHalf(); M2CCWHalf(); M3CCWHalf();
switch motorDir
Step_One: LATxbits.LATxy1 = 1 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 0 CWGxCON0bits.EN = 0 (OFF)
Step_Two: LATxbits.LATxy1 = 1 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 0 CWGxCON0bits.MODE0 = 0 (Forward)
Step_Three: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 0 (Forward)
Step_Four: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 0 LATxbits.LATxy3 = 1 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 0 (Forward)
HALF STEP DRIVE TABLE CLOCKWISE DIRECTION
Step_Five: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 0 LATxbits.LATxy3 = 1 TRISxbits.TRISxy4 = 1 CWGxCON0bits.EN =0 (OFF)
Step_Six:
LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 0 LATxbits.LATxy3 = 1 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Seven: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Eight: LATxbits.LATxy1 = 1 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 0 CWGxCON0bits.MODE0 = 1 (Reverse)
Step_One: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 0 LATxbits.LATxy3 = 1 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 0 (Forward)
Step_Two: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 0 (Forward)
Step_Three: LATxbits.LATxy1 = 1 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 0
TRISxbits.TRISxy2 = 1
LATxbits.LATxy3
= 0
TRISxbits.TRISxy4 = 0
CWGxCON0bits.EN = 0 (OFF)
HALF STEP DRIVE TABLE COUNTERCLOCKWISE DIRECTION
Step_Five: LATxbits.LATxy1 = 1 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 0 CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Six:
LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 1 LATxbits.LATxy3 = 0 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Seven: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 0 LATxbits.LATxy3 = 1 TRISxbits.TRISxy4 = 1 CWGxCON0bits.MODE0 = 1 (Reverse)
Step_Eight: LATxbits.LATxy1 = 0 TRISxbits.TRISxy2 = 0 LATxbits.LATxy3 = 1 TRISxbits.TRISxy4 = 1 CWGxCON0bits.EN = 0 (OFF)
x_movementDone && y_movementDone && z_movementDone?
TMR0_StopTimer(); stepCounter2M1 = 0;stepCounter2M2 = 0;stepCounter2M3 = 0;movementDone = 1;
YES
NO
CWGxCON0bits.MODE0 = 0 (Forward)
Step_Four: LATxbits.LATxy1 = 1
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 13
-
4.3 MicrosteppingMicrostepping is a manner of moving the stator
flux of a stepper motor smoothly. One type of microstepping
isconstant-torque, which is used in this application.
Constant-torque microstepping is achieved by simultaneouslyvarying
the current in both windings of a stepper motor.
The drive circuit used in this stepping mode is identical to the
full-step and half-step circuits. However, the controlsignals are
modulated instead of supplying a full-on or full-off signals. The
currents can be varied by changing thePWM percent modulation in
accordance with the Equation 4-1 and Equation 4-2.Equation
4-1. Winding A Current Formula�� = ���� × sin ���� ������ × 360����
����������Equation 4-2. Winding B Current Formula�� = ���� × cos
���� ������ × 360���� ����������Using the 1/16 microstepping,
assume that IMAX is equivalent to one and the drive is in step
number one. The sin of360° multiplied by the present step number
divided by 64 (1/16 microstepping resolution) results to 0.098.
Thisrepresents that the modulation of current in Winding A must
only be 9.8% of the maximum current. The modulation ofWinding A and
Winding B for the remaining 63 steps is calculated and plotted in
Figure 4-7. The step resolution of 64makes the torque graph
resemble a sinusoid. The stepping algorithm used in 1/16
microstepping is shown on Figure4-8.Figure 4-7. Phase Diagram and
Torque Response of Constant Torque 1/16 Microstepping
STEP 64(0% IA,
100% IB)
IA(%)
IB(%)
STEP 16(100% IA,
0% IB)
STEP 48(-100% IA,
0%IB)
Winding ATorque
Winding BTorque
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 14
-
Figure 4-8. Constant-Torque Microstepping Drive Signal
Q1LATxy1
Q2 PWMx
Q3 LATxy3
Q4 PWMx
Q5 CWGxA
Q6 CWGxB
Q7 CWGxC
Q8 CWGxD
Win
din
g A
CWG Drive
STEPSW
ind
ing
B
WindingMode
ForwardModulated
OFF ReverseModulated
Reverse OFFReverseModulated
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
ForwardModulated
Forward
OFF ReverseModulated
ReverseModulated
OFFReverse ForwardForwardModulated
ForwardModulated
AN3353Step Mode Implementation
© 2020 Microchip Technology Inc. DS00003353A-page 15
-
5. Firmware Flow DiagramThis section explains the firmware
design implemented for successfully driving the three motors in
3-axis.Figure 5-1. Firmware Flowchart
START
System_Initialize();
FeedingData();
SetPosition();
positionSet = 0;positionSet?
Motor_Driver();
Interrupt Initialize
dataFeedDone? dataFeedDone = 0;
StartMotorDrive();
positionSet?
movementDone?
__delay_ms(15); startDataFeed = 1;
YES
YES
YES
NO
NO
NO
Motor_Driver();
//Pick one type of drive// Full_Setup();// Half_Setup();// Microstep4_Setup(); Microstep16_Setup();
EXIT
FeedingData();
startDataFeed?
startDataFeed = 0;
coordInd++;x_coordinate = x_position[coordInd]; y_coordinate = y_position[coordInd]; z_coordinate = z_position[coordInd];
x_buffer = x_coordinate;y_buffer = y_coordinate;z_buffer = z_coordinate;
coordInd > max?
StopMotorDrive();
EXIT
YES
NO
YES
NO
dataFeedDone = 1;
The firmware flow starts with the SYSTEM_Initialize() routine.
This routine initializes pin configuration, oscillatorand all the
peripherals used in the application. It is followed by enabling
interrupts to address the functions requiringinterrupts. The
Motor_Driver() function contains the different drive
implementation. Pick one type of drive-byuncommenting the function
of the chosen drive. The selected drive will assign the value
ofstepping_mode_constant, which will be used for calculating the
number of steps required to rotate in a distinctposition. Notice
that the type of drive will only be setup once at the beginning of
the program, and the type of drivethat will be used will depend on
the application requirement and user’s discretion.
After all the initialization is done, the program will undergo a
continuous loop, executing the functions necessary tosuccessfully
move the motors in predefined positions. The FeedingData() function
contains the commands foracquiring and processing the desired
position. The positions initialized at the beginning of the program
are placed inthe axis_coordinate parameters for later processing.
The condition that tests coordInd against max variabletells if the
end position has been reached and signifies that the motors will be
stopped if the condition is satisfied.Otherwise, the dataFeedDone
variable will be set to be used as a test variable for the next
instruction.When the dataFeedDone test variable is satisfied, the
SetPosition() function will be executed. This functionserves a
vital role in the 3-axis motion control. This function consists of
checking if the individual motor movementsare completed through
testing the axis_movementDone variable. Provided that the previous
movement is finished,the current axes coordinate will be tested
against the previous axes coordinate. If the current and
previouscoordinates are not equal, two conditional statements will
be tested consequently. The first condition tests if thecurrent
coordinate is less than the previous coordinate; if so, the
resulting current coordinate will be the previous
AN3353Firmware Flow Diagram
© 2020 Microchip Technology Inc. DS00003353A-page 16
-
coordinate less the current coordinate shown in Equation 5-1.
The Mxdirection is set to the counterclockwisedirection and the DMA
is initialized for transferring the modulation data necessary for
moving in the counterclockwisedirection. However, if the current
coordinate is greater than the previous coordinate, the resulting
current coordinatewill be the current coordinate less the previous
coordinate, as shown in Equation 5-2. Furthermore, theMxdirection
will be set to the clockwise direction and the DMA source is
initialized to transfer the modulation datafor clockwise
direction.
The resulting current coordinate will be used to compute for the
motor’s desired step, which is shown in Equation 5-3.The constant
variable STEPS_PER_COORDINATE may take any value, depending on the
tool used for convertingrotational to linear motion. This variable
is introduced for flexibility in the application. The meaning of
axis inaxis_coordinate can be x, y or z, depending on the
coordinate, and the x in MxDesiredStep can be 1, 2 or 3,depending
on the motor number. The pairs created in this control scheme are
as follows: x_coordinate to M1,y_coordinate to M2 and z_coordinate
to M3.Equation 5-1. axis_coordinate <
axis_prevCoordinate�������������� = ������������������−
��������������Equation 5-2. axis_coordinate >
axis_prevCoordinate�������������� = ��������������−
������������������Equation 5-3. Motor Desired Step������������� =
����_���������� × �����_���_���������� × ��������_����_�������
�Subsequently, the previous coordinate equates to the current
coordinate to be the reference for successive positions.Then, the
motor drive is enabled by clearing the auto-shutdown feature of CWG
and setting the drive pins as output.The positionSet variable is
set, which primarily indicates that the setting of position is
done.Consequently, the function StartMotorDrive() will be
implemented, which literally starts the drive of the threemotors.
The testing of movementDone variable implies that all the motors
must be moved to their destination beforefeeding the next sets of
data for the subsequent movements. The loop will be continually
running, executing the tasksmentioned above until coordinate
movements were completed. Refer to section Appendix C: Source Code
Listing forthe complete source code.
AN3353Firmware Flow Diagram
© 2020 Microchip Technology Inc. DS00003353A-page 17
-
6. 3-Axis Control PerformanceIn order to show that the
microcontroller can provide drive signals simultaneously, a logic
analyzer is used to capturethe drive signal when operating in
different stepping modes.
Figure 6-1. Drive Signals for Three Motors from MCU Captured
using Logic Analyzer
A. Full-Step B. Half-Step
C. ¼ Microstepping D. 1/16 Microstepping
M1WA
M1WB
M2WA
M2WB
M3WA
M3WB
M1WA
M1WB
M2WA
M2WB
M3WA
M3WB
M1WA
M1WB
M2WA
M2WB
M3WA
M3WB
M1WA
M1WB
M2WA
M2WB
M3WA
M3WB
LEGEND:M1WA – Motor 1 Winding AM1WB – Motor 1 Winding BM2WA –
Motor 2 Winding A
M2WB – Motor 2 Winding BM3WA – Motor 3 Winding AM3WB – Motor 3
Winding B
Figure 6-2. Drive Signal Step Mode Comparison
A. Full-Step B. Half-Step
C. ¼ Microstepping C. 1/16 Microstepping
MxWA
MxWB
MxWAAnalog Signal
MxWBAnalog Signal
MxWA
MxWB
MxWAAnalog Signal
MxWBAnalog Signal
MxWA
MxWB
MxWAAnalog Signal
MxWBAnalog Signal
MxWA
MxWB
MxWAAnalog Signal
MxWBAnalog Signal
Figure 6-3. Data Coordinates Feed to the Motor
AN33533-Axis Control Performance
© 2020 Microchip Technology Inc. DS00003353A-page 18
-
Figure 6-1 shows the movement of motors in 3-axis in terms of
number of steps driven at full-step, half-step andmicrostepping
(1/4 and 1/16) with the speed of 180 RPM. It can be observed that
the time elapsed for all step modeimplementations is uniform
because they all operate at 180 RPM. But, it can be seen in Figure
6-2 that the requiredPPS (Pulse per Second) for each drive is
different to retain the speed of 180 RPM. The drive signals shown
are fromthe control signals of low-side MOSFETs of bipolar drive
circuits.
An example is shown in Figure 6-3, in which the positions are
defined as an array of coordinates. At an instance, thepositions
having the same element order for all the axis will be processed by
the firmware using Equations 5-1 or 5-2depending on the preceding
coordinate before passing onto Equation 5-3. Table 6-1 shows the
data of how thedesired steps are calculated for all the motors with
STEPS_PER_COORDINATE of 100, based on the coordinate givenin Figure
6-3.Table 6-1. Step Calculation for Three Motors in Full-Step
Mode
n Xn |Xn+1 - Xn| M1desiredStep Yn |Yn+1 - Yn| M2desiredStep Zn
|Zn+1 - Zn| M3desiredStep
0 0 0 0
1 3 3(CW) 300 11 11(CW) 1100 5 5(CW) 500
2 12 9(CW) 900 8 3(CCW) 300 3 2(CCW) 200
3 8 4(CCW) 400 12 4(CW) 400 9 6(CW) 600
4 2 6(CCW) 600 5 7(CCW) 700 2 7(CCW) 700
AN33533-Axis Control Performance
© 2020 Microchip Technology Inc. DS00003353A-page 19
-
7. ConclusionPIC18F-Q43 devices have the capacity to provide
control signals for 3-axis stepper motor control. Aside
fromproviding control signals, it is also capable of setting the
movement limitation to accurately move the motors to thedefined
positions. Different modes of stepping drive such as full-step,
half-step, 1/4 and 1/16 microstepping can beimplemented to achieve
the desired resolution. The CIPs such as CWG, 16-bit PWM and DMA
significantly reducedthe burden on the core, enabling the CPU to
process data for motor control movements.
AN3353Conclusion
© 2020 Microchip Technology Inc. DS00003353A-page 20
-
8. Appendix A: SchematicsFigure 8-1. Circuit Schematics
POWER
R4510kΩ
8
12
7
5
34
6
R4610kΩ
12
DC+
R4710kΩ
8
12
7
5
34
6
R4810kΩ
R30100mΩ
M1WA1
M1WA2
M1WA2
BHOM1WA
BLOM1WA
ALOM1WA
AHOM1WA
SHUNT_M1WA
FDS3992
FDS3992
FDS3992
FDS3992
IC20
IC20
IC23
IC23
J13811‐S1‐002‐10‐017101
Motor 1 Drive Circuit PIC18FxxQ
43
RA0RA1RA2RA3RA4RA5RE0
RB7RB6RB5RB4RB3RB2RB1RB0
RE1RE2VDDAVSSRA7RA6
AVDDVSSRD7RD6RD5RD4
CWG3B
RC0RC1RC2RC3RD0RD1
RC7RC6RC5RC4RD3RD2
123456789
10
11
12
13
14
15
16
17
18
19
20
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
CWG3CCWG3DLATA3
LATB7PWM1CWG1ACWG1CCWG1DCWG1BPWM1LATB0
PWM3PWM2PWM2
PWM3CWG2D
LATA4
LATE0LATE1
CWG2ACWG3A
CWG2BCWG2C
87654321
910111213
16
1415
BHOBHSBLOVSSVDDALOAHS
BHBENBHIBLIALIAHINC
AHBAHO
BHOM1WBBHSM1WBBLOM1WB
AGNDVDDM1WBALOM1WBAHSM1WBAHOM1WB
BHBM1WB+5V
AHBM1WB
MIC4606
IC16
12
C200.1µF
C190.1µF
+15V
J27811‐S1‐002‐10‐017101
VDDM1WB
C91µF
C101µF
CWG1DCWG1C
CWG1BCWG1A
910111213
16
1415
PWM1
BHBENBHIBLIALIAHINC
BHOBHSBLOVSSVDDALOAHSAHOAHB
BHOM1WABHSM1WABLOM1WAAGND
VDDM1WAALOM1WAAHSM1WAAHOM1WA
BHBM1WA+5V
LATB7
PWM1LATB0
AHBM1WA
MIC4606
87654321
IC15
C71µF
C81µF
12
C180.1µF
C170.1µF
+15V
J21811‐S1‐002‐10‐017101
VDDM1WA
R4910kΩ
8
12
7
5
34
6
R5010kΩ
12
DC+
R5110kΩ
8
12
7
5
34
6
R5210kΩ
R104100mΩ
M1WB1
M1WB2
M1WB2
BHOM1WB
BLOM1WB
ALOM1WB
AHOM1WB
SHUNT_M1WB
FDS3992
FDS3992
FDS3992
FDS3992
IC22
IC22
IC24
IC24
J14811‐S1‐002‐10‐017101
1 32
IN
GND_NO PAD GND_TAB
OUT IN
GROUND_1
OUT
GROUND_2
12
12
12
12
RAPC722X
J2C29100µF
C210.1µF
IC19MC7815CD2TG
C280.1µF
C447µF
J1811‐S1‐002‐10‐017101
J10811‐S1‐002‐10‐017101
J11811‐S1‐002‐10‐017101
J12811‐S1‐002‐10‐017101
IC21MC7805CDTG
C3647µF
C370.1µF
R271kΩ
LED15SML‐D12Y1WT86
R401Ω
C450.1µF
C46100µF
TP15014
TP25014
TP235014
+15V +5V AVDD
C3470µF
C300.1µF
R260Ω
DC+
Microcontroller
+5V
MCLR
AN3353Appendix A: Schematics
© 2020 Microchip Technology Inc. DS00003353A-page 21
-
Figure 8-2. Motor 2 and 3 Drive Circuit Schematics
R6110kΩ
8
12
7
5
34
6
R6210kΩ
12
DC+
R6310kΩ
8
12
7
5
34
6
R6410kΩ
R32100mΩ
M3WA1
M3WA2
M3WA2
BHOM3WA
BLOM3WA
ALOM3WA
AHOM3WA
SHUNT_M3WA
FDS3992
FDS3992
FDS3992
FDS3992
IC31
IC31
IC32
IC32
J17811‐S1‐002‐10‐017101
Motor 3 Drive Circuit
87654321
910111213
16
1415
BHOBHSBLOVSSVDDALOAHS
BHBENBHIBLIALIAHINC
AHBAHO
BHOM3WBBHSM3WBBLOM3WB
AGNDVDDM3WBALOM3WBAHSM3WBAHOM3WB
BHBM3WB+5V
AHBM3WB
MIC4606
IC30
12
C340.1µF
C330.1µF
+15VJ31
811‐S1‐002‐10‐017101
VDDM3WB
C311µF
C321µF
CWG3DCWG3C
CWG3BCWG3A
910111213
16
1415
BHBENBHIBLIALIAHINC
BHOBHSBLOVSSVDDALOAHSAHOAHB
BHOM3WABHSM3WABLOM3WAAGNDVDDM3WAALOM3WAAHSM3WAAHOM3WA
BHBM3WA+5V
AHBM3WA
MIC4606
87654321
IC29
C151µF
C161µF
12
C270.1µF
C260.1µF
+15VJ30
811‐S1‐002‐10‐017101
VDDM3WA
PWM3LATA4
PWM3LATA3
R5310kΩ
8
12
7
5
34
6
R5410kΩ
12
DC+
R5510kΩ
8
12
7
5
34
6
R5610kΩ
R31100mΩ
M2WA1
M2WA2
M2WA2
BHOM2WA
BLOM2WA
ALOM2WA
AHOM2WA
SHUNT_M2WA
FDS3992
FDS3992
FDS3992
FDS3992
IC25
IC25
IC26
IC26
J15811‐S1‐002‐10‐017101
Motor 2 Drive Circuit
87654321
910111213
16
1415
BHOBHSBLOVSSVDDALOAHS
BHBENBHIBLIALIAHINC
AHBAHO
BHOM2WBBHSM2WBBLOM2WB
AGNDVDDM2WBALOM2WBAHSM2WBAHOM2WB
BHBM2WB+5V
AHBM2WB
MIC4606
IC18
12
C250.1µF
C240.1µF
+15VJ28
811‐S1‐002‐10‐017101
VDDM2WB
C111µF
C111µF
CWG2DCWG2C
CWG2BCWG2A9
10111213
16
1415BHBEN
BHIBLIALIAHINC
BHOBHSBLOVSSVDDALOAHSAHOAHB
BHOM2WABHSM2WABLOM2WAAGNDVDDM2WAALOM2WAAHSM2WAAHOM2WB
BHBM2WA+5V
AHBM2WA
MIC4606
87654321
IC17
C111µF
C481µF
12
C230.1µF
C220.1µF
+15VJ29
811‐S1‐002‐10‐017101
VDDM2WA
PWM2LATE1
PWM2LATE0
R5710kΩ
8
12
7
5
34
6
R5810kΩ
12
DC+
R5910kΩ
8
12
7
5
34
6
R6010kΩ
R105100mΩ
M2WB1
M2WB2
M2WB2
BHOM2WB
BLOM2WB
ALOM2WB
AHOM2WB
SHUNT_M2WB
FDS3992
FDS3992
FDS3992
FDS3992
IC27
IC27
IC28
IC28
J16811‐S1‐002‐10‐017101
R6510kΩ
8
12
7
5
34
6
R6610kΩ
12
DC+
R6710kΩ
8
12
7
5
34
6
R6810kΩ
R106100mΩ
M3WB1
M3WB2
M3WB2
BHOM3WB
BLOM3WB
ALOM3WB
AHOM3WB
SHUNT_M3WB
FDS3992
FDS3992
FDS3992
FDS3992
IC33
IC33
IC34
IC34
J18811‐S1‐002‐10‐017101
STEPPERMOTOR 1
M1WA1
M1WA2
M1WB1
M1WB2
STEPPERMOTOR 2
M2WA1
M2WA2
M2WB1
M2WB2
STEPPERMOTOR 3
M3WA1
M3WA2
M3WB1
M3WB2
Winding A
Winding B
Winding A
Winding B
Winding A
Winding B
AN3353Appendix A: Schematics
© 2020 Microchip Technology Inc. DS00003353A-page 22
-
9. Appendix B: MPLAB® Code Configurator (MCC)
PeripheralInitializationMPLAB® Code Configurator (MCC) is an
easy-to-use plugin tool for MPLAB® X IDE that generates codes
forcontrolling the peripherals of Microchip microcontrollers, based
on the settings made in its Graphical User Interface(GUI). MCC is
utilized to easily configure the peripherals used in this motor
control application. Refer to the MPLAB®
Code Configurator User’s Guide (DS40001725) for further
information on how to install and set up the MCC inMPLAB® X
IDE.
The step-by-step process of using MCC in this application is
listed below.
1. In the system module, set the clock to HFINTOSC with the
highest available frequency of 64 MHz.2. Timer2 is used as a clock
source for CCP1/2/3. For the CCP to produce PWM, the Timer2 clock
source must
be set to FOSC/4. Enable the Timer.3. Configure the Timer0 in
16-bit Timer mode with MFINTOSC clock source, having a requested
period of 1.5 ms.
Enable the Timer interrupt and let the Timer0 be initially
disabled.4. For Motor 1 drive, configure PWM1, CCP1, CWG1, DMA
Channel 1 and DMA Channel 2.
4.1. Set up the PWM1 with HFINTOSC clock source, having a
requested frequency of 62.5 kHz and dutycycle of 50%. Enable the
PWM module.
4.2. CCP1 module must be in PWM mode with Timer2 as the selected
Timer, having a duty cycle of 50%.4.3. CWG1 must be configured with
CCP1_OUT as an input source, with Output mode in Forward Full-
Bridge mode and HFINTOSC as the selected clock source. Enable
CWG1.5. For Motor 2 drive, configure PWM2, CCP2, CWG2, DMA Channel
3 and DMA Channel 4.
5.1. Set up the PWM2 with HFINTOSC clock source, having a
requested frequency of 62.5 kHz and dutycycle of 50%. Enable the
PWM module.
5.2. CCP2 module must be in PWM mode with Timer2 as the selected
Timer, having a duty cycle of 50%.5.3. CWG2 must be configured with
CCP2_OUT as an input source, with Output mode in Forward Full-
Bridge mode and HFINTOSC as the selected clock source. Enable
CWG2.6. For Motor 3 drive, configure PWM3, CCP3, CWG3, DMA Channel
5 and DMA Channel 6.
6.1. Set up the PWM3 with HFINTOSC clock source, having a
requested frequency of 62.5 kHz and dutycycle of 50%. Enable the
PWM module.
6.2. CCP3 module must be in PWM mode with Timer2 as the selected
Timer, having a duty cycle of 50%.6.3. CWG3 must be configured with
CCP3_OUT as an input source, with Output mode in Forward Full-
Bridge mode and HFINTOSC as the selected clock source. Enable
CWG3.7. The DMA channels are configured by following these series
of steps:
7.1. In the DMAxCON0 register, enable the SIRQEN bit of DMA
Channel 1 and clear the EN bit (disable).7.2. Define the DMAxDSZL
size to 0x02, which means that the destination is 2 bytes wide.7.3.
Configure the DMAxSIRQ to TMR0. Depending on the microstepping
resolution, DMAxSSZL will be
0x80 if the mode will be 1/16 microstepping and 0x20 if it will
operate in 1/4 microstepping. Thesource and destination addresses
are defined in the firmware, depending on the direction of themotor
and motor number. Leave the configuration of other registers not
mentioned here, as is.
8. In the Pin Manager configuration, set up the input/output
pins of all the peripherals as shown in Figure B-1.9. After
configuring all the peripherals, click the “Generate Code” button
next to the Project Resources tab name
in the top left corner. This will generate a main.c file to the
project automatically. It will also initialize themodule and leave
an empty while(1) loop for custom code entry.
AN3353Appendix B: MPLAB® Code Configurator (MCC)...
© 2020 Microchip Technology Inc. DS00003353A-page 23
http://ww1.microchip.com/downloads/en/devicedoc/40001725b.pdfhttp://ww1.microchip.com/downloads/en/devicedoc/40001725b.pdf
-
Figure 9-1. PIC18FXXQ43 Pin Manager Configuration
AN3353Appendix B: MPLAB® Code Configurator (MCC)...
© 2020 Microchip Technology Inc. DS00003353A-page 24
-
10. Appendix C: Source Code ListingThe latest software version
can be downloaded from the Microchip website (www.microchip.com).
The user will findthe source code attached to the electronic
version of this application note. The latest version is v1.0.
AN3353Appendix C: Source Code Listing
© 2020 Microchip Technology Inc. DS00003353A-page 25
http://www.microchip.com
-
The Microchip WebsiteMicrochip provides online support via our
website at http://www.microchip.com/. This website is used to make
filesand information easily available to customers. Some of the
content available includes:
• Product Support – Data sheets and errata, application notes
and sample programs, design resources, user’sguides and hardware
support documents, latest software releases and archived
software
• General Technical Support – Frequently Asked Questions (FAQs),
technical support requests, onlinediscussion groups, Microchip
design partner program member listing
• Business of Microchip – Product selector and ordering guides,
latest Microchip press releases, listing ofseminars and events,
listings of Microchip sales offices, distributors and factory
representatives
Product Change Notification ServiceMicrochip’s product change
notification service helps keep customers current on Microchip
products. Subscribers willreceive email notification whenever there
are changes, updates, revisions or errata related to a specified
productfamily or development tool of interest.
To register, go to http://www.microchip.com/pcn and follow the
registration instructions.
Customer SupportUsers of Microchip products can receive
assistance through several channels:
• Distributor or Representative• Local Sales Office• Embedded
Solutions Engineer (ESE)• Technical Support
Customers should contact their distributor, representative or
ESE for support. Local sales offices are also available tohelp
customers. A listing of sales offices and locations is included in
this document.
Technical support is available through the website at:
http://www.microchip.com/support
Microchip Devices Code Protection FeatureNote the following
details of the code protection feature on Microchip devices:
• Microchip products meet the specification contained in their
particular Microchip Data Sheet.• Microchip believes that its
family of products is one of the most secure families of its kind
on the market today,
when used in the intended manner and under normal conditions.•
There are dishonest and possibly illegal methods used to breach the
code protection feature. All of these
methods, to our knowledge, require using the Microchip products
in a manner outside the operatingspecifications contained in
Microchip’s Data Sheets. Most likely, the person doing so is
engaged in theft ofintellectual property.
• Microchip is willing to work with the customer who is
concerned about the integrity of their code.• Neither Microchip nor
any other semiconductor manufacturer can guarantee the security of
their code. Code
protection does not mean that we are guaranteeing the product as
“unbreakable.”
Code protection is constantly evolving. We at Microchip are
committed to continuously improving the code protectionfeatures of
our products. Attempts to break Microchip’s code protection feature
may be a violation of the DigitalMillennium Copyright Act. If such
acts allow unauthorized access to your software or other
copyrighted work, youmay have a right to sue for relief under that
Act.
Legal NoticeInformation contained in this publication regarding
device applications and the like is provided only for
yourconvenience and may be superseded by updates. It is your
responsibility to ensure that your application meets with
AN3353
© 2020 Microchip Technology Inc. DS00003353A-page 26
http://www.microchip.com/http://www.microchip.com/pcnhttp://www.microchip.com/support
-
your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHEREXPRESS OR IMPLIED, WRITTEN OR ORAL,
STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION,INCLUDING BUT
NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY
ORFITNESS FOR PURPOSE. Microchip disclaims all liability arising
from this information and its use. Use of Microchipdevices in life
support and/or safety applications is entirely at the buyer’s risk,
and the buyer agrees to defend,indemnify and hold harmless
Microchip from any and all damages, claims, suits, or expenses
resulting from suchuse. No licenses are conveyed, implicitly or
otherwise, under any Microchip intellectual property rights
unlessotherwise stated.
TrademarksThe Microchip name and logo, the Microchip logo,
Adaptec, AnyRate, AVR, AVR logo, AVR Freaks, BesTime,BitCloud,
chipKIT, chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex,
flexPWR, HELDO, IGLOO, JukeBlox,KeeLoq, Kleer, LANCheck, LinkMD,
maXStylus, maXTouch, MediaLB, megaAVR, Microsemi, Microsemi logo,
MOST,MOST logo, MPLAB, OptoLyzer, PackeTime, PIC, picoPower,
PICSTART, PIC32 logo, PolarFire, Prochip Designer,QTouch, SAM-BA,
SenGenuity, SpyNIC, SST, SST Logo, SuperFlash, Symmetricom,
SyncServer, Tachyon,TempTrackr, TimeSource, tinyAVR, UNI/O,
Vectron, and XMEGA are registered trademarks of Microchip
TechnologyIncorporated in the U.S.A. and other countries.
APT, ClockWorks, The Embedded Control Solutions Company,
EtherSynch, FlashTec, Hyper Speed Control,HyperLight Load,
IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision
Edge, ProASIC, ProASIC Plus,ProASIC Plus logo, Quiet-Wire,
SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub, TimePictra,
TimeProvider,Vite, WinPath, and ZL are registered trademarks of
Microchip Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any
Capacitor, AnyIn, AnyOut, BlueSky, BodyCom,CodeGuard,
CryptoAuthentication, CryptoAutomotive, CryptoCompanion,
CryptoController, dsPICDEM,dsPICDEM.net, Dynamic Average Matching,
DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP,INICnet,
Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo,
memBrain, Mindi, MiWi, MPASM, MPF,MPLAB Certified logo, MPLIB,
MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation,
PICDEM,PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon,
QMatrix, REAL ICE, Ripple Blocker, SAM-ICE, Serial QuadI/O,
SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total Endurance,
TSHARC, USBCheck, VariSense,ViewSpan, WiperLock, Wireless DNA, and
ZENA are trademarks of Microchip Technology Incorporated in the
U.S.A.and other countries.
SQTP is a service mark of Microchip Technology Incorporated in
the U.S.A.
The Adaptec logo, Frequency on Demand, Silicon Storage
Technology, and Symmcom are registered trademarks ofMicrochip
Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany
II GmbH & Co. KG, a subsidiary of MicrochipTechnology Inc., in
other countries.
All other trademarks mentioned herein are property of their
respective companies.© 2020, Microchip Technology Incorporated,
Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-5479-3
Quality Management SystemFor information regarding Microchip’s
Quality Management Systems, please visit
http://www.microchip.com/quality.
AN3353
© 2020 Microchip Technology Inc. DS00003353A-page 27
http://www.microchip.com/quality
-
AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPECorporate Office2355
West Chandler Blvd.Chandler, AZ 85224-6199Tel: 480-792-7200Fax:
480-792-7277Technical Support:http://www.microchip.com/supportWeb
Address:http://www.microchip.comAtlantaDuluth, GATel:
678-957-9614Fax: 678-957-1455Austin, TXTel:
512-257-3370BostonWestborough, MATel: 774-760-0087Fax:
774-760-0088ChicagoItasca, ILTel: 630-285-0071Fax:
630-285-0075DallasAddison, TXTel: 972-818-7423Fax:
972-818-2924DetroitNovi, MITel: 248-848-4000Houston, TXTel:
281-894-5983IndianapolisNoblesville, INTel: 317-773-8323Fax:
317-773-5453Tel: 317-536-2380Los AngelesMission Viejo, CATel:
949-462-9523Fax: 949-462-9608Tel: 951-273-7800Raleigh, NCTel:
919-844-7510New York, NYTel: 631-435-6000San Jose, CATel:
408-735-9110Tel: 408-436-4270Canada - TorontoTel: 905-695-1980Fax:
905-695-2078
Australia - SydneyTel: 61-2-9868-6733China - BeijingTel:
86-10-8569-7000China - ChengduTel: 86-28-8665-5511China -
ChongqingTel: 86-23-8980-9588China - DongguanTel:
86-769-8702-9880China - GuangzhouTel: 86-20-8755-8029China -
HangzhouTel: 86-571-8792-8115China - Hong Kong SARTel:
852-2943-5100China - NanjingTel: 86-25-8473-2460China - QingdaoTel:
86-532-8502-7355China - ShanghaiTel: 86-21-3326-8000China -
ShenyangTel: 86-24-2334-2829China - ShenzhenTel:
86-755-8864-2200China - SuzhouTel: 86-186-6233-1526China -
WuhanTel: 86-27-5980-5300China - XianTel: 86-29-8833-7252China -
XiamenTel: 86-592-2388138China - ZhuhaiTel: 86-756-3210040
India - BangaloreTel: 91-80-3090-4444India - New DelhiTel:
91-11-4160-8631India - PuneTel: 91-20-4121-0141Japan - OsakaTel:
81-6-6152-7160Japan - TokyoTel: 81-3-6880- 3770Korea - DaeguTel:
82-53-744-4301Korea - SeoulTel: 82-2-554-7200Malaysia - Kuala
LumpurTel: 60-3-7651-7906Malaysia - PenangTel:
60-4-227-8870Philippines - ManilaTel: 63-2-634-9065SingaporeTel:
65-6334-8870Taiwan - Hsin ChuTel: 886-3-577-8366Taiwan -
KaohsiungTel: 886-7-213-7830Taiwan - TaipeiTel:
886-2-2508-8600Thailand - BangkokTel: 66-2-694-1351Vietnam - Ho Chi
MinhTel: 84-28-5448-2100
Austria - WelsTel: 43-7242-2244-39Fax: 43-7242-2244-393Denmark -
CopenhagenTel: 45-4450-2828Fax: 45-4485-2829Finland - EspooTel:
358-9-4520-820France - ParisTel: 33-1-69-53-63-20Fax:
33-1-69-30-90-79Germany - GarchingTel: 49-8931-9700Germany -
HaanTel: 49-2129-3766400Germany - HeilbronnTel:
49-7131-72400Germany - KarlsruheTel: 49-721-625370Germany -
MunichTel: 49-89-627-144-0Fax: 49-89-627-144-44Germany -
RosenheimTel: 49-8031-354-560Israel - Ra’ananaTel:
972-9-744-7705Italy - MilanTel: 39-0331-742611Fax:
39-0331-466781Italy - PadovaTel: 39-049-7625286Netherlands -
DrunenTel: 31-416-690399Fax: 31-416-690340Norway - TrondheimTel:
47-72884388Poland - WarsawTel: 48-22-3325737Romania - BucharestTel:
40-21-407-87-50Spain - MadridTel: 34-91-708-08-90Fax:
34-91-708-08-91Sweden - GothenbergTel: 46-31-704-60-40Sweden -
StockholmTel: 46-8-5090-4654UK - WokinghamTel: 44-118-921-5800Fax:
44-118-921-5820
Worldwide Sales and Service
© 2020 Microchip Technology Inc. DS00003353A-page 28
http://www.microchip.com/supporthttp://www.microchip.com
IntroductionTable of Contents1. Overview2. Stepper
Motor Control2.1. Control Overview2.2. Drive Circuit and
Control Process2.3. 16-Bit High Resolution PWM for Control
Signal2.4. Data Transfer
3. Stepper Motor Control Characteristics3.1. Torque
Consideration3.2. Stepping Rate
4. Step Mode Implementation4.1. Full-Step
Drive4.2. Half-Step Drive4.3. Microstepping
5. Firmware Flow Diagram6. 3-Axis Control
Performance7. Conclusion8. Appendix A:
Schematics9. Appendix B: MPLAB® Code Configurator (MCC)
Peripheral Initialization10. Appendix C: Source Code
ListingThe Microchip WebsiteProduct Change Notification
ServiceCustomer SupportMicrochip Devices Code Protection
FeatureLegal NoticeTrademarksQuality Management SystemWorldwide
Sales and Service