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

Step 6: 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.

1

Under the main.c file, in the main() 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_code1.png

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.

2

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

ADC0_Enable();
app_code2.png

Note:

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

3

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_code3.png

4

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.

ADC0_ConversionStart();

SERCOM2_I2C_WriteRead(TEMP_SENSOR_SLAVE_ADDR, &i2cWrData, 1, i2cRdData, 2);
app_code4.png

5

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 */
    while(!ADC0_ConversionStatusGet())
    {

    };    

    adcResult = ADC0_ConversionResultGet();

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

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


Next Step >

© 2022 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.