Pulse Width Modulation

Pulse Width Modulation (PWM) can be useful in many ways, one being able to control the brightness of a Light Emitting Diode (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.

proj1.png

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.

proj1pic.png

The project uses:

  • PIC16F1508
  • PICDEM Lab II Development Board
  • Power from a 9 V Power Adapter
  • PICkit™ 3 Programmer/Debugger
  • MPLAB X IDE
  • MCC plugin
  • MPLAB XC8 Compiler
  • 5 mm LED
  • 330 Ω resistor 1/8 watt

PICDEM™ Lab II Connections:

  • Connect 5 V and GND to the power bus of the protoboard.
  • Connect 5 V to the Vdd pin of the PIC16F1508
  • Connect Gnd to the Vss pin of the PIC16F1508
  • Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
  • Connect a wire from the ground bus to breadboard Row 27
  • Connect the LED Anode to breadboard Row 23
  • Connect the LED Cathode to breadboard Row 24
  • Connect a 330 Ω resistor between Row 24 and Row 27

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 is described in the "Setup and Installation" section of this training module. You should see a screen similar to the one below before proceeding to step 1.

mplabx.png

1

Create a new stand-alone project in MPLAB X for a PIC16F1508.

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow step-by-step instructions on how to do this.

2

Open the MCC under the Tools > Embedded menu of MPLAB X IDE.

mcclaunch.png
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 mccinfo.png.
mcclaunch.png

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

These modules will automatically be included when you launch the MCC. PWM3 and TMR2 need to be added from Device Resources. Just click on the PWM and then double-click on PWM3 to add it to the project. Under Timers, double-click on TMR2 to add that peripheral.

proj5per.png

4

The Timer2 needs to be set up. Click on Timer2 under Project Resources to make the Timer2 setup appear.

timer2.png

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

  1. Check the Enable Timer box.
  2. Set the Prescaler box to 1:16.
  3. Set the Postscaler box to 1:1.
  4. Set the Timer Period to 16.384 ms.

The window will appear as below after the setup.

timer2setup2.png

5

The next step is to setup the PWM3 module. Click on PWM3 under Project Resources.

eccp2.png

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

  1. Check the Enable PWM box
  2. Check the Enable pin Output box
  3. Select Timer2 from the Select a Timer drop-down menu.
  4. Set the Duty Cycle to 50%
pwm2.png

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 is green, then click on the green lock to make it blue. This will make the reset internal.

proj5io.png

7

Close the Pin Manager and then click on the Pin Module under Project Resources.

gpio.png

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

proj5ioset.png

8

The System needs to be setup next. Click on the System Name under Project Resources.

system.png

In this section, the oscillator settings and the configuration settings are selected.

Oscillator

  1. Select the INTOSC from the System Clock Select drop-down menu.
  2. Select the 1MHz_HF from the Internal Clock drop-down menu.

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

oscillator.png

Configuration

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

config1.png
config2.png

9

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

generate.png

10

The project will now have both generated header and source files. It should also have a generated main.c file.

Note: MCC may offer to generate a main.c file. Click on Yes to allow it to complete this task.

proj5files.png

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

mainfile.png

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

while (1)
    {
        // Add your application code
        for (int duty = 1; duty < 512; duty = duty + 10)
        {
            PWM3_LoadDutyValue(duty);
            for (int delay = 1; delay < 500; delay++)
            {;}
        }
    }
maincode5.png

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" (hammer icon) to compile the code and you should see a "BUILD SUCCESSFUL" message in the Output window of MPLAB X.

Main_Build_Project.png
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.

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

Click on "Make and Program Device". 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.

Main_Program_Target_Project.png

Output Window:

Connecting to MPLAB PICkit 3...

Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.41.06
Firmware type..............Enhanced Midrange

Target voltage detected
Target device PIC16F1709 found.
Device ID Revision = 2002

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory

Device Erased...

Programming...
Programming/Verify complete



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:
ra2lit.jpg

If it is 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 may 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 asked to be saved before the project is closed.
The project can be closed under the File > Close Project.

closeproject.png

Download

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

File Download
Installation
Instructions
Windows Linux Mac OSX
Project 5 Files
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
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.