Pulse Width Modulation (PWM) can be useful in many ways, one being able to control the brightness of an LED. Varying the On time at a very high rate can make the LED look dimmer or brighter without the human eye detecting the flashing. The same technique can be used for speed control of motors. PWM can be a useful technique on many designs and a PWM peripheral is built into most 8-bit MCU devices.

Step by Step Instructions

Using the MPLAB® Code Configurator (MCC), this project generates the code to control the brightness of an LED using a hardware PWM peripheral to generate the variable on/off time at a very high rate. This on/off frequency averages out to appear as a variable brightness on the LED..

To follow along with these steps, MPLAB® X should be open and the Programmer/Debugger connected to both the computer and the Development Board. The setup was described in the Setup and Installation section of this training module. You should see a screen similar to the one below to move on to step 1.

1

Create a new "Stand-Alone" project in MPLAB® X for a PIC16F1508. Instructions are below if this is your first project.

Step-by-Step

Launch the new project wizard

Launch the New Project Wizard using one of the following methods:

From the Toolbar: Click on the New Project icon , or

From the Menu: Select File ► New Project, or

From the Keyboard: Ctrl + Shift + N

The project wizard will walk you through the process and prompt you for all the required information. Your selections may be changed after the project has been created by modifying the project's properties.

2

Choose Project

a

Choose Microchip Embedded from the Categories column (center).

b

Choose Standalone Project from the Projects column (right).

c

Click the Next > button.

Click image to enlarge.

3

Select Device

a

Choose the device family from the Family drop-down box.

​

If you have used MPLAB X IDE previously, choosing Recently Used from the Family drop-down box will filter the Device list to the 3 devices you have used most recently.

b

Choose your device's part number from the Device drop down box. You may also type in the part number directly, and the list will adjust to the characters you enter.

Click image to enlarge.

4

Select Header

​

The wizard will automatically skip this step if a header is not available for your selected device.

If you are using a debug header with your debugger, select it from the Supported Debug Header drop-down box. They are listed by the header's part number.
If you are not using a debug header (i.e. the debugger is directly connected to the target device), select None.

Click image to enlarge.

5

Select Tool

If you are using a hardware debugger / programmer, you should see a serial number under its heading if it is connected to your system. Click on the serial number to associate that specific tool with your project. It is important to select the serial number itself because MPLAB X IDE permits multiple projects to be open simultaneously, and each one may use a different debug tool. For example, if you have three ICD3's connected to your PC, you could have three projects open and each one assigned to a different ICD3. This makes it possible to simultaneously debug multiple applications in the same instance of MPLAB X IDE.

For software based tools, such as the simulator, you only need to click on the tool's name to select it.

​

Hover your mouse pointer over the colored dots to see their meaning for a specific tool. Red = No Support, Yellow = Beta Support, Green = Production Tested.

Click image to enlarge.

6

Select Plug-in Board

​

The wizard will automatically skip this step if a plug-in board is not available for your selected device.

If you are using a plug-in board to enhance the features of your debugger (usually the Real ICE), select it from the Selected Plugin Board drop-down box. They are listed by function.
If you are not using a plug-in board (i.e. standard debugger configuration), select None.

Click image to enlarge.

7

Select Compiler

The IDE will display all compilers available on your system that support your selected device. Select the compiler (including version number) you wish to use for building the project.

Click image to enlarge.

8

Select Project Name and Folder

a

Choose a Project Name. This will be your project's name as well as the name of the project directory (with a ".X" appended) that the IDE will create.

b

Choose a Project Location. This is where the IDE will create the project directory.

If a directory with the specified name already exists and has no MPLAB X IDE project files in it, the IDE will use the existing directory for the new project. If the directory does have MPLAB X IDE files in it from another project, the wizard will give an error that must be fixed by choosing a different project name or location. If the directory does not exist, the IDE will create it and use it for the new project.

Click image to enlarge.

2

Open the MPLAB® Code Configurator under the Tools -> Embedded menu of MPLAB® X IDE.

The sections of the MCC tool are shown in the image below. We will refer to these sections by name throughout this article. This highlight can be enabled over the MCC at any time by clicking on the information icon .

3

Select the peripherals for your project.
In this first project these peripherals need to be selected:

System Module

Interrupt Module

Pin Module

PWM3

TMR2

The System Module, Interrupt Module and the Pin Module will all be automatically included when you launch the MCC. PWM3 and TMR2 need to be added from the Device Resources area. Just click on the PWM category and then double click on the PWM3 to add it to the project. Under Timers double click on TMR2 to ad that peripheral. The result will look like the picture below:

4

The Timer2 needs to be setup. Click on the Timer2 label in the Project Resources menu to make the Timer2 setup screen appear.

Timer2 will be driven by the instruction clock which will be setup in a future step. These selections need to be made for the TMR2 operation.

Check the Enable Timer box.

Set the Prescaler box to 1:16.

Set the Postscaler box to 1:1.

Set the Timer Period to 16.384 ms.

The window will appear as below after the setup.

5

The next step is to setup the PWM3 module. Click on the PWM3 in the project resources shown below.

The PWM3 setup screen will appear in the center of the MPLAB® X screen. This project requires a few setup selections.

Check the Enable PWM box

Check the Enable pin Output box

Select Timer2 from the Select a Timer drop down menu.

Set the Duty Cycle to 50%

The result will look like this.

6

Open the Pin Manager and then click on the PORTA pin 2 (RA2) blue lock symbol in the PWM3 row. It should turn green. This will output the PWM signal to the RA2 pin. Also make sure the Reset pin has the blue lock shown. If it's green then click on the green lock to make it blue. This will make the Reset internal. It should look like the picture here when completed:

7

Close the Pin Manager and then click on the Pin Module selection in the Project Resources section.

The center screen will show RA2 listed on the I/O chart. Click on the output box to make the pin an output (if not checked) and make sure Analog and WPU are not checked (click on them to uncheck them).

8

The System needs to be setup next. Click on the System name in the Project Resources list.

The System section will appear. In this section the oscillator settings and the configuration settings are selected.

Oscillator

Select the INTOSC from the drop down menu for the System Clock Select.

Select the 1MHz_HF selection from the Internal Clock drop down menu.

This will enable the internal 1 MHz internal oscillator as the system clock.

Configuration

Each configuration setting can be changed under the Register tab of the System window. Match the selections shown here.

9

Click on the Generate Code button to have the MCC create the software libraries for this project.

10

The project will now have both generated Header Files and Source Files. It should also have a generated main.c file.Note: MCC may offer to generate a main.c file. Click YES to allow it to complete that task.

Double click on the main.c file to open it up in the editor window.

11

The PWM Duty cycle is adjusted by a value of 0 to 1023 in the PWM control routine. This is a 10-bit resolution. This example will only use half of the range of 1 to 512 to change the brightness of the LED connected to the PWM output. The main loop of code adjusts the PWM value in increments of 10 and uses a For-Loop delay routine to allow the PWM value to be displayed on the RB2 LED. To perform this routine, the following code needs to be entered in the main.c file under the while (1) area where the MCC generated template says "// Add your application code".

Note: The PWM3_LoadDutyValue(duty) function was generated by the MCC and its definition is located in the pwm3.h file.

This is a simple example of how the MCC generates a library of useful functions to make creating a PWM project code much easier and quicker.

12

Click on the Build Project Icon (the Hammer) to compile the code and you should see a BUILD SUCCESSFUL message in the output window of MPLAB® X.

BUILD SUCCESSFUL (total time: 7s)

13

Make sure your project has the programming tool selected (Part of Step 1 above) and connect power to your development board.

​

The PICkit™ 3 has limited power capability so we recommend you power the board separately.
The ICD™ 3 can power a development board, but for simplicity we recommend you power the board separately.
The RealICE™ cannot power the development board so powering the board separately is required.

Click on the Make and Program Device icon. This will build the project again and launch the programmer. In the output window you should see a series of messages and if successful it will end with a Programming and Verify Successful message.

The RA2 LED will light up and get brighter as time passes. It will then reset to a dim light level and repeat the change to a brighter light. This will run continuously. The picture below shows the LED is fully lit:

​

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You make see a series of processes if this occurs. This should only happen once.
Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange

14

The project can be closed in MPLAB X. The project is saved automatically when it is built but any changes to files or configuration may be ask to be saved before the project is closed.
The project can be closed under the File Menu > Close Project

Download

If you have any problems with your project, the completed MPLAB® X project file can be downloaded from the link below:

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.

Click here to edit contents of this page.

Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.

Append content without editing the whole page source.

Check out how this page has evolved in the past.

If you want to discuss contents of this page - this is the easiest way to do it.

View and manage file attachments for this page.

A few useful tools to manage this Site.

See pages that link to and include this page.

Change the name (also URL address, possibly the category) of the page.