MPLAB® Harmony v3 Peripheral Libraries on PIC32CM GV-VL (Arm® Cortex®-M0+) MCUs: Step 5

Last modified by Microchip on 2025/10/03 11:32

Note: This video provides an overview of how to add application code logic and build the application.

The application is already partially developed and is available in the main.c file under <your unzip folder>/getting_started_pic32cm_gv_vl_cnano. The main.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 pic32cm_gv_vl_cnano_getting_started.zip\pic32cm_gv_vl_cnano_getting_started\firmware\src folder and copy the pre-developed main.c file.
  • Replace the main.c file of your project available at <Your project folder>/pic32cm_gv_vl_getting_started/firmware/src by overwriting it with the copied file.
  • Otherwise, open main.c in MPLAB® X IDE and add the application code by following these steps:

Under the main.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, which is configured through MPLAB Code Configurator (MCC).

Information

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

implementation for the SYS_Initialize function

Note: The NVMCTRL_Initialize, PM_Initialize, and EVSYS_Initialize are system-specific initialization functions. MCC adds these modules by default to the project graph and generates code. These modules will be initialized to your configurations if you explicitly configure them.

Back to Top


In the main.c file, macros should be defined, and all required declarations and initializations should be positioned after the header file inclusions to be utilized in this application code.

#define PERIOD_500MS                            512
#define PERIOD_1S                               1024
#define PERIOD_2S                               2048
#define PERIOD_4S                               4096

typedef enum
{
    LED_SAMPLING_RATE_500MS = 0,
    LED_SAMPLING_RATE_1S = 1,
    LED_SAMPLING_RATE_2S = 2,
    LED_SAMPLING_RATE_4S = 3,
} LED_SAMPLING_RATE;

static LED_SAMPLING_RATE ledSampleRate = LED_SAMPLING_RATE_500MS;
static const char timeouts[4][20] = {"500 milliSeconds", "1 Second",  "2 Seconds",  "4 Seconds"};

Following the addition of the previous code, add the flags that will be used in the application code:

static volatile bool isRTCExpired = false;
static volatile bool changeLedSamplingRate = false;

macros defined and flags added

Information

Note: Here the definition, declarations, and initializations are based on the LED toggling rate time period.​​​​​

Back to Top


In the main.c function, below SYS_Initialize(), add the following code to register callback event handlers.

EIC_CallbackRegister(EIC_PIN_1, EIC_User_Handler, 0);
RTC_Timer32CallbackRegister(rtcEventHandler, 0);

Add the following code to print the toggling rate at 500 milliseconds.

printf("Toggling LED at 500 milliseconds rate \r\n");

Following the addition of the previous code, add the function call to start the RTC timer:

RTC_Timer32Start();

add the function call to start the RTC timer

Information

Note:

  • ​​​​​​The function call RTC_Timer32CallbackRegister registers a Real-Time Clock (RTC) callback event handler with the RTC Peripheral Library (PLIB). The callback event handler is called by the RTC PLIB when the configured time period has elapsed.
  • The function call EIC_CallbackRegister registers an External Interrupt Controller (EIC) callback event handler with the EIC PLIB. The callback event handler is called by EIC PLIB when the SW200 switch is pressed.

Back to Top


Implement the registered callback event handlers for RTC and EIC PLIBs by adding the following code before the main() function in main.c.

static void EIC_User_Handler(uintptr_t context)
{
    changeLedSamplingRate = true;
}

static void rtcEventHandler (RTC_TIMER32_INT_MASK intCause, uintptr_t context)
{
   if (intCause & RTC_MODE0_INTENSET_CMP0_Msk)
    {
        isRTCExpired    = true;
    }
}

static void EIC_User_Handler(uintptr_t context)

Back to Top


Add the following code at the beginning of the while(1) loop to toggle the LED and print the LED toggling rate on the serial terminal.

LED_Toggle();            
printf("Toggling LED at %s rate \r\n", &timeouts[(uint8_t)ledSampleRate][0]);

Code to toggle the LED and print the LED toggling rate

Back to Top


Add the following code after the previous step to implement the change of sampling rate and display a message on the serial terminal when the SW200 switch is pressed.

if(changeLedSamplingRate == true)
        {
            changeLedSamplingRate = false;
           if(ledSampleRate == LED_SAMPLING_RATE_500MS)
            {
                ledSampleRate = LED_SAMPLING_RATE_1S;
                RTC_Timer32CompareSet(PERIOD_1S);
            }
           else if(ledSampleRate == LED_SAMPLING_RATE_1S)
            {
                ledSampleRate = LED_SAMPLING_RATE_2S;
                RTC_Timer32CompareSet(PERIOD_2S);
            }
           else if(ledSampleRate == LED_SAMPLING_RATE_2S)
            {
                ledSampleRate = LED_SAMPLING_RATE_4S;
                RTC_Timer32CompareSet(PERIOD_4S);
            }
           else if(ledSampleRate == LED_SAMPLING_RATE_4S)
            {
               ledSampleRate = LED_SAMPLING_RATE_500MS;
               RTC_Timer32CompareSet(PERIOD_500MS);
            }
           else
            {
                ;
            }

if(changeLedSamplingRate == true)

Back to Top


Add the code after the previous step to print the new periodic value whenever ledSampleRate is changed.

printf("LED Toggling rate is changed to %s\r\n", &timeouts[(uint8_t)ledSampleRate][0]);

ledSampleRate is changed

 

Back to Top


Build the project by clicking the drop-down arrow next to the Clean and Build icon, then select Clean and Build Main Project. Verify that the project builds successfully.

Clean and Build Main Project selection

At this point, you are ready to start implementing your application on the hardware.

Back to Top