PWM Subsystem Tutorial (PIC18F57Q43 Curiosity Nano)

Introduction

This tutorial will lead you through setting up an MPLabX project for the PWM subsystem on the PIC18F57Q43 Curiosity Nano and programming it.

Resources

Parts Required

Item Detail
PIC18F57Q43 Curiosity Nano Website / User Guide
Breadboard
USB-C Programming cable

Critical Information and Concepts

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.

Instructions

If you haven’t completed the hello world tutorial for the PIC18F57Q43 devkit, this would be a good place to start.

  1. In MPLabX, select File $\rightarrow$ New Project to create a new project.

    Create a new project
    Create a new project

  2. Select Microchip Embedded as the category and “Application Project(s)” from the Projects list

    Select “Application Project(s)”
    Select “Application Project(s)”

  3. Select the PIC18F57Q43 as the “device” and the PIC18F57Q43 Curiosity Nano as the programmer

    select the appropriate device and programmer
    select the appropriate device and programmer

  4. Select the XC8 compiler

    Select the XC8 compiler
    Select the XC8 compiler

  5. Name your project something like “pwm-subsystem” and select your project’s parent directory.

  6. Your project will open. Select “next” to create a MCC profile using “Melody”, the current MCC engine.

    Select next
    Select next

  7. 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
    Download Support Files

  8. MCC Should open.

    MCC Configuration View
    MCC Configuration View

  9. In the MCC Window select the “device resources” tab and then select “device drivers” from the dropdown list

    Device Resources
    Device Resources

  10. Select PWM $\rightarrow$ PWM1 and click the “+” button to add the PWM1 subsystem to te MCC Configuration.

    Expand Drivers
    Expand Drivers

    Select “+” next to PWM1
    Select “+” next to PWM1

  11. A window to the right should open with more configuration settings.

    1. Select LFINTOSC as the clock source
    2. Select 0.001 kHz (1) as the clock frequency.
    3. Select 50 percent duty cycle for output 1.

    PWM Config, Top
    PWM Config, Top
    PWM Config, Bottom
    PWM Config, Bottom

  12. In the pin manager window, click on port F3 in the PWM1_output1 row to enable RF3 as a PWM1_output1 .

    Pin Config
    Pin Config

  13. Click the “generate” button to generate your MCC configuration

    generate MCC
    generate MCC

  14. Select the “clean and compile” button to verify your project compiles.

    Clean and complile
    Clean and complile

  15. Select the “download” button, ensuring your Curiosity Nano device is connected.

    download code
    download code

  16. Observe what happens.

    Critical Concept: Why did it take no code to flash the LED with the PWM?

  17. 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
    PWM1 header file

    1. 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.
    2. Read the text above each function. What is the proper order for calling these functions, and what function needs to be run afterwards?
  18. 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
    PWM1 source file

    1. Look for and note the initialized values for the following four values: PWM1PRH, PWM1PRL, PWM1S1P1H, and PWM1S1P1L.

      PWM1PRH and PWM1PRL are the high and low bytes of the PWM subsystem’s “period” register. PWM1S1P1H and PWM1S1P1L are the high and low bytes of the PWM subsystem’s “duty cycle” register

    2. 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?

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

    4. Find and note the necessary details for the function that loads the buffer registers.

  19. Open main.c in the project explorer.

    1. prior to the while(1) loop, use the function you noted in the previouis step to change the duty cycle register
    2. Implement a 90% duty cycle with a 1-second frequency.
    3. Compile, download, and program the PIC.
    4. Observe what happens. Is the LED on 90% of the time? Can you explain why?
  20. Can you change the period to 10Hz, with a duty cycle of 50%?