DC Motor with Lock Anti-Phase Drive


It's possible to drive a 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 Complimentary 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) plug-in 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, you can find a good resource here.


Step 2

In the MPLAB Xpress IDE, click the MCC icon to open the MPLAB® Code Configurator. Under Device Resources select the CWG Module to configure the Complementary Waveform Generator.

  1. In the CWG module, use the drop-down window to change the Output Mode to "Half bridge mode"
  2. In the CWG Events window, use the drop-down window to change the Rising 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. TMR2 is chosen under Timer in Device Resources:


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 this 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:


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 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.

© 2019 Microchip Technology, Inc.