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 time between pulses 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.
The project uses:
- PIC16F18875
- HPC Curiosity Board
- MPLAB X IDE
- MPLAB Code Configurator (MCC) plug-in
- MPLAB XC8 Compiler
To follow along with these steps, MPLAB X should be open and the HPC Curiosity Board connected to the computer through a USB cable.
1
Create a new 'standalone' project in MPLAB X for a PIC16F18875.
If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.
3
Select the peripherals for your project. In this first project, the following peripherals need to be selected:
- System Module
- Interrupt Module
- Pin Module
- TMR2
- PWM6
These modules will automatically be included when you launch the MCC, with the exception of TMR2 and PWM6. TMR2 and PWM6 need to be added from Device Resources. Just click on Timer and then double-click on Timer 2 to add it to the project. Click on the PWM and then double click PWM6. The result should look like the picture below:
4
Timer2 needs to be set up. Click on Timer2 under Project Resources to make the Timer2 setup appear.
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.
- Check the Enable Timer box.
- Set the Prescaler box to '1:1'.
- Set the Postscaler box to '1:16'.
- Set the Timer Period to '16.384 ms'.
After set up, the window will appear as below:
5
The next step is to setup the PWM6 module. Click on PWM6 under Project Resources.
The PWM6 setup will appear in the center of the MPLAB X screen. This requires a few setup selections.
- Check the Enable PWM box
- Select Timer2 from the Select a Timer drop-down menu.
- Set the Duty Cycle to '50%'.
The result should look like this:
8
The System needs to be setup next. Click on System Module under Project Resources.
In this section, the oscillator settings and the configuration settings are selected.
Oscillator
- Select the HFINTOSC from the Oscillator Select drop-down menu.
- Select the 4_MHz selection from the HF Internal Clock drop-down menu.
- Select Clock Divider value of '4'.
The window should look like the one below after the proper setup:
Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.
9
Click on the Generate code button to have MCC create the software libraries for this project.
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 D2 (RA4) 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
PWM6_Initialize();
for (int duty = 1; duty < 512; duty = duty + 10)
{
PWM6_LoadDutyValue(duty);
for (int delay = 1; delay < 500; delay++)
{;}
}
}
The PWM6_LoadDutyValue(duty) function was generated by the MCC and its definition is located in the pwm6.h file.
The PWM6_Initialize() function is also generated to initialize the PWM peripheral. It's also located in the pwm6.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.
13
Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.
Click on Make and Program Device Main Project. 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/Verify complete" message and the D2 LED connected to RA4 will slowly get brighter on the HPC Curiosity Board.
Connecting to Starter Kit on Board...
Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.54.00
Firmware type..............Enhanced Midrange
Target voltage detected
Target device PIC16F18875 found.
Device Revision ID = 2002
Device Erased...
Programming...
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
**Programming/Verify complete**
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 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
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
|
| | | |