Getting Started with MPLAB® Harmony v3 Peripheral Libraries on PIC32MZ EF MCUs

Last modified by Microchip on 2023/11/10 11:00

Warning: This tutorial uses the old Harmony configuration tool (MPLAB® Harmony Configurator (MHC)). New Harmony projects should use the MPLAB Code Configurator (MCC) instead.

 

Objective

MPLAB Harmony v3 is a flexible, fully integrated embedded software development framework for 32-bit microcontrollers (MCUs) and microprocessors (MPUs). MPLAB Harmony v3 includes the MPLAB Harmony Configurator (MHC) tool, a set of modular peripheral libraries, drivers, system services, middleware, and numerous example applications, all of which are designed to help you quickly and easily develop powerful and efficient embedded software for Microchip’s 32-bit PIC® and SAM devices.

This tutorial shows you how to use the MHC to create an application that gets you started in developing applications on PIC32MZ EF MCUs using the MPLAB Harmony v3 software framework.

The application makes use of the Curiosity PIC32MZ EF 2.0 Development Board and the I/O1 Xplained Pro Extension Kit.

The application reads the current room temperature from the temperature sensor on the I/O1 Xplained Pro Extension. The temperature read is displayed on a serial console periodically every 500 milliseconds. The periodicity of the temperature values displayed on the serial console is changed to one second, two seconds, four seconds, and back to 500 milliseconds every time you press the switch SW1 on the Curiosity PIC32MZ EF 2.0 Development Board. Also, LED1 is toggled every time the temperature is displayed on the serial console.

The application you create utilizes:

  • I²C Peripheral Library (PLIB) to read the temperature from a temperature sensor.
  • TMR1 PLIB to periodically sample temperature sensor data.
  • Universal Asynchronous Receiver Transmitter (UART) and Direct Memory Access (DMA) PLIBs to print the temperature values on a COM (serial) port terminal application running on a PC.
  • GPIO PLIB to change the periodicity of temperature sensor data read using the SWITCH press event and toggle the LED.

In the process, the lab also demonstrates the use of callback functions.

Two Ways To Use This Tutorial

  1. Create the project from scratch:
    • Use the provided source files and step-by-step instructions below.
  2. Use the solution project as an example:
    • Build the solution project and program it to the Curiosity PIC32MZ EF 2.0 Development Board to observe the expected behavior.

Back to Top

Lab Objectives

  1. Create an MPLAB X IDE Harmony v3 project for a PIC32MZ EF microcontroller from scratch.
  2. Use the MHC to configure and generate Harmony v3 PLIBS code for TMR1, I²C, UART, DMA, and GPIO peripherals.
  3. Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.

Back to Top

Materials

Hardware Tools

The Curiosity PIC32MZ EF 2.0 Development Board includes a PICkit™ On Board 4 (PKOB4). No external tools are necessary to program or debug the PIC32MZ2048EFM144. For programming/debugging, the PKOB4 connects to the host PC through the USB Micro-B connector on the Curiosity PIC32MZ EF 2.0 Development Board.

Hardware Setup

Hardware Setup

Hardware Connection Setup

Apart from the hardware tools listed above, the following items are required:

  • USB Type-A male to Micro-B male cable for programming/debugging.

Software Tools

​This project has been verified to work with the following versions of software tools: MPLAB X IDE v5.30, MPLAB XC32 Compiler v2.20, MPLAB Harmony CSP v3.4.0, MPLAB Harmony DEV_PACKS v3.4.0, MHC v3.3.1, and MPLAB Harmony Configurator Plugin v3.3.1

We update our tools regularly. If you discover an issue while using the newer version, we recommend the usage of the same version with which the project was tested.

For this lab, download the following repositories from GitHub:

  • CSP: The following table shows the summary of contents.
FolderDescription
appsExample applications for CSP library components
archInitialization and starter code templates and data
docsCSP library help documentation
peripheralPLIB templates and configuration data
  • DEV_PACKS: The following table shows the summary of contents.
FolderDescription
MicrochipPeripheral register specific definitions
armCore Specific Register definitions (CMSIS)
  • MHC: The following table shows the summary of contents.
File/FolderDescription
docHelp documentation and licenses for libraries used
np_templatesNew Project templates for supported toolchains
*.jarJava implementations of MHC modules
mhc.jarMain Java executable (run: java -jar mhc.jar -h)
runmhc.batWindows cmd batch file to run standalone MHC GUI

Back to Top

Overview

This lab shows you how to create an MPLAB Harmony v3 project from scratch, configure, and generate Harmony v3 PLIBs code for TMR1, I²C, UART, DMA, and GPIO peripherals. It demonstrates the reading of temperature sensor values from the temperature sensor available on the I/O1 Xplained Pro Extension kit periodically and displays it on a serial console. The periodicity of temperature sampling is changed for every switch press event. Every time the temperature is displayed on the serial console, an LED is toggled.

Figure 1.1 Application Flow Sequence

The application initializes the clock, GPIO, and other peripherals (configured through the MHC) by calling the function SYS_Initialize.
The application registers callback event handlers for I²C, DMA, TMR1, and GPIO PLIBs. The callback event handlers are called back by the PLIBs when the transaction completion events occur.
Note:
A callback event handler for UART is not registered as the actual UART data transfer, it is accomplished by the DMA. The DMA calls back the callback event handler when the DMA transfer request is completed.

The application checks whether the configured TMR1 timer period has expired. On every timer period expiration, the application calls the function I2C1_WriteRead to submit a temperature sensor read request to the I²C PLIB. The I²C PLIB calls back the registered callback event handler when the latest temperature value is read from the sensor. The application sets a flag in the I²C callback event handler.

Application Flow Sequence

Figure 1.1 shows the flow sequence of the application task.
 

The application checks the temperature read complete flag to submit a write request to DMA to print the latest temperature value (in a formatted message) onto the serial console over the UART interface. The application also monitors the pressing of the switch SW1. If a switch press is detected, the application changes the temperature sampling rate from the default 500 milliseconds to one second. On subsequent switch press, the application changes the temperature sampling rate to two seconds, four seconds, and back to 500 milliseconds. The application cycles the temperature sampling rate on every switch press, as shown in Figure 2.

The application cycles the temperature sampling rate on every switch press

Figure 2

The application also toggles LED1 every time the latest temperature value is displayed on the serial console.

Back to Top

Lab Source Files and Solutions

This ZIP file contains the completed solution project for this lab. It also contains the source files needed to perform the lab as per the following step-by-step instructions (see the "Procedure" section on this page).

The contents of this ZIP file need to be placed in the below folder:

<Any directory of your choice>/
(example Directory = C:/microchip/harmony/v3_4_0)

 

‍ZIP

Extracting the ZIP file creates the following folders:

  • pic32mzef_getting_started contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
    • dev_files contains subfolder pic32mz_ef_curiosity_v2 containing application source files and other support files (if any) required to perform the lab (see "Procedure" section below).
    • firmware contains the completed lab solution project. It can be directly built and programmed on the hardware to observe expected behavior.

Back to Top

Procedure

All the steps mentioned below must be completed before you build, download, and run the application.

Lab Index

Step 1: Create project and configure the PIC32MZ EF

  • Step 1.1 - Install the MPLAB® Harmony Configurator (MHC) Plugin in MPLAB X IDE
  • Step 1.2 - Create MPLAB Harmony v3 Project using MPLAB X IDE
  • Step 1.3 - Verify Clock Settings

Step 2: Configure I²C, UART, and TMR1 Peripheral Libraries

  • Step 2.1 - Configure TMR1 Peripheral Library
  • Step 2.2 - Configure I²C Peripheral Library and I²C pins
  • Step 2.3 - Configure UART Peripheral Library and UART pins
  • Step 2.4 - Configure DMA Peripheral Library
  • Step 2.5 - Configure Cache Maintenance

Step 3: Configure Pins for Switch and LED

  • Step 3.1 - Configure switch button pin with GPIO to generate an interrupt
  • Step 3.2 - Configure LED Pin
  • Step 3.3 - Rename the default main file

Step 4: Generate Code
Step 5: Add application code to the project
Step 6: Build, program and observe the outputs

Back to Top