Setting up a new PIC project

Introduction

The purpose of this tutorial is to get you familiar with the MPLabX programming environment to program the PIC18F47Q10 microcontroller to flash a LED.

Resources

Tools Required

Item Description
PIC18F47Q10
Snap Programmer A standalone programmer
Breadboard A breadboard set up for programming with the Snap Programmer (see this tutorial)
LED
Resistor Selected to limit current going through the LED
3.3V Power Supply Required to power the PIC

Install MPLabX

Go to Microchip.com, and download MPLabX and the MPLab XC8 Compiler.

Follow all instructions and accept all defaults

Click Next
Click Next

Accept The License
Accept The License

Accept Default Settings
Accept Default Settings

Select MPLabX and 8-bit MCUs
Select MPLabX and 8-bit MCUs

Click Next
Click Next

Wait for the install to finish
Wait for the install to finish

Say yes to install all device drivers
Say yes to install all device drivers

Select MPLab XC8 Compiler
Select MPLab XC8 Compiler

Proceed through the XC8 Installer
Proceed through the XC8 Installer

Accept the License Agreement
Accept the License Agreement

Select the Free License
Select the Free License

Select Defaults
Select Defaults

You will want to make sure you check the “Add to Path” Option at this step:

Check the “Add to Path” Option
Check the “Add to Path” Option

Allow network access
Allow network access

Update the Software

  1. When you open MPLabX, you may see a notification to update plugins. Click the link.

    Click on the notification to update your install
    Click on the notification to update your install

  2. If you want to manually initiate an update, go to “Tools” –> “Plugins”.

    Select all updates and hit “update”
    Select all updates and hit “update”

  3. In the first tab (“Updates”), select all available updates and then click the “update” button.

  4. Walk through the dialog boxes

  5. Restart the application if it asks. It will take a moment.

  6. MPLabX Will restart
    MPLabX Will restart

Install MPLAB Code Configurator (optional)

Note: The MPLab Code Configurator is installed by default. The following steps detail how to install the plugin if it was not automatically installed.

  1. Select “Plugins” under “Tools”
  2. Select the second tab (“Available Plugins”) and check the “MPLAB Code Configurator.” Click “Install” to install this plugin.
  3. Once the Code Configurator has been installed, you can open it by clicking “Tools” - “Embedded”.

Create Your First Project

  1. After finish installing the MPLABX IDE, you can plug the USB mini cable into your programmer’s debugging port. Once you plug the USB to your PC, a green LED will light up. This indicates the programmer has been powered up.

  2. To create a new project, go to “File” - “New Project” - select “Application Project” - and click “Next”

    Create a New Project
    Create a New Project

    Select Microchip Embedded and “Application Project(s)”
    Select Microchip Embedded and “Application Project(s)”

    1. From “Family” select Advanced 8-bit MCUs (PIC 18) to filter the available list.

    2. Select Device as “PIC18F47Q10” and click “Next”

    3. If you have your Snap Programmer connected to your PC, the device will show up.

      Select the Snap Programmer and click “Next”

    Select the PIC and the Programmer
    Select the PIC and the Programmer

  1. Select the “XC8” as the compiler for this project.

    Select the XC8 Compiler
    Select the XC8 Compiler

  2. Next give a name to your first project as well as the location where you want to put your project.

    Name your Project
    Name your Project

  3. When your new project opens, the Microchip Code Configurator (MCC for short) should open. You will be asked whether to use MCC “Classic” or “Melody” (Melody is default, with “classic” offered at the bottom)

    Select Melody
    Select Melody

    Note: As of 2025, we recommend melody, as it is the more modern version of MCC, with more support planned going forward.

  4. You will also be asked to install any missing packages. Go ahead and accept any and all suggestions when this screen pops up.

    Update Content
    Update Content

    Content is downloading
    Content is downloading

    Content is downloaded
    Content is downloaded

    Allow Netowrk Access
    Allow Netowrk Access

3. Getting Familiar with Code Configurator

<!-- ![caption](VirtualBox_Win11.2024_21_01_2025_09_27_05.png) -->

<!-- ![caption](VirtualBox_Win11.2024_21_01_2025_09_28_03.png) -->

MCC Layout
MCC Layout

To open MCC, select the blue MCC icon on the top ribbon of MPLabX’s IDE.

MCC Layout(colored)
MCC Layout(colored)

  • Application Builder (Center Area, magenta)

    Graphical block diagram of all the system blocks that are available for configuration.

  • Pin Manager (Bottom Right Area, yellow)

    Configure the direction (Input/Output), external interrupt, and PWM of GPIO pins in this area.

  • Pin Package View (Bottom Left Area, blue)

    Indicates the pinout of the selected package type. Ensure you have selected the right package. (PDIP in our case)

  • Device Resources (Mid Left Area, red)

    Peripherals (UART, PWM, I2C, SPI, etc) that available to the microcontroller.

  • Project Resources (Top Left Area, green)

    The peripherals have been added/used in the microcontroller.

  • Resources Setups/ Register Setup (Top Right Area, cyan)

    The detailed register set up for the selected peripheral.

  • Code Generate Button (Next to “Project Resources”, in the green area)

You will next turn on and off an external LED by using a GPIO pin on the Curiosity.

Code Configurator Setup

  1. We are going to select pin RD1 for our LED’s digital output.

  2. Double Click Pin Module under Project Resources.

    Pin RD1 Configured
    Pin RD1 Configured

  3. Lock the Port A - Pin 2 to GPIO - Output.

  4. Click the Generate button to generate code for the GPIO module.

    Generate
    Generate

    Configuration Complete
    Configuration Complete

    The Code Configurator setup for Lab 4.1 should look like this, with the message of “Generation Complete”

MCC (Microchip Code Configurator) Generated Files and Main.c

  1. Go back to Projects, you will see two subfolders called MCC Generated Files under both Header Files and Source Files. These folders contain the generated code, functions, and macros by MCC (Microchip Code Configurator).

  2. The list of MCC generated functions or macro could be found at the header files. (A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.)

    pin.h functions
    pin.h functions

    1. Since we only use GPIO as our peripheral, we can double click the “pins.h” and see all the macros that MCC generated for us.

    2. To enable the onboard LED, we can use the following macros (also highlighted at the screenshot below):

    Macros Purpose of the Macro
    IO_RD1_SetHigh() Set pin RD1 as logic High
    IO_RD1_SetLow() Set pin RD1 as logic Low
    IO_RD1_Toggle() Set pin RD1 opposite to the previous logic state
  3. Go back to the “main.c” and use the macros inside the main function.

    main.c
    main.c

    You can also add a delay function in order to see the on and off transition of the LED.

    IO_RD1_SetHigh(); //Set RD1 (LED0) to logic high, turn on
    __delay_ms(1000); //delay
    IO_RD1_SetLow();  //Set RD1 (LED0) to logic low, turn off
    __delay_ms(1000); //delay
    
  4. Click the Hammer button to compile the project. If you see “Build Successful”, then you can flash the program to the microcontroller by clicking the Run or download button in the top ribbon.

Note: Another tutorial provides more detail regarding setting up an external programmer.