Getting Started With MPLAB® Harmony v3 Peripheral Libraries on PIC32MX 470 MCUs

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

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 Code Configurator (MCC) 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 MCC to create an application that gets you started in developing applications on PIC32MX470 MCUs using MPLAB Harmony v3 software framework.

The application makes use of PIC32MX470the  Curiosity Development BoardMikroElectronika Weather Click board™, and MikroElectronika USB UART Click board (sold separately).

The application reads the current room temperature from the temperature sensor on the MikroElectronika Weather Click board. 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 S1 on the PIC32MX470 Curiosity 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.
  • TMR2 PLIB to periodically sample temperature sensor data.
  • CORETIMER PLIB to use blocking timer delay for initializing temperature sensor.
  • UART and Direct Memory Access (DMA) PLIBs to print the temperature values on a COM (serial) port terminal application running on a PC.
  • GPIO Peripheral Library to change the periodicity of temperature sensor data read using 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 PIC32MX470 Curiosity Development Board to observe the expected behavior.

Back to top

Lab Objectives

  1. Create an MPLAB X IDE Harmony v3 project for a PIC32MX470 microcontroller from scratch.
  2. Use MCC to configure and generate Harmony v3 Peripheral Libraries code for TMR2, I²C, UART, CORETIMER, DMA, and GPIO peripherals.
  3. Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.

Back to top

Resources

The PIC32MX470 Curiosity Development Board includes a PICkit™ On Board (PKOB). No external tools are necessary to program or debug the PIC32MX470F512H. For programming and debugging, the PKOB connects to the host PC through the USB mini-B connector on the PIC32MX470 Curiosity Development Board.

Hardware setup

Back to top

Hardware Connection Setup

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

  • USB Type-A male to Mini-B male cable for programming/debugging.
  • USB Type-A male to Mini-B male cable to connect USB-UART serial port.

Back to top

This project has been verified to work with the following versions of software tools:
 MPLAB X IDE v6.10
 MPLAB XC32 Compiler v4.30
 MPLAB Harmony CSP v3.17.0
 MPLAB Harmony DEV_PACKS v3.17.0
 MCC v5.5.7
 MPLAB Code Configurator Plugin v5.3.7

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

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
peripheralPeripheral library templates and configuration data
  • DEV_PACKS - The following table shows the summary of contents.
FolderDescription
MicrochipPeripheral register specific definitions
armCore specific register differences (CMSIS)

Back to top.

Overview

This lab shows you how to create an MPLAB Harmony v3 project from scratch, configure, and generate Harmony v3 Peripheral Libraries code for TMR2, I²C, UART, CORETIMER, DMA, and GPIO peripherals. It demonstrates the reading of temperature sensor values from the temperature sensor available on the MikroElectronika Weather Click board 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.

The application initializes clock, GPIO, and other peripherals (configured through MCC) by calling the SYS_Initialize function.
The application registers callback event handlers for I²C, DMA, TMR2, CORE TIMER, and GPIO Peripheral Libraries. The callback event handlers are called back by the peripheral libraries when the transaction completion events occur.

Note: A callback event handler for UART is not registered as the actual UART data transfer is accomplished by the DMA. The DMA calls back the callback event handler when the DMA transfer request is completed.

Application Flow Sequence

The application checks whether the configured TMR2 timer period has expired. On every timer period expiration, the application calls the BME280_ReadRawWeatherData function 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 I²C read request from the sensor is completed. The application calls the BME280_GetTempReading function to get the compensated temperature value.

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 S1. 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 the following figure.

Application running LED sequence

The application also toggles user LED, 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)

Note:

  1. The project location of a Harmony v3 project is independent of the location of the Harmony Framework path (i.e., you need not create or place a Harmony v3 project in a relative path under the Harmony v3 framework folder). The project can be created or placed in any directory of your choice.
  2. The point above is true because when created, a Harmony v3 project generates all the referred source/header files and libraries (if any) under the project folder.
  3. Both points above contrast with the Harmony v2 project location. In Harmony v2, the project was supposed to be created in a location under the Harmony framework.

Back to top

Zip Files

Extracting the ZIP file creates the following folders:

  • The pic32mx470_getting_started folder contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
    • The dev_files folder contains subfolder pic32mx470_curiosity containing application source files and other support files (if any) required to perform the lab (see "Procedure" section below).
    • The firmware folder 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.

Back to top

Lab Index

Step 1: Create Project and Configure the PIC32MX470

  • Step 1.1 - Install the MPLAB® Code Configurator (MCC) Plug-in 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, CORE TIMER, and TMR2 Peripheral Libraries

  • Step 2.1 - Configure TMR2 Peripheral Library
  • Step 2.1 - Configure CORE TIMER 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 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