MPLAB® Harmony v3 Peripheral Libraries on PIC32MZ EF: Step 2

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

Configure I2C, UART, and TMR1 Peripheral Libraries

Configure TMR1 Peripheral Library

Under the Available Components tab, expand Peripherals > TMR1.
Double-click or drag and drop TMR1 to add the Timer1 Peripheral Library (PLIB) to the project graph.

Timer1 Peripheral Library (PLIB) in the project graph

Set TMR1 to run at 32,768 Hz low-speed clock.

  • When a module is added to the project graph, the MPLAB® Harmony Configurator (MHC) automatically enables the clock to the module. The default TMR1 clock source is the internal peripheral 100 MHz clock (PBCLK3), shown below in green highlight.
  • On the PIC32MZEF device, TMR1 can be clocked through several clock sources with frequencies ranging from 32,768 Hz to 200 MHz. TMR1 can be sourced through a low-speed clock using the secondary oscillator. Configure the secondary oscillator as shown below in red highlight.

TMR1 schematic

  • Go back to the project graph and select timer clock source as 32,768 Hz low-speed clock source through External clock from T1CKI pin.

Configuration Options tab TMR1

Configure the TMR1 PLIB to generate a compare interrupt every 500 milliseconds.

  • The 32,768 Hz low-speed Timer1 Clock Frequency is sufficient to generate periods of 500 milliseconds and 1 second, but it is not sufficient to generate periods of 2 seconds and 4 seconds.
  • To generate periods of 2 seconds and 4 seconds, it needs further low-speed Timer1 Clock Frequency. This can be achieved using the TMR1 Prescaler. Configure TMR1 > Select Prescaler to 1:8 prescale value. This will generate periods of 2 seconds and 4 seconds at run time. Also, configure the Timer Period (Milli Sec) to 500.

Configuration Options tab TMR1

Note: Ensure that the Enable Interrupts ? option is checked.

Back to Top


Configure I²C Peripheral Library and I²C pins

Under the Available Components tab, expand Peripherals > I²C

Double click on I²C1 to add the I²C instance 1 to the project.

I2C1 in the Project Graph pane

Select the I²C1 Peripheral Library and use the default configuration as shown below.

I2C1 Configuration Options tab

Note: Retain I²C1 speed as default 50,000 Hz because the temperature sensor chip on I/O1 Xplained Pro Extension Kit can operate at 50,000 Hz.

Open the Pin Configuration tabs by clicking MHC > Tools > Pin Configuration.

MHC > Tools > Pin Configuration menu

 Select the MHC Pin Table tab and then scroll down to the I²C1 module as shown below.

  • Enable I²C Clock (SCL1) on RA14 (Pin #95).
  • Enable I²C Data (SDA1) on RA15 (Pin #96).

MHC Pin Table tab

Note: This completes the configuration of the I²C PLIB. The application code will use the I²C PLIB Application Peripheral Interfaces (APIs) to read temperature from the temperature sensor.

Back to Top


Configure Universal Asynchronous Receiver Transmitter (UART) PLIB and UART pins

Under the tab Available Components tab, expand Peripheral > UART.

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

 Available Components tab, expand Peripheral > UART

Select the UART6 Peripheral Library in the Project Graph and configure it as shown below.

UART6 Peripheral Library in the Project Graph

Verify that the default baud rate is set to 115,200 Hz.

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

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

Enable UART_TX (U6TX) on RF2 (Pin #79).

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

Pin Table tab

Note: 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


Configure DMA Peripheral Library

Launch DMA Configurator by going to the MHC tab in MPLAB X IDE and then selecting Tools > DMA Configuration.

Tools > DMA Configuration menu

Click on the DMA Settings tab. Enable and configure DMA Channel 0 to transfer the application buffer to the UART TX register as shown below. The DMA transfers one byte from the user buffer to the 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 channel is configured by the application. Since in this application, only one DMA channel is configured, the default priority (zero or CHPRI0) is not changed.

DMA Settings tab

Note: UART transmit buffer empty event triggers the DMA to transfer one byte of data from the source (user buffer) to the 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


Configure Cache Maintenance

Under the Project Graph tab, click on System, and enable Use Cache Maintenance.

Configuration Options tab

Note: The above configuration is enabled to generate the cache maintenance API. The cache maintenance APIs are used in this application to address the cache coherency issues observed in transferring the temperature values to the serial terminal using DMA.

Back to Top