Objective
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.
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)
- 10 kΩ 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
- DC Bi-directional Control
Connection Diagram
Procedure
1
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.
2
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.
- In the CWG module, use the drop down window to change the Output Mode to Half bridge mode.
- 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.
4
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:
5
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.
6
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.
7
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 SYSTEM_Initialize(); 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 } }
Results
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.
Conclusions
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.