Update and Configure an Existing MHC-based MPLAB® Harmony v3 Project to MCC-based Project: Step 5

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

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:


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

Application code

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.Application code


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 this image.

Application code


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);

Application code

Note:

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.

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


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

Application code

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;
    }
}

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();
    }
}

Application code


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

toggleLED_BasedOnSwitchPress();

You are now ready to build the code.

Application code