MPLAB® Harmony v3 Peripheral Libraries on PIC32MZ EF: Step 5
Add Application Code to the Project
The application is already developed (partially) and is available in the main_pic32mz.c file under <your unzip folder>/pic32mzef_getting_started/dev_files/pic32mz_ef_curiosity_v2. The main_pic32mz.c file contains the application logic. It also contains placeholders that you will populate with the necessary code in the next step.
- Go to the pic32mzef_getting_started/dev_files/pic32mz_ef_curiosity_v2 folder and copy the pre-developed main_pic32mz.c file.
- Replace (over-write) the main_pic32mz.c file of your project available at <Your project folder>/pic32mzef_getting_started/firmware/src with the copied file.
- Open main_pic32mz.c in MPLAB® X IDE and add the application code by following the steps below:
Under the main_pic32mz.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 (configured through the MPLAB Harmony Configurator (MHC)).
In the main_pic32mz.c function, below SYS_Initialize(), add the following code to register callback event handlers.
DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, UARTDmaChannelHandler, 0);
TMR1_CallbackRegister(tmr1EventHandler, 0);
GPIO_PinInterruptCallbackRegister(SW1_PIN, SW1_User_Handler, 0);
GPIO_PinInterruptEnable(SW1_PIN);
Following the addition of the code above, add the function call.
Implement the registered callback event handlers for TMR1, I²C, Universal Asynchronous Receiver Transmitter (UART), and GPIO PLIBs by adding the following code before the main() function in main_pic32mz.c.
{
if(SW1_Get() == SWITCH_PRESSED_STATE)
{
changeTempSamplingRate = true;
}
}
static void tmr1EventHandler (uint32_t intCause, uintptr_t context)
{
isTmr1Expired = true;
}
static void i2cEventHandler(uintptr_t contextHandle)
{
if (I2C1_ErrorGet() == I2C_ERROR_NONE)
{
isTemperatureRead = true;
}
}
static void UARTDmaChannelHandler(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle)
{
if (event == DMAC_TRANSFER_EVENT_COMPLETE)
{
isUARTTxComplete = true;
}
}
Add the code below to submit an I²C transfer request to read the temperature sensor value when the configured time period (default 500 milliseconds) has elapsed. The I²C PLIB calls back the callback event handler (registered in Step 2) when the submitted request is complete.
I2C1_WriteRead(TEMP_SENSOR_SLAVE_ADDR, &i2cWrData, 1, i2cRdData, 2);
Add the code below to prepare the received temperature value from the sensor to be printed on the serial terminal.
sprintf((char*)uartTxBuffer, "Temperature = %02d F\r\n", temperatureVal);
LED1_Toggle();
Add the code below to implement the change of sampling rate and prepare a message for the same on the serial terminal when the user presses the switch SW1.
if(tempSampleRate == TEMP_SAMPLING_RATE_500MS)
{
tempSampleRate = TEMP_SAMPLING_RATE_1S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 1 second \r\n");
TMR1_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");
TMR1_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");
TMR1_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");
TMR1_PeriodSet(PERIOD_500MS);
}
else
{
;
}
Add code 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.
DCACHE_CLEAN_BY_ADDR((uint32_t)uartTxBuffer, sizeof(uartTxBuffer));
DMAC_ChannelTransfer(DMAC_CHANNEL_0, (const void *)uartTxBuffer, strlen((const char*)uartTxBuffer), (const void *)&U6TXREG, 1, 1);