MPLAB® Harmony v3 Peripheral Libraries on SAM D21: Step 5
Add Application Code to the Project
The application is already partially developed and is available in the main_d21.c file under <your unzip folder>/samd21_getting_started/dev_files/sam_d21_xpro. The main_d21.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 samd21_getting_started/dev_files/sam_d21_xpro folder and copy the pre-developed main_d21.c file.
- Replace the main_d21.c file of your project available at <Your project folder>/samd21_getting_started/firmware/src by over-writing it with the copied file.
- Open main_d21.c in MPLAB® X IDE and add the application code by following the steps below:
Under the main_d21.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_d21.c function, below SYS_Initialize(), add the following code to register callback event handlers.
DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, usartDmaChannelHandler, 0);
RTC_Timer32CallbackRegister(rtcEventHandler, 0);
EIC_CallbackRegister(EIC_PIN_15,EIC_User_Handler, 0);
Following the addition of the previous code, add the function call:
Implement the registered callback event handlers for RTC, I²C, Universal Synchronous Asynchronous Receiver Transmitter (USART), and EIC PLIBs by adding the following code before the main() function in main_d21.c.
{
changeTempSamplingRate = true;
}
static void rtcEventHandler (RTC_TIMER32_INT_MASK intCause, uintptr_t context)
{
if (intCause & RTC_MODE0_INTENSET_CMP0_Msk)
{
isRTCTimerExpired = true;
}
}
static void i2cEventHandler(uintptr_t contextHandle)
{
if (SERCOM2_I2C_ErrorGet() == SERCOM_I2C_ERROR_NONE)
{
isTemperatureRead = true;
}
}
static void usartDmaChannelHandler(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle)
{
if (event == DMAC_TRANSFER_EVENT_COMPLETE)
{
isUSARTTxComplete = true;
}
}
Add the following code to prepare the received temperature value from the sensor to be shown on the serial terminal.
sprintf((char*)uartTxBuffer, "Temperature = %02d F\r\n", temperatureVal);
LED_Toggle();
Add the following code to implement the change of sampling rate and prepare a message for the change on the serial terminal when the user presses the SW0 switch.
if(tempSampleRate == TEMP_SAMPLING_RATE_500MS)
{
tempSampleRate = TEMP_SAMPLING_RATE_1S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 1 second \r\n");
RTC_Timer32CompareSet(PERIOD_1S);
}
else if(tempSampleRate == TEMP_SAMPLING_RATE_1S)
{
tempSampleRate = TEMP_SAMPLING_RATE_2S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 2 seconds \r\n");
RTC_Timer32CompareSet(PERIOD_2S);
}
else if(tempSampleRate == TEMP_SAMPLING_RATE_2S)
{
tempSampleRate = TEMP_SAMPLING_RATE_4S;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 4 seconds \r\n");
RTC_Timer32CompareSet(PERIOD_4S);
}
else if(tempSampleRate == TEMP_SAMPLING_RATE_4S)
{
tempSampleRate = TEMP_SAMPLING_RATE_500MS;
sprintf((char*)uartTxBuffer, "Sampling Temperature every 500 ms \r\n");
RTC_Timer32CompareSet(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 USART using DMA.
You are now ready to build the code!