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.
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();
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.
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);
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);
}
You are now ready to build the code and observe the results!
Next Step >