Harmony v3 Peripheral Libraries on PIC32MK GP: Step 2

Step 2.1: Configure TMR2 Peripheral Library

1

Click on the Available Components tab in the left pane in the MPLAB® Harmony Configurator (MHC) window. Expand Peripherals > TMR.
Double click or drag and drop TMR2 to add the Timer2 Peripheral Library (PLIB) to the project graph.

tmr2_adding.png

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.

2

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

tmr2_configuration_setup.png

3

Verify that PBCLK2 is set to 60 MHz.

tmr2_clock_setup.png

Step 2.2: Configure CORETIMER Peripheral Library

1

Under the Available Components tab, expand Peripherals > CORE TIMER
Double click on CORE TIMER to add the CORE TIMER PLIB to the project graph.

coretimer_adding.png

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.

2

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

coretimer_configuration_setup.png

Step 2.3: Configure SPI PLIB and SPI Pins

1

Under the Available Components tab, expand Peripherals > SPI

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

spi_selection.png

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

spi_setup.png
  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 MHC 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.

2

Open the Pin Configuration tab by clicking Tools > Pin Configuration.

open_pin_configuration.png

3

In MPLAB® Harmony Configurator (MHC), select the Pin Settings tab and sort the entries by Ports as shown below.

pins_sorting.png

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

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

Select the MHC 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 below.

spi_pins_setup_2.png
  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.
isolation_step.png

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

1

Under the Available Components tab, expand Peripherals > UART.

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

uart_selection.png

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

uart_setup.png

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.

2

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

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

uart_pins_setup.png

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.

Step 2.5: Configure DMA PLIB

1

Open the DMA Configurator tab by clicking Tools > DMA Configuration.

open_dma_configuration.png

2

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 below. 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.png

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.



Next Step >

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.