3.2.1.3.1 Steps to Configure the Application Modules

Create separate VS Code projects for the AVR128DA48 and PIC32CM6408PL10 microcontrollers. Open MCC and perform the following steps to configure the application modules.

Step 1: Clock Configuration

Configure the system clock frequency to 24 MHz on the AVR128DA48 from the CLKCTRL module in the Project Resources panel. Refer to the following image:
Figure 3-4. Clock Configuration of AVR® DA
To configure the system clock frequency to 24 MHz on the PIC32CM6408PL10048, open the Clock configuration from the Plugins tab and make the selections, highlighted below.
Figure 3-5. Clock Configuration of PIC32CM PL10

Step 2: Peripheral Configuration

Step 2.a: I2C Configuration

In this application, the temperature sensor (MCP9808) is connected through the I2C interface. The Two-Wire Serial Interface (TWI) peripheral on the AVR128DA48 and the SERCOM I2C peripheral on the PIC32CM6408PL10048 are configured with the settings below to read temperature sensor data. The sensor data read through the I2C interface is processed in the application code and displayed as temperature in degree Celsius.

Add the I2C module to the Project Resources and configure it for a 100 kHz communication speed. Use Figure 3-6 and Figure 3-7 as references for configuring the I2C peripheral on the AVR128DA48 microcontroller.

TWI Peripheral in AVR® DA

Figure 3-6. I2C Host Driver
Figure 3-7. I2C Host PLIB

SERCOM I2C (SERCOM0) Peripheral in PIC32CM PL10

Add the SERCOM0 module to the project, configure it as I2C Master, and set the communication speed to 100 kHz. Use Figure 3-8 as a reference for configuring the I2C peripheral on the PIC32CM6408PL10048 microcontroller.

Figure 3-8. I2C Host Driver

Step 2.b: ADC Configuration

The Curiosity Nano Explorer board includes a rotary potentiometer connected to one of the analog input channels of the ADC. The ADC periodically samples and converts the potentiometer signal. As the user rotates the potentiometer, the corresponding output voltage is sensed and measured by the ADC peripheral. Both the AVR128DA48 and PIC32CM6408PL10048 feature a 12-bit ADC.

Add the ADC peripheral to the Project Resources. Then, configure the module as shown in Figure 3-9 for the AVR128DA48 microcontroller.

Figure 3-9. ADC Configuration in AVR® DA

Select VREF from Project Resource>Drivers and configure VDD as ADC Voltage Reference Source as shown in Figure 3-10.

Figure 3-10. VREF Configuration in AVR® DA

Similarly, for the PIC32CM6408PL10 microcontroller, add the ADC module to the Project Resources and configure it as shown in Figure 3-11.

Figure 3-11. ADC Configuration in PIC32CM PL10

Step 2.c: RTC Configuration

In this application, a Real-Time Counter (RTC) peripheral is used to periodically read the temperature sensor and the variable output voltage from the rotary potentiometer. By default, the RTC generates a compare interrupt every 500 ms and initiates the I2C transfer and an ADC conversion to read the temperature sensor and potentiometer values, respectively. With every switch press on the Curiosity Nano board, the RTC interrupt period changes to 1s, 2s, 4s, and then returns to the default 500 ms interval.

Add the RTC module to the Project Resources and configure it to generate a 500 ms interrupt, as shown in Figure 3-12 for the AVR128DA48 microcontroller.

Figure 3-12. RTC Configuration in AVR® DA

Similarly, for the PIC32CM6408PL10 microcontroller, add the RTC module to the Project Resources and configure it as shown in Figure 3-13.

Figure 3-13. RTC Configuration in PIC32CM PL10

Step 2.d: Configuring the Port Pin for Switch

The AVR® DA and PIC32CM-PL10 Curiosity Nano boards have on-board switches. The AVR® DA device detects switch presses using the interrupt capability on port pins, whereas the PIC32CM PL10 device uses an External Interrupt Controller (EIC) peripheral. The EIC allows external pins to be configured as interrupt lines and can generate interrupts on rising, falling or both edges. In this application, the EIC is configured to detect a falling edge on the pin connected to the on‑board switch. Refer to Figure 3-14 to configure PB03 as a switch input. Refer to Step 3: GPIO/Port Configuration to see how the port pin is configured on the AVR128DA48.

Figure 3-14. EIC Configuration in PIC32CM PL10

Step 2.e: Timer Configuration

Since the on‑board switch is used to change the RTC interrupt periodicity, the application must handle switch debouncing. To achieve this, a timer peripheral is used to generate a 50 ms overflow interrupt upon detecting a falling edge on the switch pin. The Timer/Counter Type A (TCA) peripheral is configured on the AVR128DA48, and the Timer/Counter0 (TC0) peripheral on the PIC32CM6408PL10048 to manage switch debouncing.

For the AVR128DA48 microcontroller, add the TCA0 peripheral to the Project Resources and configure it as shown in Figure 3-15.

Figure 3-15. TCA Configuration in AVR® DA

Similarly, for the PIC32CM6408PL10 microcontroller, add the TC0 peripheral to the Project Resources and configure it as shown in Figure 3-16.

Figure 3-16. TC0 Configuration in PIC32CM PL10

Step 2.f: UART Configuration

Configure the Universal Asynchronous Receiver and Transmitter (UART) peripheral to periodically print the temperature sensor and ADC results to the terminal. The AVR DA requires configuration of both the UART driver and PLIB instances.

For the AVR128DA48 microcontroller, add the UART peripheral to the Project Resources and configure it for a baud rate of 115200, as shown in Figure 3-17.

Figure 3-17. UART Driver Configuration in AVR® DA

Enable the USART Receiver and Transmitter interrupts from the UART PLIB configuration, as shown in Figure 3-18.

Figure 3-18. UART PLIB Configuration in AVR® DA

Similarly, for the PIC32CM6408PL10 microcontroller, add the SERCOM1 peripheral to the Project Resources and configure it for a baud rate of 115200, as shown in Figure 3-19.

Figure 3-19. UART Configuration in PIC32CM PL10

Add STDIO from Device Resources>Libraries>Harmony>Tools and connect its corresponding UART to the SERCOM1 UART, as shown below.

Figure 3-20. STDIO Configuration

Step 3: Port Configuration

After configuring the peripherals, configure the pin setting as shown below. On the AVR128DA48, the port pin connected to the on-board switch is configured through Pin settings to detect a falling edge and generate an interrupt.

For the AVR128DA48 microcontroller, open the Pin Grid View in MCC and configure the pin assignments according to Figure 3-21: USART1 (TXD - RC0), TWI0 (SDA - PC2, SCL - PC3), ADC0 (PD7), GPIO Input (PC7), and GPIO Output (PC6).

Figure 3-21. Pin Grid View in AVR® DA

For the AVR128DA48 microcontroller, open the Pins View in MCC and configure the switch interrupt on pin PC7 to detect a falling edge, as shown in Figure 3-22.

Figure 3-22. Pins View in AVR® DA
To configure the pins for the PIC32CM6408PL10048, open Pin Configurations from Plugins in MCC Harmony and select the settings shown in Figure 3-23. UART (SERCOM1_PAD0 - PB00, SERCOM1_PAD1-PB01), I2C (SERCOM0_PAD0 - PA00, SERCOM0_PAD1 - PA01), ADC (PA29), GPIO Input (PB03), and GPIO Output (PB02).
Figure 3-23. Pin Configurations in PIC32CM PL10

Step 4: Interrupt Settings

For the AVR128DA48 microcontroller, open the Interrupt Manager in MCC and toggle on Global Interrupt Enable.

Figure 3-24. Interrupt Manager Settings in AVR® DA

For the PIC32CM6408PL10 microcontroller, go to Project Resources>System>Interrupts (NVIC) and enable the RTC, EIC, SERCOM0, TC0 and ADC0 interrupts.

Figure 3-25. Interrupt Settings in PIC32CM PL10

Step 5: Generate Source Files

After configuring all peripherals, click the Generate button in the MCC window to generate the driver files. Refer to the Code Generation section for more details on MCC-generated files.

As the final step, add the application code to the AVR128DA48 and PIC32CM6408PL10048 microcontroller projects. The application code required for this example is available at the following link.