Objective
The stepper motor is useful in applications that have a high torque requirement, as well as in applications where very precise movements are required. A typical stepper motor will move in 1.8 degree or 0.9 degree movements thus resulting in 200 or 400 steps per revolution respectively. A stepper motor can be driven by two H-bridges, and this project example shows how to set that up using the MPLAB® Code Configurator (MCC) within MPLAB Xpress.
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| |
This Project Uses
- Stepper Motor
- MTP50N06V N-Channel MOSFET (x2)
- ZVP 211 P-Channel MOSFET (x2)
- IN4001 Diode (x4)
- 10 kΩ Resistor (x8)
Useful Resources
- AN907 "Stepping Motor Fundamentals"
- Introduction to Complimentary Wave Generator (CWG) Module
- Introduction to Pulse Width Modulation (PWM) Module
- Introduction to Timer2/4/6 Module
- Driving a Direct Current (DC) motor with an H-bridge
The stepper motor used in this tutorial is a bi-polar stepper motor, meaning that it is made of two coils, which can turn the motor in opposite directions. See the very simplified drawing below:
Each coil of wire controls two poles around the rotor of the motor. This is a very high-level overview. The image above will be useful for understanding the operation of this tutorial as you work through it.
Connection Diagram
The first step is to build the two H-bridges necessary to control the two coils in the motor.
The circuit is as follows:
Procedure
1
Task 1
The first bridge is run by pins RB2 through RB5. If you are using the motor listed in the "Materials" section of this article, the brown lead will connect to the left side of the first H-bridge and the orange lead will connect to the other side. Similarly, the red lead will connect to the left side of the second H-bridge and the yellow lead will connect to the right. If you are using a different motor, make sure the leads connect in a similar orientation to the schematic above. Because some motors will run on upwards of 12 V and a much higher current than the microcontroller is able to send from one pin, the H-bridge uses MOSFETs, which act as electric switches. The MOSFETs allow small current/voltages like those on the microcontroller pins to control much larger current/voltages coming from an external power supply.
To follow along with these steps, MPLAB Xpress should be open and the user should be logged in so that the MCC plugin can be used.
2
Task 2
Create a new project in MPLAB Xpress for a PIC16F1855 using the MPLAB Xpress Development Board.
Instructions are below if this is your first project.
3
4
Task 4
This tutorial uses the CWG to run each of the H-bridges. The CWG has several different application modes, but for the purposes of this tutorial, we are using the Forward and Reverse modes only. These two modes drive all four gates of the H-bridge simultaneously, minimizing the amount of written code after the initialization. The CWG can be found under the Device Resources pane of the MCC.
Because we have two H-bridges to control, we need to use two CWG modules.
5
Task 5
Double clicking CWG1 opens the following window:
This window shows the adjustments to the Core Independent Peripheral (CIP) that have already been made. The Input Source is PWM6_OUT. The Output Mode is set to FWD Full Bridge mode because Full Bridge mode will enable the CWG to drive the current through the wire coil in the stepper motor in both directions. The Clock Source, which refers to the clock source for the dead-band delay, is set to FOSC. To prevent both MOSFETs on one side of the H-bridge from opening at the same time, the setup includes a dead-band delay of 2 to 3 microseconds. This is a safe dead-band time to prevent the same side MOSFETs from turning on simultaneously, but may be too high for some applications.
6
Task 6
The CWG takes an input signal to run, and the input signal used will be the PWM6 module. This can also be found under Device Resources by double clicking PWM6:
In Forward mode, the gate of Q1 is opened while the Q4 gate is sent to the PWM input signal. The gates of Q2 and Q3 are closed. In Reverse mode, Q1 and Q4 are closed while Q3 is opened and Q2 is sent to the PWM signal. The PWM signal opens the gate proportional to the compare value of the PWM signal. This allows current to flow from the corresponding top gate, through the motor in the middle, and down through the PWM enabled gate.
What sets this tutorial apart from driving a DC motor with an H-bridge is that the stepper requires two H-bridges. Each H-bridge will drive one of the coils seen in the figure in the introduction. This way, we can drive one pole of the magnet high while driving the other side low, effectively pulling and pushing into the correct rotational position.
7
Task 7
Here is a simple explanation of the drive concept:
The green arrows represent the current controlled directionally by the CWG output signals opening select gates. Depending upon which side of the coil the CWG drives HIGH, the current in the coil will drive the magnets’ poles in a similar fashion. In the first image, the + and - poles of both magnets hold the motor in a single position.
Next, one coil is turned completely off, causing the motor to rotate 45 degrees to align with the magnet that still has current running through the wire. The method of rotating in 45 degree increments, as opposed to 90, is called half-stepping. This will provide a smoother motor turn, but torque is not consistent because the coils alternate on and off.
Next, both windings are turned on, but this time the B winding is charged in the opposite direction, changing the poles of the B magnet. This causes another 45 degree step in the same direction.
Finally, the A winding is now turned off and the shaft of the motor aligns with the B magnet completely. Remember, this is a very simplified diagram, and the stepper will almost always have more teeth which correspond to more poles and smaller step sizes. The number of these teeth will correspond to the resolution of the motor.
8
Task 8
The PWM module is setup as below:
Using the PWM signal, as opposed to simply a HIGH on the MOSFET gate, comes in handy in more advanced applications that involve micro-stepping. While the microstepping method is not covered in this tutorial, a great resource can be found in AN906, "Stepper Motor Control Using the PIC16F684".
Remember to choose Timer 4 (TMR4) to support the PWM6 module. This is also found under Device Resources.
Set it up as follows:
Change the Clock Source to FOSC/4 because this timer will be used with a PWM module. Change the Prescaler to 1:16 so that the period is long enough to register with the stepper motor. See these changes below:
10
Task 10
Once these modules have been completed, it is time to generate the initialization code and return to the MCC. Click the Generate button.
You can tell if the code has generated properly by the inclusion of the libraries for each CIP in MCC.
Remember, from step 3b, that to turn the motor we must change when a coil is energized and in what direction the coil around the magnets turn on. In order to do this, we will leverage the forward and reverse settings of the Full Bridge modes. To control this we will utilize some written code in the main.c file. The code follows this format:
CWG1CON0 = 0x83; CWG2CON0 = 0x82; __delay_ms(50);
CWG1CON0 refers to a register that controls some settings, including Forward mode or Reverse mode, of the CWG1 module. CWG2CON0 refers to a similar register for CWG2. 0x83 sets the CWG to Reverse mode, and 0x82 to forward. Finally, 0x02 or 0x03 turns off the CWG, causing the current to stop flowing through that particular coil.
With that in mind, look at the code and accompanying simplified diagram:
CWG1CON0 = 0x83; //Reverse mode CWG2CON0 = 0x82; //Forward mode __delay_ms(50);
CWG1CON0 = 0x02; //Coil off CWG2CON0 = 0x82; //Forward mode __delay_ms(50);
CWG1CON0 = 0x82; //Forward mode CWG2CON0 = 0x82; //Forward mode __delay_ms(50);
CWG1CON0 = 0x82; //Forward mode CWG2CON0 = 0x02; //Coil off __delay_ms(50);
Putting these together and including the rest of the code to complete one full rotation cycle results in the following code in main.c:
#include "mcc_generated_files/mcc.h" /* Main application */ void main(void) { // initialize the device SYSTEM_Initialize(); while (1) { CWG1CON0 = 0x83; //Reverse CWG2CON0 = 0x82; //Forward __delay_ms(50); CWG1CON0 = 0x02; //Off CWG2CON0 = 0x82; //Forward __delay_ms(50); CWG1CON0 = 0x82; //Forward CWG2CON0 = 0x82; //Forward __delay_ms(50); CWG1CON0 = 0x82; //Forward CWG2CON0 = 0x02; //Off __delay_ms(50); CWG1CON0 = 0x82; //Forward CWG2CON0 = 0x83; //Reverse __delay_ms(50); CWG1CON0 = 0x03; //Off CWG2CON0 = 0x83; //Reverse __delay_ms(50); CWG1CON0 = 0x83; //Reverse CWG2CON0 = 0x83; //Reverse __delay_ms(50); CWG1CON0 = 0x83; //Reverse CWG2CON0 = 0x03; //Off __delay_ms(50); } }
11
Task 11
Generate a HEX file by clicking the Make and Program Device button.
Program the MPLAB Xpress board by dragging the generated project HEX file from the downloads section of the browser and dropping the file on to the XPRESS drive.
The Programmer LED on the Xpress board should quickly flash from green to red and then back to green, indicating that the HEX file was successfully programmed to the PIC16F18855.
When the MPLAB Xpress board is programmed, the stepper motor will rotate in the counter-clockwise direction. However, in most cases, it is useful to drive the motor in both directions. The rotation code must be slightly altered in order to accomplish this. This is seen in the modified code below:
while (1) { CWG1CON0 = 0x82; //Forward CWG2CON0 = 0x02; //Off __delay_ms(50); CWG1CON0 = 0x82; //Forward CWG2CON0 = 0x82; //Forward __delay_ms(50); CWG1CON0 = 0x02; //Off CWG2CON0 = 0x82; //Forward __delay_ms(50); CWG1CON0 = 0x83; //Reverse CWG2CON0 = 0x82; //Forward __delay_ms(50); CWG1CON0 = 0x83; //Reverse CWG2CON0 = 0x03; //Off __delay_ms(50); CWG1CON0 = 0x83; //Reverse CWG2CON0 = 0x83; //Reverse __delay_ms(50); CWG1CON0 = 0x03; //Off CWG2CON0 = 0x83; //Reverse __delay_ms(50); CWG1CON0 = 0x82; //Forward CWG2CON0 = 0x83; //Reverse __delay_ms(50); }
Results
The project, when programmed into the Xpress board, controls the motor speed and direction. This can be altered in many ways, but demonstrates the basic principle of controlling a stepper motor with a PIC® microcontroller using the CWG peripheral.
Analysis
The CWG module along with the PWM creates the signals required to drive the H-bridge and thus the stepper motor. The main.c file can be changed to reverse the motor direction. Adding more delays between steps is a simple way of controlling the speed of the motor.
Conclusions
There are many options for controlling a stepper motor, including integrated circuits dedicated to this purpose, that make driving a motor in smaller increments known as micro-stepping much easier to do. But to really understand how driving a stepper motor works, it helps to understand how to drive a stepper directly, which is the purpose of this project.