/pic18f16q41-uart-hello-world-mplab-mcc

In this application, it will show how to use the UART peripheral on MCC so the PIC18F16Q41 Curiosity Nano can display a message on a Terminal Emulator Program

Primary LanguageCOtherNOASSERTION

MCHP

Printing "Hello World" using UART for PIC18F16Q41

In this application, "Hello World!" is printed to a terminal emulator using the UART peripheral. This application is a good starting point for testing out the PIC18F16Q41.

Related Documentation

Software Used

Hardware Used

Setup

Step #1: Creating the Project

  • On the toolbar, click on New Project
  • Microchip Embedded; Standalone Project
  • Enter the Device
    • For this Project: PIC18F16Q41
  • Enter a name for this project, such as PrintMessageUART
    • Name: “PrintMessageUART”
    • Note: The project name cannot have any empty spaces

Step #2: MPLAB Code Configurator (MCC)

  • Set Configuration Bits

    • Disable "External Oscillator Mode Selection"
    • Set "Power-up Default Value for COSC" to "HFINTOSC with HFFRQ = 4MHz and CDIV = 4:1"
    • Ensure that "WDT Operating Mode" is set to "WDT Disabled; SWDTEN is ignored"

    Config Bit Settings

  • Modify the Clock Control

  • Set “Clock Source” to High Frequency Internal Oscillator (HFINTOSC)

  • Set “HF Internal Clock” to 4_MHz

  • Set “Clock Divider” to 1

Clock Settings

Step #3: Adding UART Peripheral

  • In Device Resources:
    • Drivers → UART → UART1
  • Once the peripheral is added, modify the peripheral.
    • Enable UART box should be checked
    • Enable transmit should be checked
    • Set the Baud Rate to 19200
    • Enable "Redirect STDIO to UART" in order to use the function (printf) for sending messages.
    • Everything else can be left as default settings

UART Register Settings

Step #4: Configure the Pins/Modify Pin Module

  • There are two pins we need to configure, Transmit (TX) and Receive (RX).
    • TX is connected to pin RB7
    • RX is connected to pin RB5
    • Set GPIO output on pin RC1 (Rename pin to: LED0)
    • Select the pins. When selected, the pins will change from a blue unlock into a green lock.

Pin Manager

Pin Module

Step #5: Generate the project

  • Click the generate button in MCC to create the appropriate header and source files for this configuration.

Generate

Step #6: Modifying main.c

  • Once the generation is complete, the new MCC generated header and source files will be in the project window. Select the main.c file and you will see an empty while(1) loop where you can add your application code.
  • Select on the source files and open the “main.c” file
    • As mentioned earlier we are going to put a printf and delay function in the while loop. This printf statement has what you want to be printed.
    while(1)
    {
        LED0_Toggle();
        printf("Hello World!\n\r");
        __delay_ms(500);
    }
  • Make and Program the Device

Step #7: Terminal Emulator

  • For this project, the terminal emulator program that is being used is TeraTerm.
  • Open up the serial terminal on the host computer and select the COM port associated with the Curiosity Nano. (COM port may vary)

Terminal Emulator Port Connection

  • Recall in the UART2 peripheral the baud rate is set to 19200. Configure the serial terminal to communicate at 19200 baud, no parity, and 1 stop bit.

    • Setup → Serial Port → Speed: 19200 → New Setting

      Terminal Emulator Settings

  • If everything is setup correctly, then the serial terminal should start displaying the printf statement in the while loop.

    Terminal Emulator display message

Summary

This application shows how to set up the UART peripheral and send a message to a serial terminal.