Introduction to PWM module in MPLAB® Xpress

The Pulse Width Modulation (PWM) module generates a square wave that has a constant period but varying duty cycle (compare time as shown here). The general form of this signal can be seen below:


The PWM is controlled by an internal timer and a compare register is used to test the timer value and set the duty cycle or high time of the signal when a match occurs.

PWM signals are frequently used in motor control to control the speed and direction of DC, servo, and stepper motors. The purpose of this resource is not to teach PWM theory, but to give an explanation of the using the PWM module within the MPLAB® Xpress IDE.

Locating the PWM Module

The PWM module is located under Device Resources in the MPLAB Code Configurator (MCC) in the MPLAB Xpress IDE.


One thing to keep in mind when using the PWM module is the necessity to always combine it with Timer2/4/6. Timers are a necessary part of signal generation to keep the period and compare values consistent.

Once the PWMx is chosen, the following window should open:


Basic Module Functionality

Enable PWM: Checking this box states to the program that the PWM module will begin as part of the original initialization within your code, as opposed to manually calling it to begin in the user written software section. Unchecking this box may be useful for power saving reasons at the microcontroller start-up.

Duty Cycle: This box is one way to automatically set what the original duty cycle percentage will be when the PWM module first initializes. For example, a 50% duty cycle means that the compare value will be half of the period. If Vcc was equal to 5V, then the observed voltage would be 2.5V. The actual period and compare time values will be determined by the Timer2/4/6 module and system clock.

PWMDC Value: The PWMDC value is useful to know when writing the software to accompany your MCC initial settings. This number corresponds to the Duty Cycle % number.

PWM Polarity: This value sets the active polarity, or the value corresponding to ON, of the PWM signal. Active_hi means that at 100% duty cycle, the digital value will be 1 for 100% of the period. Active_lo means that at 100% duty cycle, the digital value will be 0 for 100% of the period.

PWM Period: The finalized period time length based upon the chosen clock settings in the System Module and Timer2/4/6.

PWM Frequency: This frequency is derived based upon the system clock frequency. See the datasheet for more information.

PWM Resolution: This number gives the number of different duty cycle values that a particular signal can use. For example, a 10-bit signal can have 1024 different duty cycle values, which would correspond to 1024 different pulse lengths.

Configuring the Pins in MCC

There is usually only one pin that is necessary to set when using the PWM module. This is the output pin which will be used to send the signal out of the microcontroller to another peripheral, such as a motor.

The port options can be seen below:


In this example, pin RA4 was used to output the signal to an oscilloscope for testing.

Using Basic Software Functionality in main.c file

Besides the initialize function, which only needs to be called if the Enable PWM is not initially checked, there is only one other custom function generated by the MCC.

Software Function What it does
void PWM6_LoadDutyValue(uint16_t dutyValue); This function is looking for one of the numbers referenced in the section above labeled PWMDC Value. You will also notice that the range of numbers in PWMDC Value correlates to the PWM Resolution. For example, at 1% duty cycle, basically always OFF, the value will be around 0. At 100%, the PWMDC Value will correspond to the final bit resolution, say 1023 for 10-bit. This function is extremely useful when sending servo values where specific pulses relate to specific positions of the servo.

Example Code

void main(void)
   //no PWM6_Initialize() called because Enable PWM was checked
    while (1)
        //will load a 1% duty cycle value for the corresponding clock settings

Example Project

LED Control using the Xpress board
Learn more >

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.