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.
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.
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.
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.
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.
Select the SPI6 Peripheral Library and disable the Master Mode Slave Select Enable bit, as shown below.
- 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.
- 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.
3
In MPLAB® Harmony Configurator (MHC), select the Pin Settings tab and sort the entries by Ports as shown below.
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)
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.
- 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.
- 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.
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.
Select the UART6 Peripheral Library in the project graph and configure it as shown below.
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).
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
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 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.