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

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

Configure I2C, UART, and TMR1 Peripheral Libraries

Configure TMR1 Peripheral Library

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

Adding Timer1

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

  • When a module is added to the project graph, the MPLAB® Code Configurator (MCC) automatically enables the clock to the module. The default TMR1 clock source is the internal peripheral 100 MHz clock (PBCLK3), shown in the accompanying image 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 in the accompanying image in red highlight.

Timer1 clock setup diagram

  • 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.

Timer1 clock setup

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.

Timer1 configuration setup

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

Back to Top


Configure I²C Peripheral Library and I²C pins

Under the Device Resources tab, expand Harmony > Peripherals > I²C

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

I2C selection

Select the I²C1 Peripheral Library and use the default configuration as shown in the accompanying image.

I2C setup

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 Project Graph > Plugins > Pin Configuration.

Open pin configuration

 

 Select the Pin Table tab and then scroll down to the I²C1 module as shown in the accompanying image.

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

I2C pins setup

I2C pins setup

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 Device Resources tab, expand Harmony Peripheral > UART.

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 in the accompanying image.

UART setup

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 the application buffer to the UART TX register.

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

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

UART pins setup

UART pins setup

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 Project Graph tab in MPLAB X IDE and then selecting Plugins > DMA Configuration.

Open DMA configuration

Click on the DMA Settings tab. Enable and configure DMA Channel 0 to transfer the application buffer to the UART TX register as shown in the accompanying image. 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.

UART DMA setup

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.

Cache maintenance

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