Objective
Driving a DC (Direct Current) motor in one direction can be done with a single transistor in a low side drive. When you want to drive in both directions then a drive circuit called an H-bridge is a typical way to achieve that control. The Microchip core independent peripheral called the Complimentary Waveform Generator (CWG) can simplify the code requirements with complementary dual drive outputs designed to drive the H-bridge transistors. This simple project will show how to setup the CWG and control it with a Pulse Width Modulation (PWM) signal.
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| |
The project uses:
- DC Motor
- MTP50N06V N-Channel MOSFET (x2)
- NTD2955 P-Channel MOSFET (x2)
- IN4001 Diode (x4)
- 10K Ohm Resistor (x8)
Useful Pre-Knowledge:
- Understanding of P-channel and N-channel MOSFETs
- Introduction to CWG Module
- Introduction to Comparator Module
- DC Motor Basics
- DC Single Direction Control
Procedure
1
Step 1
An H-bridge circuit can be used to control the direction and amount of current flow through a DC motor to control the motor speed and spin direction. H-bridges use MOSFETs (a type of electrical switch) and a Pulse Width Modulation (PWM) signal to do this. A simple circuit is shown below:
The schematic above shows the components used when developing this lab. The buffers driving the transistors are not needed as this project actually used logic level MOSFETs. The diodes are used to prevent inductive kickback.
How it Works
You will notice the red line, signifying the current flow through the motor in the direction defined as 'forward', and the green line labeled as 'reverse'. The current flows in a diagonal fashion so that it flows through the motor. For example, if Q1 is opened, then Q4 will also be opened, while Q3 and Q2 are both closed to prevent short-circuiting Vsupply to GND. The bottom MOSFETs (Q2, Q4) are sent a PWM signal which controls how open or closed the gate is, which then controls how fast the motor is moving.
2
Step 2
This project uses the CWG to produce the complementary waveforms to drive the H-Bridge. The CWG module is designed specifically to create the type of signals useful for controlling DC motors with an H-Bridge. The [[| CWG generates all the signals] to control the FETs. This can save valuable time in code development and reduces costs associated with the external logic typically used in these applications. It also has separate modes for forward and reverse movement.
To include the CWG in a project, the MPLAB® Code Configurator (MCC) can be used to generate the driver code. The CWG can be found under Device Resources.
The CWG will produce four signals that will be output on CWGA (Xpress pin 1), CWGB (Xpress pin 2), CWGC (Xpress pin 3), CWGD (Xpress pin 4 in ). Please see the schematic in Step 1 to reference these.
3
Step 3
Setting up the CWG
Choose PWM6_OUT for the input source in order to send a PWM signal to Q2 and Q4. For the output mode, we can choose either Forward or Reverse Full Bridge Mode. For the clock source, remember this is not referring to the PWM clock source. This is the clock source for the dead-band delay (as mentioned in the Introduction to CWG page). Choose FOSC.
4
Step 4
If the two MOSFETs on one side of the H-bridge open at the same time, a phenomenon known as 'shoot through' can occur. This results in high current through both MOSFETs on one side of the H-bridge. This can quickly destroy MOSFETs and other associated circuit components. One way to help prevent this from happening is to implement a dead-band delay.
The CWG module has a built-in option for a dead-band delay. The dead-band creates a delay between the activating edges on the signals of the top and bottom MOSFETs to prevent them from being ON at the same time. The exact amount of time between the activating edges is controlled by a combination of the CWG clock source and the rising/falling counts.
Dead Band Delay is a useful tool found under Events. Clicking the arrow will open this window:
The exact time necessary to use for the dead-band delay should be longer than it takes the MOSFETs to reach fully ON and OFF states. One helpful resource to calculate these numbers can be found here. A one to two microseconds delay should be plenty for the MOSFETs and motor we are using in this example.
5
Step 5
Finally, the Output Pin Config window changes settings for each individual output from the CWG. The P-channel MOSFET gates at the top of the H-bridge are off when receiving LOW and on when receiving a HIGH signal. Therefore the polarity of the CWGA and CWGC pins need to be inverted. This is easily done in the MCC setup window for the CWG module.
8
Step 8
Finally, the PWM signal needs a clock source, so choose the Timer4 from the Device Resources:
While the window below is TMR2, the setting changes will be the same. Change the 'Clock Source' to FOSC/4 because we are using it with a PWM module. Change the 'Prescaler' to 1:128 so that the period can be extended. Last, change the 'Timer Period' to 100 ms.
10
Step 10
Once you have completed these steps, click on the Generate button on the top of the MCC and return to the IDE window. This will generate the source files for the CWG and PWM according to the MCC settings.
The only software left to create is the main.c code that will control the direction of the motor (forward and reverse) using the CWG.
It will be necessary to write to the CWG1CON0 register to control the direction. This is one of the initialization regisers, and has control over the mode of the CWG module. The right-most three bits of this register control which mode the CWG is currently in. For example, a register defined as 1000 0011 (0x83 in hexadecimal), will operate the CWG in 'Reverse Full-Bridge Mode'. On the other hand, when CWG1CON0 is set to 1000 0010 (0x82), the CWG will operate in 'Forward Full-Bridge Mode'.
The important takeaway from this is that CWG1CON0 = 0x83 will drive motor rotation one direction and CWG1CON0 = 0x82 will drive motor rotation in the opposite direction.
The following example code will drive the motor in one direction for 5 seconds, and then change the direction for 5 seconds:
#include "mcc_generated_files/mcc.h" void main(void) { // initialize the device SYSTEM_Initialize(); while (1) { CWG1CON0 = 0x83; //drive the motor in reverse mode __delay_ms(5000); CWG1CON0 = 0x82; //drive the motor in forward mode __delay_ms(5000); } }
Results
The motor will repeatedly spin in one direction for 5 seconds, then spin in the opposite direction for 5 seconds.
Conclusions
The method just described is only one way to drive a motor using an H-bridge. Another option is to control a motor through a style called Lock Anti-Phase Drive. In this method, a single PWM signal can be used to drive both the speed and direction of the motor. Once again the CWG makes producing this type of drive much easier to implement.