Getting Started with MPLAB® Harmony v3 Peripheral Libraries on PIC32MK GP MCUs: Step 2

Last modified by Microchip on 2023/11/09 09:09

Configure TMR2 Peripheral Library

Click on the Device Resources tab in the left pane in the MPLAB® Code Configurator (MCC) window. Expand Harmony > Peripherals > TMR > TMR2.

Double click or drag and drop TMR2 to add the Timer2 Peripheral Library (PLIB) to the project graph.

Add TMR2

In PIC32MK GP devices, by default, TMR2 is configured as a 16-bit timer and is sourced by a 60 MHz Peripheral Bus Clock (PBCLK). Using the default TMR2 configuration, the generation of 500 milliseconds, one second, two seconds, and four seconds period is not possible. To generate 500 milliseconds, one second, two seconds, and four seconds time periods, TMR2 needs to be configured as a 32-bit timer.

Back to Top


In the project graph, configure the TMR2 PLIB as a 32-bit timer and set the timer period as 500 milliseconds.

TMR2 configuration setup

Back to Top


Verify that PBCLK2 is set to 60 MHz.

TMR2 clock setup

Back to Top


Configure CORE TIMER Peripheral Library

Under the Device Resources tab, expand Harmony > Peripherals > CORE TIMER
Double click on CORE TIMER to add the CORE TIMER PLIB to the project graph.

Add Core Timer

The core timer is configured to use the CORE TIMER PLIB. The CORE TIMER PLIB provides blocking timer delay APIs. The blocking timer delay APIs are needed when initializing the temperature sensor.

Back to Top


In the project graph, use the default CORE TIMER PLIB configuration as shown.

Core Timer configuration setup

Back to Top


Configure SPI PLIB and SPI Pins

Under the Device Resources tab, expand Harmony > Peripherals > SPI >SPI6

Double click on SPI6 to add the SPI instance 6 to the project.

SPI selection

Select the SPI6 Peripheral Library and disable the Master Mode Slave Select Enable bit, as shown.

SPI setup

  1. Retain the SPI6 baud rate as the default 1 MHz because the temperature sensor chip on the MikroElectronika Weather click board can operate at 1 MHz.
  2. As per the BME280 sensor data sheet, the chip select (slave select) bit needs to go LOW when SPI communication (read/write) starts and needs to go HIGH when SPI communication stops. If the MCC SPI configuration MASTER Mode Slave Select Enable bit is enabled, SPI peripheral library (PLIB) will control the chip select bit on every byte transfer. Because of this, the chip select bit needs to be controlled manually by the application program code as a GPIO pin. For this, the MASTER Mode Slave Select Enable bit needs to be disabled, which will disable the control of the selected chip through the SPI PLIB.

​​

Back to Top


Open the Pin Configuration tabs by clicking Project Graph > Plugins > Pin Configuration

Open pin configuration

Back to Top


In MPLAB Code Configurator (MCC), select the Pin Settings tab and sort the entries by Ports as shown.

Sorting pins

Now, select the MCC Pin Table tab and then scroll down to the SPI6 module as shown.

  • Enable SPI Clock (SCK6) on Pin #23 (RA1)
  • Enable SPI MISO (SD06) on Pin #24 (RB0)
  • Enable SPI MOSI (SDI6) on Pin #14 (RG9)

Setting up SPI pins

Setting up SPI pins

Click the Pin Settings tab and then scroll down to the GPIO pin RC0 (Pin Number 32) in the Pin ID column and configure RC0 as an output pin for SENSOR_CS functionality as shown.

Setting up SPI pins

  1. This completes the configuration of the SPI PLIB. The application code will use the SPI PLIB Application Programming Interfaces (APIs) to read temperature values from the temperature sensor.
  2. In the Pin Table window, any peripheral or any pin can be isolated from other peripherals or other pins by right clicking and choosing that specific peripheral or that specific pin.

Peripheral and pin isolation

​​​​​​

Back to Top


Step 2.4: Configure Universal Asynchronous Receiver Transmitter (UART) PLIB and UART Pins

Under the Device Resources tab, expand Harmony > Peripherals > UART > UART6.

Double click on UART6 to add the UART instance 6 to the project.

UART selection

Select the UART6 Peripheral Library in the project graph and configure it as shown.

UART setup

Verify the default baud rate is set to 115200 Hz.

​UART interrupt is disabled because Direct Memory Access (DMA) will be used (configured in future steps) to transfer application buffer to the UART TX register.

Back to Top


Select the Pin Table tab and then scroll down to the UART6 module as shown.

Enable UART_TX (U6TX) on Pin #51 (RA4).

UART pins setup

The application will use the UART PLIB for printing messages on the serial terminal. Hence, in the UART6 configuration, only the transmit pin is configured and the receive pin is not configured.

Back to Top


Step 2.5: Configure DMA PLIB

Open the DMA Configurator tab by clicking Project Graph > Plugins > DMA Configuration.

Open DMA configuration

Back to Top


In the DMA Settings window, enable and configure Direct Memory Access (DMA) Channel 0 to transfer application buffer to the UART TX register as shown. The DMA transfers 1 byte from the user buffer to UART transmit buffer on each trigger.

  • The Priority drop-down option for a channel helps give priority to a DMA channel over the other when more than one DMA channels are configured by the application. Since in this application only one DMA channel is configured, the default priority (zero or CHPRI0) is not changed.

UART DMA setup

UART transmit buffer empty event triggers the DMA to transfer 1 byte of data from source (user buffer) to destination (UART Tx register). When all the requested bytes are transmitted, the DMA PLIB notifies the application by calling the registered DMA callback event handler.

Back to Top