Complimentary Waveform Generator in Xpress

The Complimentary Waveform Generator (CWG) can create a complementary waveform of a signal from a variety of on-chip and off-chip waveform sources. The CWG also includes useful features such as auto shutdown/restart and dead band control.

  • Auto shutdown/restart allows the developer to force the CWG to turn off when a certain condition is met, regardless of the CWG’s current activities.
  • Dead band control is used to prevent damaging “shoot through” conditions that can occur during H-Bridge switching algorithms, in which Vcc is connected directly to Ground.

The advantage of using these capabilities over other options, such as an external motor driver chip, is that component count is decreased while increasing intelligent control to the system. The CWG includes these and other useful tools to increase efficiency while decreasing board size and cost by replacing physical components with software solutions on the microcontroller.

Within MPLAB® X or MPLAB Xpress is the optional plug-in MPLAB Code Configurator (MCC). The MCC makes setting up the CWG very easy. The MCC will also auto-generate functions that control the CWG. This makes incorporating the CWG capabilities into your application easier as well.

Locating the CWG in MCC

The CWG blocks are located under Device Resources in the MPLAB Code Configurator (MCC).


Choosing the CWG peripheral opens the following window:


CWG Basic Functionality

The Enable CWG box turns the CWG functionality on. Leave this box checked.

Input Source: This drop down menu defines the signal to which the CWG will add enhanced capabilities. This menu includes a variety of on-chip and off-chip sources including integrated PWM modules.

Output Mode: This drop down menu defines how the input signal will be enhanced upon output from the module. While this document will provide a high level, basic explanation for each mode, look to the device datasheet for detailed explanations of all modes.

  • FWD Full bridge mode, very useful in motor direction control, generates four signals: CWGxA = 1, CWGxB,C = 0, CWGxD = modulated by input signal.
  • Half bridge mode generates two signals, one that is the same as the input, and one that is the inversion.
  • Push-Pull Mode generates alternating copies on the input signals on CWGxA and CWGxB, which is useful for driving transformers in power supplies.
  • Reverse Full bridge mode is similar to FWD mode, but is set up to run an H-bridge in the opposite direction. CWGxA = 0, CWGxB =modulated input signal, CWGxC = 1, CWGxD = 0.
  • Steering mode repeats the input signal to up to all four of the CWG lines. Section 20.9.1 in the datasheet explains the difference between synchronous and non-synchronous.

Clock Source: The CWG needs a clock source for time based functions such as dead band control, and has two options: FOSC and HFINTOSC.



The Events box includes two drop down menus: Rising Counts and Falling Counts. These two values control the dead-band delay length of time in half bridge and full bridge mode. The time increments are generated based upon the clock chosen under Clock Source.



The Auto-Shutdown functionality is the microcontroller’s ability to override the CWG outputs to shut down the outputs if certain criteria are met. The options within the box help to set these conditions.

Enable Auto-Restart: If enabled, then after the auto-shutdown has been completed, the microcontroller will restart on the next clock cycle. If not, the restart must be manually triggered in software.

CWGA and CWGC Shutdown State & CWGB and CWGD Shutdown State: These drop-down menus refer to the state a particular CWG signal will take once the module has gone into shutdown mode…

  • Inactive: The outputs selected will act as they do if the input signal ends.
  • Logic 0: The outputs selected will act as they do if the input signal ends.
  • Logic 1: The outputs selected will send logic 1.
  • Tristate: The outputs selected become high impedance inputs appearing like an open circuit.

The final five boxes each signify an input that may be used to trigger the auto-shutdown. Each is active-low, meaning the signal on that line must become 0 in order to trigger the auto-shutdown sequence. These outputs can also often be routed through the Configurable Logic Cell (CLC) logic should that accommodate your project.

Output Pin Config:


The Enable Steering box allows the steering mode to steer the particular CWGX output.

Leave Steering Data to the default choice.

Polarity: This drop down menu allows the choice between inverting the polarity of the chosen CWGx bit.

Configuring the Pins in MCC

The four CWGx outputs, A through D, can be connected to any of the Port pins shown. For ease and debugging purposes, RB1 through RB4 were chosen when in this explanation. An additional helpful step for early debugging was routing the PWM_6 output to RA7 in order to compare the signal coming into the CWG with those being output. One last row to note is the row whose function is labeled CWG1IN. This row is the input of the CWG if the input is defined by a pin rather than an internally sourced signal. This signal input can also be connected to any pin in Port B or Port C.


Using Basic Software Functionality in main.c file

MCC will generate custom functions in the cwgx.h and cwgx.c files depending on which mode was chosen. The functions simplify controlling the CWG.


Some of the functions created are described here:

Software Function What it does
void CWG1_Initialize(void); Triggers all the components set-up in the MCC window to start. However, because this function is already called in the mcc.c source file, this step has already been taken care of for you.
void CWG1_AutoShutdownEventSet(); This function triggers a software called auto-shutdown of the CWG module, forcing the module into shutdown state.
void CWG1_AutoShutdownEventClear(); This function will restart the CWG module after it has shutdown via a trigger set in the code configurator, or after the AutoShutdownEventSet() is called. This function is only necessary when the auto-restart is not already being used.
void CWG1_LoadRiseDeadbandCount(uint8_t dutyValue); This function sets or changes the rising edge dead band delay register value via the user written program.
void CWG1_LoadFallDeadbandCount(uint8_t dutyValue); This function sets or changes the falling edge dead band delay register value via the user written program.
bool CWG1_IsModuleEnabled(void); This function returns a boolean True/False value corresponding to whether or not the CWGx module is enabled.
void CWG1_LoadBufferEnable(void); Once the register values are set, they still need to be loaded into the CWG1CON0<LD> bit. This must be changed before the user written changes are actually implemented in the waveforms being generated by the chip. It is also important to note that the CWGx module must be enabled before the buffer can be loaded!

Example Code

//Set value to change the falling edge dead band count

uint8_t myDutyValue;    

CWG1_Initialize();    //Initialize the CWG1 module

//Load the chosen dead band delay register value 


//Check to make sure the CWG1 module is enabled


 //Load the register value into the buffer and change the 
 //  CWG waveform on the next edge of the input signal 



20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

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