Skip to content

Demonstrates basic UART communication on PIC32CM JH devices using MPLAB X and Harmony v3, sending a periodic "Hello World" message over SERCOM1.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic32cmjh-uart-hello-world-mplab-harmony

Repository files navigation

Microchip Technologies Inc.

Getting Started with SERCOM (UART) Communication on PIC32CM M0+ Devices

This example project demonstrates basic UART communication using the SERCOM peripheral on a PIC32CM JH00 device. The application transmits the message “Hello World” once every 500 ms using the SysTick timer, and the output can be viewed on a serial terminal connected to the board. This project provides a straightforward introduction to initializing the SERCOM for UART operation and sending periodic messages.

While this project uses a PIC32CM JH00 device, the same initialization and communication principles apply to other PIC32CM M0+ devices.

Hardware Used

Software Used

Opening Projects in Visual Studio Code (VS Code®)

This example project was developed in MPLAB X IDE and can also be opened in VS Code® using the MPLAB Extension Pack. Follow the provided instructions here to import and run the project in VS Code®.

For a step-by-step walkthrough on importing the project, watch the video: Importing an MPLAB® X Project into Microsoft® VS Code®

Related Documentation

Operation

UART Communication Concept

UART communication is based on a simple point-to-point connection between two devices. One device’s TX (transmit) pin connects to the other device’s RX (receive) pin, and vice versa. This crossover allows data to flow in both directions, as shown in the following figure.

In this configuration, the UART provides a straightforward way to exchange data without the need for a clock signal. For communication to work correctly, both devices must be configured with the same parameters:

  • Baud Rate – Defines the speed of communication in bits per second. If the baud rates do not match, the timing of bits will drift and data will be corrupted.
  • Data Bits – Specifies how many bits represent the actual data (commonly 8). Both devices must agree to properly interpret each frame.
  • Parity – An optional error-checking bit (None, Even, or Odd). If enabled, both sides must use the same setting to validate data integrity.
  • Stop Bits – Mark the end of each data frame (typically 1 or 2). Mismatched stop bits cause framing errors.

Together, these settings ensure that the transmitted bits are correctly packaged, sent, and interpreted on the receiving side.


BasicUART

Application on PIC32CM:

In this example, the concept is applied using the SERCOM1 peripheral configured as a UART. The onboard debugger uses a Communications Device Class (CDC) interface that appears on the host computer as a virtual COM port, allowing the data to stream in both directions between the host and the target device.

Characters sent from the host are transmitted on the debugger’s CDC TX pin, while characters received on the CDC RX pin are returned to the host. The UART output can be viewed in a terminal program such as the MPLAB Data Visualizer, making it easy to monitor or interact with the application over a USB without extra hardware.


CDCDebugger

Application Sequence/Output Behavior

The application performs the following sequence:

  1. SERCOM1 is initialized as a UART interface with the chosen baud rate.
  2. The SysTick timer is initialized and provides a 500 ms time base.
  3. A fixed message string, Hello World, is transmitted over the USB CDC connection to the host computer.
  4. After 500 ms, the process repeats continuously.

Once programmed, the device continuously sends the Hello World message, which appears in a terminal window on the host computer through the Curiosity Nano’s CDC virtual COM port.

Running the Example project

  1. Connect the Debug USB port of the PIC32CM JH00 Curiosity Nano to a PC using a Micro-USB to USB 2.0 cable.
  2. If not already installed, download and install MPLAB X IDE version 6.25 or newer.
  3. If not already installed, download and install the XC32 C-Compiler version 4.60 or newer.
  4. Clone or download this project from GitHub to the local machine.
  5. If downloaded as a .zip file, extract the files to a location preferably as close as possible to the root directory.
  6. In MPLAB X IDE, go to File → Open Project. Navigate to the extracted location and select the project file:

    FilePath

  1. Click the Make and Program Device button in the MPLAB X IDE toolbar to program the device. Then, verify that the device is successfully programmed.
    Program
    ProgramComplete

View the results:

To view the output of this project, launch the MPLAB Data Visualizer from the MPLAB X IDE toolbar. In the Data Visualizer, select the serial COM port associated with the PIC32CM JH00 Curiosity Nano and confirm that the settings match the configured SERCOM UART parameters in the Project Configuration section.


TerminalWindow


The UART pins can be probed with a logic analyzer, where the output should decode as "Hello World\r\n" at the configured baud rate, confirming proper signal operation.

LogicCapture


Project Configuration

This example project is already provided as a preconfigured MPLAB X project. Start by opening the project in MPLAB X IDE. Once the project is loaded, launch the MPLAB® Code Configurator (MCC) in Harmony to review the configuration.

For detailed instructions on how to create and set up a new MPLAB X project and open MCC Harmony, refer to the this Microchip online reference here

When opened in MCC Harmony, the Project Graph provides a visual overview of the project, showing the active components and their interactions.


ProjectGraph


With the Project Graph open, review the configuration step by step. Use the following checklist to confirm each setting before proceeding:

Step 1: Confirm Clock Settings

Open the Clock Configurator from the Plugins section in the Project Graph to verify the system clock is set to 48 MHz.

To check the system clock, go to Project Graph → Plugins → Clock Configurator


ClockConfigPlugins
ClockConfig48

Note: Most Harmony v3 projects default to the 48 MHz internal oscillator.


Step 2: Configuring the System Tick (SysTick)

The SysTick is a built-in timer that allows the application to create precise delays without relying on software loops. In this demonstration, it will be used to add a fixed delay between each Hello World message sent over the UART.

To enable SysTick:

  1. In the Project Graph, select the System module.
    SystemModule

  1. In the Configuration Options panel, expand Cortex M0+ Configuration and enable the SysTick option.
    SysTickConfig

Step 3: Add and Configure the UART Peripheral (SERCOM1)

The device includes multiple SERCOM peripherals that can be configured as UART, SPI, or I²C. On the Curiosity Nano, only SERCOM1 is internally routed to the onboard debugger’s CDC interface, so it must be used for sending the UART data to a terminal over a USB.

This information is available in the Pinout section of the Curiosity Nano User's Guide.


SERCOMPinOut


To add and configure SERCOM1:

  1. In Device Resources, expand the SERCOM module and add SERCOM1 to the project.
    SERCOM1DeviceResources

  1. Once added, Select SERCOM1 in the Project Graph. In the Configuration Options panel, apply the following settings:
    • Operation Mode: USART with internal Clock. Enables UART mode for the selected SERCOM instance using the internal clock.
    • Transmit/Receive Enable: Enable both Transmit is required to send data to the terminal; receive is optional but recommended for flexibility.
    • Baud Rate: Set to 115200.
    • All other settings can remain at default for this project.
      SERCOMConfig

Step 4: Assign UART Pins (SERCOM1)

After configuring SERCOM1, the UART TX and RX pins must be mapped to the I/O lines connected to the debugger’s CDC interface. This enables UART messages to be sent and received through the Curiosity Nano’s USB connection

According to the Curiosity Nano user guide:

  • PA17 is used for RX
  • PA16 is used for TX
    TXRXPins

To assign the pins:

  1. In the Project Graph, open Plugins → Pin Configuration.

    PinConfigPlugins

  1. In the Pin Settings tab. Locate PA16 (Pin #35) and PA17 (Pin #36) in the Pin Table.
    • Set the following:
      • PA16 → SERCOM1_PAD0 (RX): Used by the MCU to send data out to the terminal
      • PA17 → SERCOM1_PAD1 (TX): Used by the MCU to receive data coming from the terminal
        PinConfig

Step 5: Generate the MCC Code

With the UART and SysTick configured, the final step is to generate the code. The MCC creates the initialization files, peripheral drivers, and APIs for UART communication and SysTick timing, preparing the project for application development.
Generate


Application Source Code

This code defines a macro for "Hello World", starts the SysTick timer, and enters a loop that sends the "Hello_World" message over the UART using SERCOM1_USART_Write(). Timing is provided by SYSTICK_DelayMs() to create a 500 ms delay and prevent flooding the terminal.


MCC-Generated APIs Used

  • SYSTICK_TimerStart() – starts the SysTick timer so delay functions are available.
  • SYSTICK_DelayMs(x) – creates a blocking delay for the specified number of milliseconds.
  • SERCOM1_USART_Write() – sends a buffer of bytes over the SERCOM1 UART interface.
#define Hello_World "Hello World \r\n"

int main(void)
{
    /* Initialize all modules */
    SYS_Initialize(NULL);

    SYSTICK_TimerStart();

    while (true)
    {
        SERCOM1_USART_Write(Hello_World, sizeof(Hello_World));
        SYSTICK_DelayMs(500);
    }

    /* Execution should not come here during normal operation */
    return (EXIT_FAILURE);
}

Conclusion

This project showed how to configure the SERCOM in UART mode on a PIC32CM M0+ device using MCC Harmony. The system clock, SysTick timer, and SERCOM1 peripheral were set up to send a "Hello World" message every 500 ms over the Curiosity Nano’s USB CDC interface. This simple example provides a foundation for expanding into bidirectional communication or more advanced embedded applications.

Related Projects

About

Demonstrates basic UART communication on PIC32CM JH devices using MPLAB X and Harmony v3, sending a periodic "Hello World" message over SERCOM1.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages