Update and configure an existing MHC based MPLAB Harmony v3 project to MCC based project: Step 5

Step 5: Extend the application code in the project

The application is already developed (partially) and is available in the main_pic32mz.c file under <your_unzip_solution_folder>/getting_started_ext/dev_files/pic32mz_ef_curiosity_v2. The main_pic32mz.c file contains the application logic. It also contains placeholders that you will populate with the necessary code in the next step.

  • Go to the getting_started_ext/dev_files/pic32mz_ef_curiosity_v2 folder and copy the pre-developed main_pic32mz.c file.
  • Replace (over-write) the main_pic32mz.c file of your project available at <Your project folder>/pic32mzef_getting_started/firmware/src with the copied file.
  • Open main_pic32mz.c in MPLAB® X IDE and add the application code by following the steps below:

1

Under the main_pic32mz.c file, in function main, notice the call to function SYS_Initialize. The generated function SYS_Initialize initializes all the peripheral modules used in the application (configured through the MPLAB Harmony Configurator (MHC)).

Tip: Press the CTRL key and left click on the SYS_Initialize function. The click will open the implementation for the SYS_Initialize function as shown in Figure 1.

app_code1.png
Figure 1

Tip: Press the CTRL key and left click on the GPIO_Initialize function. The click will open the implementation for the GPIO_Initialize function. Notice MCC generated and added code for the SW3 and LED3 pins. The changes are shown in Figure 2.

app_gpio_code.png
Figure 2

Open the plib_gpio.h under Header Files > config > pic32mz_ef_curiosity_v2 > peripheral > gpio > plib_gpio.h and check the MCC-generated macros for LED3 and SW3 pins. The changes are shown in Figure 3.

app_gpio_h_code.png
Figure 3

2

In the main_pic32mz.c function, after SYS_Initialize(), add the following code to register callback SW3 event handler.

GPIO_PinInterruptCallbackRegister(SW3_PIN, SW3_User_Handler, 0);
GPIO_PinInterruptEnable(SW3_PIN);
app_code2.png
Figure 4

Note:

a

The function call GPIO_PinInterruptCallbackRegister registers a General Purpose Input/Output (GPIO) callback event handler with the GPIO PLIB. The callback event handler is called by the GPIO PLIB when the user presses the switch SW3.

b

The function call GPIO_PinInterruptEnable enables the GPIO interrupt on a SW3 pin.

3

Replace the SW1 registered callback event handler and add the SW3 registered event handler by adding the following code before the main() function in main_pic32mz.c

static void SW1_User_Handler(GPIO_PIN pin, uintptr_t context)
{
    if(SW1_Get() == SWITCH_PRESSED_STATE)
    {
        currSwitchPressedState = SWITCH_1;

        if (prevSwitchPressedState == currSwitchPressedState)
        {
            changeTempSamplingRate = true;
        }
        prevSwitchPressedState = SWITCH_1;
    }
}

static void SW3_User_Handler(GPIO_PIN pin, uintptr_t context)
{
    if(SW3_Get() == SWITCH_PRESSED_STATE)
    {
        currSwitchPressedState = SWITCH_3;

        if (prevSwitchPressedState == currSwitchPressedState)
        {
            changeTempSamplingRate = true;
        }
        prevSwitchPressedState = SWITCH_3;
    }
}
app_code3.png
Figure 5

4

Add the below code to toggle the LED based on the Switch press event before the main() function.

static void toggleLED_BasedOnSwitchPress(void)
{
    if (currSwitchPressedState == SWITCH_1)
    {
        LED3_Set();
        LED1_Toggle();
    }
    else
    {
        LED1_Set();
        LED3_Toggle();
    }
}
app_code5.png
Figure 6

5

Replace the code LED1_Toggle(); with the below code to toggle the LED based on the Switch press event.

toggleLED_BasedOnSwitchPress();
app_code6.png
Figure 7

You are now ready to build the code.


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.