Migrating ASF SAM C21 Application to MPLAB® Harmony v3 PIC32CM MC: Step 5
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.
Procedure
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).
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).
ADC0_Enable();
Implement the registered callback interrupt handlers for I²C PLIB by adding the following code before the int main (void) function in main.c.
{
if (SERCOM2_I2C_ErrorGet() == SERCOM_I2C_ERROR_NONE)
{
isTemperatureRead = true;
}
}
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);
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.
{
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);
}
You are now ready to build the code and observe the results!