DC Motor with Lock Anti-Phase Drive


It's possible to drive a Direct Current (DC) motor, via an H-bridge, in a bi-directional way using a single Pulse Width Modulation (PWM) signal. That single PWM signal can control both the speed and direction of the motor through a method of motor control called Lock Anti-Phase Drive. Normally, some additional circuitry may be necessary, but the Complementary Wave Generator (CWG) peripheral included in many of the latest 8-Bit Microchip PIC® Microcontrollers eliminates the need for this circuitry by creating a complementary waveform to the input PWM signal. This project will show a brief step by step view of how to set this up using the MPLAB® Code Configurator (MCC) plugin for MPLAB Xpress.


Hardware Tools (Optional)

Tool About Purchase
MPLAB® Xpress
Development Board

Software Tools

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

The Project Uses:

Useful Pre-Knowledge:

 Connection Diagram




Step 1

Lock Anti-Phase Drive uses one complementary PWM signal (two pins) to drive an H-bridge. The duty cycle (PWM signal high time) of the PWM controls the speed and direction of the motor. When the duty cycle is at 50% (50% high time), the motor is stationary. As the PWM duty cycle starts to move away from 50%, the motor starts to spin. The closer the duty cycle is to 0 or 100%, the faster the motor spins. A duty cycle from 0 to 49 will spin the motor in one direction, and a duty cycle from 51% to 100% will spin it in the opposite direction.

Using two pins to drive a complementary PWM signal ensures that both pins will never be high at the same time.


If you would like more information on the theory behind this method, visit the Modular Circuits "Lock Anti-Phase Drive" blog article.


Step 2

In the MPLAB Xpress IDE, click the MCC button to open MCC. Under Device Resources select the CWG Module to configure the CWG.

  1. In the CWG module, use the drop down window to change the Output Mode to Half bridge mode.
  2. In the CWG Events pane, use the drop down window to change the Rising Counts and Falling Counts to 30 to 31.

The rising and falling counts determine how much dead-band time is applied to the complementary PWM signal.

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 a high current path 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.


Step 3

In the Device Resources module select the PWM6 module.


The default settings for the module will work for this application.


Step 4

The PWM module needs a timer, and therefore Timer2 will be used for this application. Chose Device Resources > Timer > TMR2:


This will open the TMR2 configuration window. Change the Timer Period to 100 ms, and change the Clock Source to FOSC/4. To change the period, the Prescaler is set to 1:128. Check your settings with the completed window below:



Step 5

The circuitry requires two pins from the microcontroller instead of four that a standard H-bridge drive may use. However, because we are using P-channel MOSFETs on the top of the bridge and N-channel on the bottom, the CWGxA and CWGxB signals will need to be routed accordingly. Remember, P-channel MOSFETs turn on with their gate at zero while N-channel MOSFETs turn on with a one. To run the motor in one direction, one of the P-channel MOSFETs needs to be driven LOW while the corresponding N-channel receives a HIGH. In order to accomplish this, connect the CWGxA signal to both MOSFETs on the left side of the bridge and connect the CWGxB signal to both MOSFETs on the right side of the bridge. Because they are opposite type MOSFETs, the signal will always be opening one at the same rate as it's closing the other, which is what this set-up requires.



Step 6

The circuit will be controlled by a potentiometer input similar to the project, LED Control Using a Potentiometer. The potentiometer is built into the Xpress board that is used to control the H-Bridge. The input for the potentiometer is an Analog-to-Digital Convertor with Computation (ADCC) pin. The MCC can also setup the ADCC. The ADCC setup window is shown below:


For the purposes of this application, there are a few things to change in the ADCC block. Update the Clock Source to FRC and update the Result Alignment to right justification so that the ADC is reading the correct end of the updated ADC data array. Once the changes are complete click on the Generate button to have the MCC create the driver code.


To change the motor speed and direction based on the potentiometer position, we will need to use the output of the ADCC to control the PWM duty cycle. This is done in the main.c file of the project. The code looks as follows:

void main(void)
    // initialize the device
    while (1)
        adc_result_t PotVal = ADCC_GetSingleConversion(Pot);    \\ Get a value from the ADCC
        PWM6_LoadDutyValue(PotVal);                \\ Load the value to the PWM duty cycle


Step 8

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


After the code appears in the downloads, drag the program to the Xpress as shown below:



The motor will now rotate in opposite directions at the fastest speed when the potentiometer is moved to the maximum left or right position. Any position in between will spin the motor at a slower speed. The closer the potentiometer gets to the middle of the wiper, the slower the motor will move. At the mid-point of the potentiometer position, the motor will reach an idle state and stop spinning.


This method just described for driving a motor with Lock Anti-Phase Drive of a motor in an H-bridge is just one option for controlling a motor. DC motor bi-directional drive is another option for controlling motors. This is a simpler method to consider that requires four separate inputs to the H-bridge but still uses the CWG to reduce hardware requirements. A simple single direction drive can also be achieved through a low side switch.

© 2020 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
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.