MPLAB® Harmony v3 Peripheral Libraries on PIC32CM GV-VL (Arm® Cortex®-M0+) MCUs: Step 5
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).
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_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 changeLedSamplingRate = false;
In the main.c function, below SYS_Initialize(), add the following code to register callback event handlers.
RTC_Timer32CallbackRegister(rtcEventHandler, 0);
Add the following code to print the toggling rate at 500 milliseconds.
Following the addition of the previous code, add the function call to start the RTC timer:
Implement the registered callback event handlers for RTC and EIC PLIBs by adding the following code before the main() function in main.c.
{
changeLedSamplingRate = true;
}
static void rtcEventHandler (RTC_TIMER32_INT_MASK intCause, uintptr_t context)
{
if (intCause & RTC_MODE0_INTENSET_CMP0_Msk)
{
isRTCExpired = true;
}
}
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.
printf("Toggling LED at %s rate \r\n", &timeouts[(uint8_t)ledSampleRate][0]);
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.
{
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
{
;
}
Add the code after the previous step to print the new periodic value whenever ledSampleRate is changed.
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.
At this point, you are ready to start implementing your application on the hardware.