Getting Started with MPLAB® Harmony v3 Peripheral Libraries on PIC32MK GP MCUs: Step 5
Add Application Code to the Project
The application is already partially developed and is available in the main_pic32mk.c file under <your unzip folder>/pic32mk_getting_started/dev_files/pic32mk_gp_db.X. The bme280_driver.c file contains the driver code to read temperature from the BME280 sensor. The main_pic32mk.c file contains the application logic. It also contains placeholders that you will populate with necessary code in the next step.
- Go to the pic32mk_getting_started/dev_files/pic32mk_gp_db folder and copy the pre-developed main_pic32mk.c file.
- Replace the main_pic32mk.c file of your project available at <Your project folder>/pic32mk_getting_started/firmware/src by over-writing it with the copied file.
- Open main_pic32mk.c in MPLAB® X IDE and add application code by following the steps below:
Add the bme280_driver.c file into Source Files in the Projects tab.
- Go to the pic32mk_getting_started/dev_files/pic32mk_gp_db folder and copy the pre-developed bme280_driver.c, bme280_driver.h and bme280_definitions.h files.
- Paste these bme280_driver.c, bme280_driver.h and bme280_definitions.h files into your project source files, in the <Your project folder>/pic32mk_getting_started/firmware/src folder.
- In the Projects tab, right click on Source Files to add the existing pre-developed bme280_driver.c file.
- Select the bme280_driver.c file from the <Your project folder>/pic32mk_getting_started/firmware/src folder.
- Once selected, the bme280_driver.c file will be added into the project source files.
Under the main_pic32mk.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() function, below SYS_Initialize(), add the following code to register callback event handlers.
DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, UARTDmaChannelHandler, 0);
TMR2_CallbackRegister(tmr2EventHandler, 0);
GPIO_PinInterruptCallbackRegister(S1_PIN, S1_User_Handler, 0);
GPIO_PinInterruptEnable(S1_PIN);
Following the addition of the code given above, set the Transmit Interrupt Mode Selection bits. This is needed to work around a known erroneous behavior of UART with DMAC. This issue is described in an infobox under Step 10.
Following the addition of the code given above, add the function call to initialize the BME280 sensor.
After the addition of the code given above, add the function call.
Implement the registered callback event handlers for TMR2, SPI, UART, and GPIO PLIBs by adding the following code before the main() function in main_pic32mk.c.
{
if(S1_Get() == SWITCH_PRESSED_STATE)
{
changeTempSamplingRate = true;
}
}
static void tmr2EventHandler (uint32_t intCause, uintptr_t context)
{
isTmr2Expired = true;
}
static void SPIBufferEventHandler(uintptr_t context)
{
SENSOR_CS_Set();
BME280SensorData.isBufferCompleteEvent = true;
}
static void UARTDmaChannelHandler(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle)
{
if (event == DMAC_TRANSFER_EVENT_COMPLETE)
{
isUARTTxComplete = true;
}
}
Implement the BME280Sensor_Initialize() function after callback event handlers' functions in main_pic32mk.c.
{
BME280SensorData.temperature = 0;
BME280SensorData.slaveID = BME280_SPI_ADDRESS;
BME280SensorData.isBufferCompleteEvent = false;
/* Register with BME280 sensor */
BME280_RegisterDrvWriteReg(BME280Sensor_WriteReg);
BME280_RegisterDrvReadReg(BME280Sensor_ReadReg);
BME280_RegisterDrvRead(BME280Sensor_Read);
BME280_SoftReset();
/* 100 m.sec delay */
CORETIMER_DelayMs(100);
if (BME280_CHIP_ID != BME280_ID_Get())
{
while(1); /* Error Occurred */
}
BME280_CalibParams_Get();
BME280_SetOversampling(BME280_PARAM_TEMP, BME280_OVERSAMPLING_1X);
BME280_PowerMode_Set(BME280_NORMAL_MODE);
}
Implement the BME280Sensor_WriteReg(), BME280Sensor_ReadReg(), and BME280Sensor_Read() functions before the BME280Sensor_Initialize() function in main_pic32mk.c.
{
BME280SensorData.txBuffer[0] = wrAddr & 0x7F;
BME280SensorData.txBuffer[1] = wrData;
BME280SensorData.isBufferCompleteEvent = false;
SENSOR_CS_Clear();
SPI6_Write(BME280SensorData.txBuffer, 2);
while(false == BME280SensorData.isBufferCompleteEvent);
return true;
}
static uint8_t BME280Sensor_ReadReg(uint8_t rAddr)
{
BME280SensorData.txBuffer[0] = rAddr;
BME280SensorData.isBufferCompleteEvent = false;
SENSOR_CS_Clear();
SPI6_WriteRead(BME280SensorData.txBuffer, 1, BME280SensorData.rxBuffer, 2);
while(false == BME280SensorData.isBufferCompleteEvent);
return BME280SensorData.rxBuffer[1];
}
static bool BME280Sensor_Read(uint8_t rAddr, uint8_t* const pReadBuffer, uint8_t nBytes)
{
BME280SensorData.txBuffer[0] = rAddr;
BME280SensorData.isBufferCompleteEvent = false;
SENSOR_CS_Clear();
SPI6_WriteRead(BME280SensorData.txBuffer, 1, BME280SensorData.rxBuffer, (nBytes + 1));
while(false == BME280SensorData.isBufferCompleteEvent);
memcpy(pReadBuffer, &BME280SensorData.rxBuffer[1], nBytes);
return true;
}
Add the code given below to submit a temperature read transfer request to read temperature sensor value when the configured time period (defaulted to 500 milliseconds) has elapsed. It will read uncompensated temperature values from the sensor when the submitted request is complete.
BME280_ReadRawWeatherData();
Add the code given below to calculate compensated temperature value. The compensated temperature is in degrees Celsius (°C) and its resolution is 0.01°C.
Add the code given below to derive the Celcius to Fahrenheit (°C to °F) conversion (°F = (°C × 9/5) + 32).
Add the code below to prepare the received temperature value from the sensor to be shown on the serial terminal.
LED1_Toggle();
Add the code given below to implement the change of sampling rate and prepare a message for the change on the serial terminal when you press the S1 switch.
TMR2_Stop();
if(tempSampleRate == TEMP_SAMPLING_RATE_500MS)
{
tempSampleRate = TEMP_SAMPLING_RATE_1S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 1 second \r\n");
TMR2_PeriodSet(PERIOD_1S);
}
else if(tempSampleRate == TEMP_SAMPLING_RATE_1S)
{
tempSampleRate = TEMP_SAMPLING_RATE_2S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 2 seconds \r\n");
TMR2_PeriodSet(PERIOD_2S);
}
else if(tempSampleRate == TEMP_SAMPLING_RATE_2S)
{
tempSampleRate = TEMP_SAMPLING_RATE_4S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 4 seconds \r\n");
TMR2_PeriodSet(PERIOD_4S);
}
else if(tempSampleRate == TEMP_SAMPLING_RATE_4S)
{
tempSampleRate = TEMP_SAMPLING_RATE_500MS;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 500 ms \r\n");
TMR2_PeriodSet(PERIOD_500MS);
}
else
{
;
}
TMR2_Start();
Add the code given below to transfer the buffer containing either:
- The latest temperature value in the format “Temperature = XX F\r\n”, or
- The message mentioning the change of sampling rate over UART using DMA.
DCH0ECON |= _DCH0ECON_CFORCE_MASK;
You are now ready to build the code!