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.
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.
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.
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);
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;
}
}
You are now ready to build the code.
Next Step >