This example presents a bipolar stepper drive with acceleration and deceleration ramps.
The bipolar stepper motor will be driven in three modes:
In Full-Step mode, the motor rotates complete mechanical steps at once - for a standard 200 full-steps/revolution stepper motor, this would be 1.8 degrees for a sub-step. The motor's two coils are powered sequentially in four full-steps. The sequence determines the stepper motor's direction of rotation. Full-Step mode is simple to control, but can cause vibration and noise due to the larger step angle. Another drawback of using this mode is that the stepper motor's current consumption is at the highest level, having the lowest efficiency. This mode is often used in applications where the drive algorithm simplicity is more important than the smoothness of operation or precision.
In Half-Step mode, four extra steps are inserted between the four initial full-steps, which makes the stepper motor rotate by half of its full-step angle. This effectively doubles the number of mechanical steps per revolution - for a standard 200 full-steps/revolution stepper motor, this would be 0.9 degrees for a complete step, resulting in a smoother operation and less vibration compared to the Full-Step mode.
The benefits of having a balance between simplicity, efficiency, smoothness of operation
and a lower current consumption outweigh the increased complexity of the control algorithm needed for this
mode. It also provides double resolution of the Full-Step mode.
The Microstep mode divides the full-step angle into even smaller steps, providing even smoother operation and higher precision. The number of microsteps per full-step can vary, but common values are 8, 16, 32, or even 256 microsteps per full-step. Even though this mode requires the most complex control algorithms, the current consumption is lower than in Full-Step and Half-Step modes, providing the highest precision, the least vibration and less noise. Microstep mode is often used in applications where precision and smoothness of operation are paramount, such as in CNC machines.
More details and code examples on the AVR16EB32 can be found at the following links:
- MPLAB® X IDE v6.20 or newer
- AVR-Ex DFP-2.9.197 or newer Device Pack
- MPLAB® XC8 compiler v2.46
- MPLAB® Code Configurator (MCC) v 5.5.0
- AVR® EB Curiosity Nano
- Multi-Phase Power Board (MPPB)
- AVR-EB Curiosity Nano Adaptor to MPPB
- Motor used in the application: 17HS8401S Stepper Motor, but not mandatory
- A Voltage Power Supply (6-50V and 1-3A current capability, depending on the chosen motor)
The AVR16EB32 Curiosity Nano Development board is used along with the MPPB, AVR-EB Cnano to MPPB Adaptor Board, one stepper motor and a voltage power supply.
Adjust the supply voltage and the current limit according to the stepper motor used.
Caution: Do not change voltage during the stepper movement. The application assumes the supply voltage is stable.
The supply voltage is checked and current adjusted only before movement.
The application is periodically calling the Stepper_Move
function with the parameters:
initial position, steps (to go), acceleration, deceleration, speed and vbus (bus voltage). In this implementation, the application automatically adjusts the drive amplitude according the the power supply voltage, trying to keep the current constant through the coils.
The function precalculates the acceleration and deceleration time based on the speed and the number of steps the end-user wants the motor to move.
After the computation is finished, the StepAdvance
function is called, which controls the movement of the motor. The stepper drive schema is controlled by the StepAdvance
function. StepAdvance
is generating a wave 90 electrical degrees shifted.
After movement completion, the Stepper_Move
returns the final position.
The drive is updated at every Pulse-width modulation (PWM) cycle, once every 50 µs.
Depending on the number of steps requested and the acceleration/deceleration values, the motor mmay reach the desired speed limit (figure 1) or not (figure 2). If the distance to reach the speed is too small, the motor will accelerate and then start decelerating without reaching the limit speed.
Figure 1. The stepper motor has time to accelerate, reach and cruise at the speed limit, followed by the deceleration period
Figure 2. The distance to reach the speed is too small. The stepper motor will accelerate and then start decelerating without reaching the limit speed
Ideal waveforms of the current that flows through the coils. Each full-step represents a stepper motor mechanical movement of 1.8 degrees.
Full-Step: four full-steps sequence
For a better aproximation, we are inserting intermediary steps, called half-steps.
Ideal waveforms of the current that flows through the coils. Each half-step represents a stepper motor mechanical movement of 0.9 degrees.
Half-Step: eight half-steps sequence
Note: 0.707 represents sin(45 degrees) or cos(45 degrees).
To obtain a smoother movement and better positioning, the driving waveform is an approximation of a sinewave.
In the current implementation, the 1.8 degrees precision of the full-step are divided by 32 microstep, resulting in a microstepping precision of less than 0.06 degrees.
Ideal waveforms of the current that flows through the coils.
Back to Top
Note: for a standard 200 full-steps/revolution stepper motor 1.8 mechanical degrees represent 360 electrical degrees (360 electrical degrees/200-steps is 1.8 mechanical degrees).
The motor parameters can be configured in stepper.h
. The parameters that can be configured are the mechanical full-step angle and the mode in which the stepper should move (Full-Step, Half-Step or Microstep).
The STEP_SIZE
parameter is fixed and it represents the step angle of the stepper used in Full-Step mode. For this application, a stepper motor with 200 steps/mechanical revolution is used. 360 mechanical degrees/revolution divided by 200 steps results in 1.8 mechanical degrees/step.
The R
parameter is fixed and it represents the motor windings resistance, expressed in [ohm].
The I_OUT
parameter is fixed and it represents the motor peak current expressed in [mA]
The KV
parameter is fixed and it represents the BEMF current compensation. At higher speed, the BEMF increases significantly and needs to be compensated, in order to maintain a constant torque, by proportionally increasing the drive amplitude.
The application contains an option that allows the stepper coils to still remain energized after the steper motor has finished the movement. The user can enable or disable this functionality with the help of the RELEASE_IN_IDLE
flag.
The flag is by default true
, which means that after every movement the current through the coils is stopped. If the user needs the coils to remain energized while the motor is idle, the RELEASE_IN_IDLE
flag must be set to false
.
To change the stepping mode, uncomment the corresponding macro in stepper.h
.
Back to Top
Flowchart for the Stepper_Move
function
Sub-step division:
1 full-step = reference
1 half-step = 1/2 full-step
1 microstep = 1/32 full-step
The CheckSteps
function is called by the Stepper_Move
function. The function is needed to provide variable length delay inversely proportional to the momentary speed of the stepper motor. It is using a fractional computation to avoid divisions.
time_flag
is a shared boolean variable which is set by the Timer/Counter type E (TCE) interrupt, every 50 microseconds.
Flowchart for the CheckSteps
function
Back to Top
To generate this project using MPLAB X IDE and the MPLAB X Code Configurator MPLAB X Code Configurator (MCC) with Melody (MCC Clasic is not supported for this device), follow the next steps:
1. Open MPLAB X IDE and create a new project for the AVR16EB32 device.
2. Open MCC from the toolbar (more information on how to install the MCC plug-in can be found here).
3. From the MCC Content Manager tab click the Select MCC Melody button and then click Finish.
4. Click Project>Resources>System>CLKCTRL, then do the following configuration:
- Disable the Prescaler enable button
5. Click Project>Resources>System>Interrupt Manager, then do the following configuration:
- Enable Global Interrupts
6. To add the TCE module, go to Device Resources>Drivers>Timer>TCE0, then do the following configuration:
- Module Enable: Must be enabled by default, if not just toggle the button (it turns blue if enabled)
- Clock Selection: System clock (by default the divider must be 1 - System clock)
- Counter Direction: UP
- Waveform Generation Mode: Single-Slope PWM mode with overflow on TOP
- Requested Period [s]: 0.00005
- Duty Cycle 0 [%]: 0
- Duty Cycle 1 [%]: 0
- Duty Cycle 2 [%]: 0
- Duty Cycle 3 [%]: 0
- Waveform Output n: Check the boxes from the Enable column for Waveform Output 0, 1, 2, 3
- Scale mode: CMP values are scaled from Bottom, 50% DC (duty cycle)
- Scaled Writing to registers: Normal
- Amplitude Control Enable: Toggle the button (it turns blue if enabled)
- Amplitude Value: 1
- Generate ISR: Toggle the button (it turns blue if enabled)
-Overflow Interrupt Enable: Toggle the button (it turns blue if enabled)
7. To add the WEX module, go to Device Resources>Drivers>WEX>WEX0, then do the following configuration:
- Input Matrix: Direct
- Update Source: TCE (the update condition for the output signals will be dictated by TCE)
- Override Settings: Check all the boxes from the Output Enable column for the Waveform Output [0-7]
- Dead-time Insertion Channel 0 Enable: Toggle the button (it turns blue if enabled)
- Dead-time Insertion Channel 1 Enable: Toggle the button (it turns blue if enabled)
- Dead-time Insertion Channel 2 Enable: Toggle the button (it turns blue if enabled)
- Dead-time Insertion Channel 3 Enable: Toggle the button (it turns blue if enabled)
- Requested Dead-time Low Side (μs) : 0.2
- Requested Dead-time High Side (μs) : 0.2
8. To add the ADC module, go to Device Resources>Drivers>ADC>ADC0, then do the following configuration:
- Prescaler Value: must be "System clock divided by 2" by default; if not select "System clock divided by 2"
- Sample Duratiom: 255
- ADC Enable: Toggle the button (it turns blue if enabled)
- Left Adjust
- Free-Running mode Enable: Toggle the button (it turns blue if enabled)
- Reference select: VDD
- Analog Positive Channel Selection: ADC input pin 20
- Analog Negative Channel Selection: Ground
9. In the Pin Grid View tab check if the WEX_WO [0-7] pins are locked as outputs on PORTA. When the boxes from the Enable column in the Output Settings of WEX are checked, the pins are also locked. To change the PORT simply click a pin from another PORT in Pin Grid View.
Pin | Configuration |
---|---|
PA0 | TCE and WEX WO0 |
PA1 | TCE and WEX WO1 |
PA2 | TCE and WEX WO2 |
PA3 | TCE and WEX WO3 |
PA4 | TCE and WEX WO4 |
PA5 | TCE and WEX WO5 |
PA7 | TCE and WEX WO6 |
PA7 | TCE and WEX WO7 |
PF4 | ADC |
10. To add the BOD module, go to Device Resources>System>BOD, then do the following configuration:
-BOD Operation in Active Mode: Enabled in continuous mode
-BOD Level: 2.7V
-BOD Operation in Sleep Mode: Disabled
11. To add the UART module, go to Device Resources>Drivers>UART>UART0, then do the following configuration:
- Requested Baudrate: 115200
-Redirect Printf to UART Enable: Toggle the button (it turns blue if enabled)
12. In the Project Resources tab, click the Generate button so that MCC will generate all the specified drivers and configurations.
12. Add the main.c
, stepper.c
and stepper.h
files to the project.
Back to Top
Caution:
Do not change power supply voltage during stepper movement. The application assumes the supply voltage is stable.
The Supply voltage is checked and the current is adjusted only before movement.
-
Be sure the power supply is turned off.
-
Adjust the voltage of the power supply according to the stepper motor specs.
-
Adjust the current limit of the power supply according to the stepper motor specs.
-
Connect the power supply to the MPPB.
-
Connect the AVR16EB32 board the the MPPB Adapter.
-
Connect the MPPB Adapter to the MPPB.
-
Connect the AVR16EB32 board to the PC.
-
Open the
avr16eb32-stepper-full-ramp-mcc.X
or solution in MPLAB X IDE. -
Right click the project and select Set as main project.
- Build the
avr16eb32-stepper-full-ramp-mcc.X
project by clicking Clean and Build Project.
- Click Make and Program Device to program the project to the board.
- Start the power supply.
Back to Top
On the terminal, the information about the movement of the stepper is shown.
Screen capture from Microstep and Full-Ramp configuration.
Full-Step
Half-Step
Microstep
Colours in the figures:
Yellow and blue represent the voltages at the two ends of coil A,
Green represents the current through coil A.
On all three traces a low pass filtering from the oscilloscope is applied. This removes the PWM artifacts and makes visible the short time average values.
Back to Top
This application shows how to drive a stepper motor in Full-Step, Half-Step or Microstep, while controlling the acceleration and deceleration, using AVR16EB32, MPPB, MPPB Adapter and a power supply.
Back to Top
Back to Related Documentation
Back to Software Used
Back to Hardware Used
Back to Functionality
Back to Configuration
Back to Flowcharts
Back to Setup
Back to Operation
Back to Results
Back to Summary