DC Motor Bi-Directional Drive

 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 complimentary 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
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® Xpress
Cloud Integrated Development Environment

The project uses:

Useful Pre-Knowledge:

 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:

Figure8_1(1).jpg

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 complimentary waveforms to drive the H-Bridge Forward and Reverse modes. The CWG module is designed specifically to create the type of signal generation useful for controlling DC motors with an H-Bridge. The [[| CWG generates all the signals] to control the FET's. 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 the Device Resources selection.

Figure9.JPG

The CWG will produce four signals that will be output on CWGA (Xpress pin 1 in schematic), CWGB (Xpress pin 2 in schematic), CWGC (Xpress pin 3 in schematic), CWGD (Xpress pin 4 in schematic),

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.

Figure10.JPG

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:

Figure11.JPG

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.

Figure13.JPG

6

Step 6

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.

Figure20.JPG

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 registers, 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);
 
}
}

7

Step 7

Once you have completed all these steps, choose Make and Program Device

Figure%2012.JPG

The MPLAB Xpress will automatically download a .hex file the compiled code. After the code appears in the downloads area of your computer, drag the program to the Xpress board memory as shown below:

Figure%2013.png

 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.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices 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 such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.