10-Bit PWM MPLAB Code Configurator Example

The MPLAB Code Configurator (MCC) makes setting up a 10-bit PWM peripheral easy. The steps include setting up the I/O, Timer 2 and PWM module to make it run. The MCC will automatically generate the code to load the proper registers and initialize the proper values to produce the desired PWM signal.

The best way to show how this is done is through a simple example. A PIC16F1825 Capture/Comparae/PWM peripheral will be configured to create a PWM signal at: 500Hz, 50% duty cycle using a 4Mhz system clock and 1:16 prescaler.


The first step after launching the MCC within MPLAB X® is to select the peripherals we will use and setup the PWM.
The three resources required are the System, TMR2::Timer and the CCP3:PWM modules with the MCC list of options.


System Setup

The System is where the oscillator speed is selected and any changes to the configuration settings you may need. The 4 MHz internal oscillator is selected as shown in the picture below.


Timer 2 Setup

Timer 2 uses the oscillator selected in the System section to adjust the Timer 2 period. The time of 2.0 milliseconds is entered for the period to yield a 500 Hz frequency. The prescaler is selected as 1:16 from the drop-down menu. The Start Timer After Initialization box is also checked. This will start the timer running and also the PWM signal after the PIC16F1825 finishes initializing all the peripherals.


CCP3:PWM Setup

By selecting the CCP3:PWM the MCC automatically selects the I/O pin RA2 in the I/O selection window. The RA2 pin actually shows up with the label CCP3 in green to show that the CCP3 peripheral now controls the I/O pin.


The CCP3:PWM setup screen is where the Duty Cycle is selected and 50 is entered for 50%.
PWM period and frequency are displayed in this window as well based on the Timer 2 selection window.


Generate Code

When all the setup screens are complete the MCC Generate Code button generate.png is clicked and the MCC produces the software files for the project. The MCC will produce a MAIN.C file that contains a System Initialize function as it's only component.

The System_Initialize function is placed in a file named MCC.C.
System_Initialize calls four functions:



The OSCILLATOR_Initialize function takes the Oscillator Settings selected and sets up the proper registers for the 4 MHz internal oscillator.


The PIN_MANAGER_Initialize function sets the registers for the I/O pins.


The TMR2_Initialize function sets the registers for the Timer 2 settings selected including the prescaler and PR2 value.


The PWM3_Initialize function selects the settings for the 50% duty cycle value. Notice the CCP3RL register is loaded with the proper value to create the proper high time of the 50% duty cycle.


The code is then compiled within MPLABX IDE environment and programmed into the PIC16F1825. The device will start operating as soon as it's powered up. Timer 2 will start running immediately after the initialization phase of the code. The results are shown on the oscilloscope screen capture below. The screen capture shows, in the measurement section, a period of 2 milliseconds and frequency of 500 Hz as we expected. Each pulse is an equal 1 milliseconds off the center of the signal for a perfect 50% duty cycle.


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.