AN1307 Stepper Motor Control With dsPIC DSCs DS00001307B
AN1307 Stepper Motor Control With dsPIC DSCs DS00001307B
AN1307 Stepper Motor Control With dsPIC DSCs DS00001307B
Author: Sorin Manea and Srisairam Gaarlapati To learn more details about the hardware tool used,
Microchip Technology Inc. refer to the “dsPICDEM™ MCSM Development Board
User’s Guide” (DS70000610).
The application firmware is developed for demonstration The dsPICDEM MCSM Development Board was
using the dsPICDEM™ MCSM Development Board designed to work with drive voltages of up to 80V, and
(DM330022-1), which includes the dsPIC33CK64MP105 therefore, accommodate a wide range of stepper motors
External Op Amp Motor Control PIM (Part# MA330050-1), and driving algorithms. Since high voltages are used
along with a stepper motor (Leadshine Part relative to the stepper motor rated voltage, a very fast
Number 42HS03), which is supplied separately from reacting controller is needed. A PWM frequency of
Microchip as Part Number AC300024. The 24-volt power 40 kHz was chosen to have the smallest possible reac-
supply for the board is also supplied separately by tion time. For example, having a stepper motor with
Microchip as Part Number AC002013. As shown in 2.3 ohm and 4 mH per phase, driven at 80V, requires
Figure 1, the hardware consists of the dsPIC® DSC, the 70 µs to reach the current magnitude of 1.4A at 100%
drivers and two H-bridges. Each MOSFET in the dual duty cycle, whereas at 24V, 250 µs are needed to reach
H-bridge is controlled by one PWM signal. The powerful the current magnitude of 1.4A.
PWM module of the dsPIC® DSC features independent
or complementary control over each of the four PWM
pairs, plus an additional override function on each pin,
which gives more flexibility in controlling the power
MOSFETs.
3.3V PWM1H1
PWM1L1 J8
POT
PWM1H2
PWM1L2
Drivers M1
PWM1H3 M1 M3
S1 PWM1L3 M2
M2 M4
PWM2H1 M
PWM2L1 M3
DC_BUS M4
Plug In Module
(PIM) IMOTOR1
Amplifier
IMOTOR2
J1/J2
Programmer
Amplifier
FAULT
J4
UART
USB to Safe Current
USB Comparator Level
0.5
-0.5
-1
-1.5
1+ ¼ ½¼ 1 ¼½¼ 1+ ½ 1 ½ 1+ 1 1+ 1+
The values represented in the microstep table repre- values in the microstep table represent current refer-
sent different things depending on the operating control ences. In both cases, the table is scaled with the max-
mode. If the control mode is Open-Loop Voltage/Open- imum allowed voltage or current, as appropriate.
Loop Current Control, then this table represents the Figure 3 shows the current waveforms for full step
desired voltages to be applied to each winding. If the generation in closed-loop current control.
operating mode is Closed-Loop Current Control, the
Users can change microstep granularity in software. Note that when the value of step-size increases, the
Table 1 summarizes steps per electrical cycle for number of microsteps also increases.
various Step modes. When operating in Full Step mode, two options are
available to drive the motor:
TABLE 1: STEP MODES
• Full Step Wave Drive mode: In this mode, only
Step Mode Steps per Electric Cycle one phase is on at any moment in time.
Full Step 4 • Full Step Two-Phase On mode: In this mode, two
Half Step 8 phases are always on, but the polarity changes
every two steps.
1/4 Step 16
Note: Figure 5 shows the current waveforms
1/8 Step 32
during Wave Drive mode and Figure 6
1/16 Step 64 shows the current waveforms during
1/32 Step 128 Two-Phase On mode.
1/64 Step 256
FIGURE 10: FIXED CURRENT CONTROL AT 120 RPM WITH ALTERNATE DECAY
A simple control loop is used for controlling the current Fast Decay
amplitude. The maximum amplitude of the current in
both motor windings is sampled during one complete In fast decay, when the current is flowing through a
sine wave. If the maximum current amplitude is lower motor’s winding and all MOSFETs are switched off, the
than the desired value, the drive voltage is increased voltage on that winding will be equal to the negative of
gradually by adjusting the PWM duty cycle until the the supply voltage plus the drop voltage on two free-
desired current amplitude is reached. If the current is wheeling diodes, as shown in Figure 11. The decay
too high, the duty cycle is decreased, but not less than rate can be adjusted slightly by shorting one or two
the initial value corresponding to the rated motor diodes in the circuit with their corresponding
voltage. MOSFETs. However, the reverse voltage applied to the
As long as the drive voltage is higher than the motor coil will not change significantly since the voltage drop
rated voltage, this method provides an extended speed across a diode (1V) is much smaller than the supply
range over the classic open-loop (Fixed Voltage Con- voltage (24V). Still, the advantage of using this method
trol) approach. Another advantage to using this algo- is that the decaying current is flowing through the
rithm is that there is no need to retune for different MOSFET body diodes only briefly, until the MOSFET
motors. As long as the starting voltage produces a turns on. The MOSFET has a lower on resistance, and
lower current than desired, the algorithm will increase thus, the dissipated power will be much lower, which
this voltage until the desired current level amplitude is presents an advantage to the overall system power
reached. dissipation.
Another advantage of fast decay is the simplicity of the
DECAY MODES current feedback circuit, since motor current can be
read from the simple shunt resistor at all times. When
When a motor winding is turned off by the PWM, such the winding is driven, the current is positive. While the
as in a chopping circuit, the current through that current is dropping during fast decay, the current will be
winding starts to decay until it reaches zero or until the negative since the voltage is reversed across the wind-
winding is energized again. The rate at which the ing. Therefore, current is available on the shunt resistor
current decays depends on the configuration of the at all times.
H-bridge at that specific moment. The different current
decay methods are referred to as Decay modes in this
document.
FIGURE 12: FAST DECAY PWM TIMING FIGURE 14: FAST DECAY (REVERSE)
DIAGRAM PWM TIMING DIAGRAM
PWM1H1 PWM1H1
PWM1L1 PWM1L1
PWM1H2 PWM1H2
PWM1L2 PWM1L2
FIGURE 17: CURRENT SIGNALS FOR FAST AND SLOW DECAY MODES
COMBINING DECAY MODES low speeds. As the motor speed increases and the
desired current declines at a faster rate, the winding
In this application note, there are two ways that the current can no longer follow this curve using slow
Decay modes can be used. The first one is Fixed decay. Although operation in fast decay is noisier, it
Decay mode, in which either fast or slow decay is used, allows greater control of the current descent rate.
and this can be selected by the user in application
The two plots in Figure 18 show the difference between
code. The second option is Alternate Decay mode,
Fixed Decay mode, using Slow Decay and Alternate
where two Decay modes are combined while driving
Decay modes, using fast and slow decay. In Alternate
the stepper motor. Table 7 shows two Decay Operation
Decay mode (right plot), fast decay is only used when
modes.
the current is decreasing and only for a limited time
until the current reaches the desired level.
Fixed Decay
The advantage of using the Alternate Decay mode can
As mentioned earlier, in Fixed Decay mode, there is be seen at high speeds, where slow decay cannot pro-
only one Decay mode used during motor operation. vide a fast current drop rate as demanded by the
The recommended Decay mode is Slow Decay mode switching pattern. Also, the BEMF of the motor pre-
in the low MOSFET recirculation configuration. vents the current from decreasing fast enough. Fast
decay can be used to bring the current down faster to
Alternate Decay the desired level. Where fast decay is too aggressive or
needs to be used for a very short time, slow decay with
With all of the available Decay modes, the question diode recirculation can be used for a longer period as it
arises of which one to use and when. Slow decay pro- forces the current to decay faster than in the MOSFET
vides quieter motor operation and is good at relative Recirculation mode.
iref u i
HPI HM
+
–
In this application, a value of 70 µs was chosen as the equals 75 µs, which is very close to the desired time. In
desired rise time. The step response of the closed-loop all other cases, when the output is limited, the rise time
system is shown in Figure 23. We can only measure the will be longer because higher voltages than the DC bus
correct rise time as set by the PI parameters during the would be needed to achieve the set rise time.
last pulse, where the voltage output is not limited. We
can count six periods, which at 12.5 µs for one period
FIGURE 24: 1/4 STEP AT 840 RPM WITH A LOW ANTI-WINDUP GAIN
In Figure 25, the high anti-windup gain forces the The current waveform reference plays an important
controller output voltage to exit saturation sooner, and role here. If it is closer to a sine wave, the current will
therefore, changes the phase of the winding current rel- follow it better and the motor will have better torque. At
ative to the desired current. With this phase advance, high speeds, it is best to use the smallest possible
the current has enough time to rise into the winding microsteps in order to obtain the best motor torque.
before the rotor pole reaches the energized stator pole. However, at high speeds, the microstep changing rate
Further increasing the speed, the current amplitude becomes faster than the output frequency of 40 kHz.
keeps dropping until it eventually changes phase The dsPIC DSC device might also run out of time to
forced by the back-EMF. At this point, the current ampli- execute all of the step changes if they are very fast. For
tude will begin to rise again and the phase advance and these reasons, a value of approximately 20 µs for one
motor back-EMF work together to keep the motor run- microstep is implemented as the lower limit for one
ning, as shown in Figure 26. The motor torque at microstep time, regardless of the microstepping
2400 RPM is strong enough to operate the motor under resolution used. This means the top speed is higher for
a light load. As a comparison, the maximum speed Low-Resolution modes, such as full, half or quarter
achieved in the Open-Loop Control modes with the step, and lower for high-resolution microstepping.
same motor is around 200 RPM.
FIGURE 27: POSITION CONTROL WITH VARIABLE GAIN – EIGHT MOTOR ROTATIONS ARE
PERFORMED AT QUARTER STEP RESOLUTION
posFixed_gain decelerationRate
speed Ref = posGain pos Err = --------------------------------------------------------------------------------- pos Err
speedOut
speedRef speedMax
Start
Peripherals
Initialization
YES Process
Control Mode = NO
State = RUN DMCI
Button Control
Commands
YES
NO
Button NO
Pressed
YES
Step Sizes = NO
Increment Step-Size
1/64 Step
YES
YES Process
State = RUN Control Mode = NO
DMCI
Command Wait Button Control
Commands
YES
NO
Button NO
Pressed
YES
State = RUN
YES
State = INIT Initialize Control Algorithm State = RUN Command Wait
NO
ADC Interrupt
NO YES PWM NO
State = RUN Counting
Up
YES
Process Alternate
Decay for Both NO NO
Windings
Set PWM Duty Winding1 PI Loop Winding2 PI Loop Set PWM Duty
Cycles Update Decay Mode Update Decay Mode Cycles
Exit
32-Bit Timer
Interrupt
Timer1
Interrupt
Update Timer
Period
Call Speed and
Position Controllers
Increment
Microstep Counter
Process Fixed
Current Mode
NO
Calculate Position
Exit
PWM1H2
PWM1L2
PWM1H2
PWM1L2
Drive Decay
DC_BUS
Drive Decay
DC_BUS
PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1
PWM1H2
PWM1L2
Q1B Q2B Q1B Q2B
Drive Decay
DC_BUS DC_BUS Drive Decay
PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1
PWM1H2
PWM1L2
Q1B Q2B Q1B Q2B
Drive Decay
Drive Decay
DC_BUS DC_BUS
PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1
PWM1H2
PWM1L2
Q1B Q2B Q1B Q2B
PWM1H2
PWM1L2
• 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 operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual 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 protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.