This repository contains an example of MCC® Melody generated code for Pulse Width Modulation (PWM) signals using Capture/Compare/PWM (CCP) module as described in Getting Started with PWM Using CCP on PIC18.
The PIC18F47Q10 features four 10-bit PWM generators. This example describes how to configure the CCP1 peripheral in conjunction with TMR2 to produce a low speed PWM signal.
Two GPIO pins are used:
- RE2 as an input to read the on-board button for controlling the demo
- RB4 as a PWM output
- Getting Started with PWM Using CCP on PIC18
- PIC18-Q10 Product Family Page
- PIC18F47Q10 Data Sheet
- PIC18F47Q10 Code Examples on GitHub
- MPLAB® X IDE v6.05 or newer
- MPLAB® XC8 2.40 or newer
- MPLAB® Code Configurator Melody
- MPLAB® Data Visualizer
- PIC18F-Q_DFP Device Family Pack v1.15.360 or newer
- Logic analyzer software
- PIC18F47Q10 Curiosity Nano Development Board is used as a test platform. (DM182029)
- Oscilloscope or Logic Analyzer
To program the Curiosity Nano board with this MPLAB® X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.
The following peripheral and clock configurations are set up using MCC Melody for the PIC18F47Q10:
-
Clock Control:
-
Configuration Bits:
-
CCP1:
-
TMR2:
-
Pin Grid View:
-
Pins:
- RB4:
- Function: CCP1
- Direction: In/Out
- RE2:
- Function: GPIO
- Direction: Input
- Custom Name: Button
- Weak Pullup: Enabled
- RB4:
The following pin configuration must be made for this project:
Pin | Configuration | Function |
---|---|---|
RB4 | Digital Output | PWM CCP1 |
RE2 | Digital Input | GPIO - Button |
Note: RE0 (LED0) pin is used as input for LED visualization. (optional)
There are two options to run this example:
- hook the oscilloscope or the logic analyzer probe on RB4 pin
- run a wire between RB4 and RE0
Then run the code generated by MCC, RB4 will generate a PWM signal with a frequency of 1 Hz. To demonstrate the run-time update of the PWM parameters, SW0 (RE2) push-button is used.
- A short button press (less than 1.5 seconds) results in a change of duty-cycle in steps of 25% (from 25% up to 75%)
- A long button press (1.5 to 2.0 seconds) results in a change of frequency from 1 Hz to 4 Hz in steps of 1 Hz
Snapshots of different cofigurations:
- Frequency: 1 Hz, Duty-Cycle: 25%
- Frequency: 1 Hz, Duty-Cycle: 50%
- Frequency: 1 Hz, Duty-Cycle: 75%
- Frequency: 2 Hz, Duty-Cycle: 25%
- Frequency: 3 Hz, Duty-Cycle: 25%
- Frequency: 4 Hz, Duty-Cycle: 25%
This project showcases how to configure basic PWM functionalities, such like duty cycle and frequency on the PIC18F47Q10 microcontroller. The example shows both MCC initialization of the CCP1 and TMR2 for PWM and update of parameters at run-time.
This chapter shows how to use the MPLAB® X IDE to program an PIC® device with an Example_Project.X. This can be applied for any other projects.
-
Connect the board to the PC.
-
Open the Example_Project.X project in MPLAB X IDE.
-
Set the Example_Project.X project as main project.
-
Clean and build the Example_Project.X project.
-
Select the PICxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
-
Program the project to the board.