This tutorial will lead you through setting up an MPLabX project for the PWM subsystem on the PIC18F57Q43 Curiosity Nano and programming it.
Item | Detail |
---|---|
PIC18F57Q43 Curiosity Nano | Website / User Guide |
Breadboard | |
USB-C Programming cable |
Before working through the tutorial, it is important to answer some key questions about the board. Open up the curiosity nano hardware user guide to find more detailed answers to the following questions:
Question | Answer | Context |
---|---|---|
What is the difference between period and duty cycle? | wikipedia article | |
How do you reload the PWM buffer registers | Read here and here | |
How is the onboard LED connected to pin RF3? | Refer to the PIC18F57Q43 Hardware User Guide, figure 7-1 | This will help you answer whether you have to set the pin low or high to turn on the LED. |
If you haven’t completed the hello world tutorial for the PIC18F57Q43 devkit, this would be a good place to start.
In MPLabX, select File $\rightarrow$ New Project to create a new project.
Create a new project |
Select Microchip Embedded as the category and “Application Project(s)” from the Projects list
Select “Application Project(s)” |
Select the PIC18F57Q43 as the “device” and the PIC18F57Q43 Curiosity Nano as the programmer
select the appropriate device and programmer |
Select the XC8 compiler
Select the XC8 compiler |
Name your project something like “pwm-subsystem” and select your project’s parent directory.
Your project will open. Select “next” to create a MCC profile using “Melody”, the current MCC engine.
Select next |
Based on the device settings entered during project creation, MPLabX may need to download additional files. Wait for this process to finish. (Ensure you have an internet connection or the process may not work)
Download Support Files |
MCC Should open.
MCC Configuration View |
In the MCC Window select the “device resources” tab and then select “device drivers” from the dropdown list
Device Resources |
Select PWM $\rightarrow$ PWM1 and click the “+” button to add the PWM1 subsystem to te MCC Configuration.
Expand Drivers |
Select “+” next to PWM1 |
A window to the right should open with more configuration settings.
PWM Config, Top |
PWM Config, Bottom |
In the pin manager window, click on port F3 in the PWM1_output1 row to enable RF3 as a PWM1_output1 .
Pin Config |
Click the “generate” button to generate your MCC configuration
generate MCC |
Select the “clean and compile” button to verify your project compiles.
Clean and complile |
Select the “download” button, ensuring your Curiosity Nano device is connected.
download code |
Observe what happens.
Critical Concept: Why did it take no code to flash the LED with the PWM?
In the project explorer window, select your pwm project, then select “header files” $\rightarrow$ “mcc generated files” $\rightarrow$ “pwm” $\rightarrow$ “pwm1_16bit.h”
Note: a header file (ending in
.h
) “declares” functions and variables, while source files (ending in.c
) “defines” functions. Function declaration lets the compiler know about the function’s name and data types, while function definition provides the code.
PWM1 header file |
In the project explorer window, select your pwm project, then select “source files” $\rightarrow$ “mcc generated files” $\rightarrow$ “pwm” $\rightarrow$ “src”-> “pwm1_16bit.c”
PWM1 source file |
Look for and note the initialized values for the following four values: PWM1PRH
, PWM1PRL
, PWM1S1P1H
, and PWM1S1P1L
.
PWM1PRH
andPWM1PRL
are the high and low bytes of the PWM subsystem’s “period” register.PWM1S1P1H
andPWM1S1P1L
are the high and low bytes of the PWM subsystem’s “duty cycle” register
Compute the 16-bit decimal value of both registers from their given hexadecimal values. Given the fact that you set the initial duty cycle as 50%, how does this explain the difference between the period and duty cycle values you calculated?
Scroll down in this file further and find the functions that set the period register fir PWM1 as well as the function that sets the duty cycle register for PWM1, Output 1.
Note: You should be noting the function name, the names and data types of the inputs to the function, and the return value type.
Find and note the necessary details for the function that loads the buffer registers.
Open main.c in the project explorer.
while(1)
loop, use the function you noted in the previouis step to change the duty cycle registerCan you change the period to 10Hz, with a duty cycle of 50%?