Migrating ASF SAM C21 Application to MPLAB® Harmony v3 PIC32CM MC: Step 5

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

Add Application Code to the Project

The application is already partially developed and is available in the main.c file under <your unzip folder>/pic32cm_mc_curiosity_getting_started/dev_files/pic32cm_mc00_curiosity_pro. The main.c file contains the application logic. It also contains placeholders that you will populate with necessary code in the next step.


Go to the pic32cm_mc_curiosity_getting_started/dev_files/pic32cm_mc00_curiosity_pro folder and copy the pre-developed main.c file.

Replace (over-write) the main.c file of your project available at <Your project folder>/pic32cm_mc_getting_started/firmware/src with the copied file.

Open main.c in MPLAB® X IDE and add the application code by following the next steps.

Under the main.c file, in the int main(void) function, notice the call to the SYS_Initialize function.

The generated SYS_Initialize function initializes all the peripheral modules used in the application, configured through 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 the following image.

app code void

Note: The NVMCTRL_Initialize and EVSYS_Initialize are system-specific initialization functions necessary to run the device. MHC adds these modules by default to the project graph and generates code. These modules will be initialized to user configurations if the user configures them explicitly.

Back to Top

In the int main (void) function, below the SYS_Initialize() function call, add the following code to register callback event handler, enable the Analog-to-Digital Converter (ADC).

SERCOM2_I2C_CallbackRegister(i2cEventHandler, 0);


app code int main​​​


  • The SERCOM2_I2C_CallbackRegister function call registers a callback interrupt handler with the I²C PLIB. The interrupt handler is called by the I²C PLIB when the I²C reads the temperature value from the temperature sensor.
  • The ADC_Enable function call enables the ADC module. The ADC is used to sample the light sensor when a trigger is received from the software.

Back to Top

Implement the registered callback interrupt handlers for I²C PLIB by adding the following code before the int main (void) function in main.c

static void i2cEventHandler(uintptr_t contextHandle)
   if (SERCOM2_I2C_ErrorGet() == SERCOM_I2C_ERROR_NONE)
       isTemperatureRead = true;

app code static void​​​

Back to Top

Inside the while loop, start the ADC conversion and submit an I²C transfer to read the temperature sensor value.

When the submitted request is completed, the i2cEventHandler callback function declared above is called.


SERCOM2_I2C_WriteRead(TEMP_SENSOR_SLAVE_ADDR, &i2cWrData, 1, i2cRdData, 2);

app code while​​​

Back to Top

Inside the while loop, add the following code to unset the isTemperatureRead flag, the temperature read will be converted into degrees Fahrenheit format for display.

The ADC polls whether the conversion is complete. Once ADC conversion is completed, the ADC result will be read and printed on the console with the temperature continuously.

if (isTemperatureRead == true)
   isTemperatureRead = false;

   temperatureVal = getTemperature(i2cRdData);
         /* Wait till ADC conversion result is available */


   adcResult = ADC0_ConversionResultGet();

   printf("Temperature = %02d F  Light sensor = %0d \r\n", temperatureVal, adcResult);

app code if​​​

You are now ready to build the code and observe the results!

Back to Top