Enabling UART in MPLabX (PIC18F57Q43 Curiosity Nano)

Introduction

The purpose of this tutoial is to enable basic serial communication to your computer over the UART subsystem in your PIC microcontroller.

Note: This tutorial continues from the Hello World Tutorial for the PIC18F57Q43 Curiosity Nano. With small changes, it is applicable to a wide range of PIC microcontrollers, especially other Curiosity Nano Boards.

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
Is it possible to communicate with it once programmed over USB? Yes, see section 4.1.1 and figure 4-1 for details on how the CDC RX/TX pins connect to the microcontroller’s UART pins.

Instructions

  1. If closed Open up MPLabX and load your previous hello-world-uart project.

  2. Open MCC by clicking on the MCC icon in the top ribbon of MPLabX

    open MCC
    open MCC

  3. In the MCC window, select the “Device Resource” tab, scroll down and expand the “UART” item, and select “Add UART”

    Add UART
    Add UART

  4. A dropdown list should become visible to the right. Select “UART1”

    Select UART1
    Select UART1

  5. A configuration window should open to the right. Provide additional details for the UART subsystem

    • Baudrate 9600
    • Parity: None
    • Data Size: 8
    • Stop Bits: 1
    • Flow Control: None
    • Redirect Printf to UART: Turn on
    • Interrupt Driven: Keep off

    Configuration Page
    Configuration Page

  6. Go to the pin manager window. Referring to the hardware user guide, figure 4-1, select the appropriate RX and TX pins for the UART subsystem. These should match with the CDC TX and RX pins, respectively.

    What do we mean? The CDC RX and TX pins belong to the secondary programmer / communication microcontroller on the curiosity nano board that is wired to the pins of the USB connector. Look at figure 4-1 to see which pins the CDC RX and TX pins connect to on the PIC microcontoller. The CDC RX Pin connects to the microcontroller’s TX pin, and the CDC TX pin connects to the PIC’s RX pin. Set up MCC to match this diagram.

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

    Click “generate”
    Click “generate”

  8. Once you’ve generated MCC, click on the “clean and compile” button to build your project.

  9. Research: Now it’s time to do a bit more research into the functionality you just added. In the project explorer window, click on “source files” -> “MCC generated files” -> “uart” -> “src” -> “uart1.c” to answer the following questions

    navigate to “uart1.c”
    navigate to “uart1.c”

    Question Answer
    Which function enables the UART1 subsystem Look in uart1.c
    Which function tells you when a character is ready to be received? Look in uart1.c
    Which function receives a single character, and what datatype is returned? Look in uart1.c
    Which function tells you when the microcontroller is ready to send a single character? Look in uart1.c
    Which function sends a single character? Look in uart1.c
  10. Now we need to open and modify main.c in the project explorer. Inside the curly brackets after the while(1) statement, comment out your previous code:

    __delay_ms(500);
    IO_RF3_Toggle();
    

    Your goal is to:

    1. create a variable to store a character
    2. check whether there is a character ready to receive
    3. if so, receive that character
    4. if you have received a character, check whether the microcontroller is ready to send
    5. if so, send the character and toggle pin RF3
  11. Select the “clean and compile” button to verify your project compiles.

    clean and compile
    clean and compile

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

    download
    download

  13. Next, open up windows device manager and look for communication ports mapped to the curiosity nano. Record which COM port is used by the curiosity nano

  14. Next, open putty (or your serial communication program of choice), and, in serial mode, open up the COM port indicated from the last step. Use the same settings as those you defined in MCC for UART1:

    • Baudrate 9600
    • Parity: None
    • Data Size: 8
    • Stop Bits: 1
    • Flow Control: None
  15. Type something in the window and observe what happens.

    You should see the letters you type on the screen. This is a function of the microcontroller transmitting back what it sees. You should also see the user-defined LED on the curiosity nano board toggling each time you type a letter.