Step 4: Add Application Code to the Project
Last modified by Microchip on 2026/06/26 07:39
Add application code to the project.
Once the project is generated, add the following macros and variables outside the main function (in main.c) as shown in the image in the accompanying image.
#define RX_BUFFER_SIZE 256
char messageStart[] = "**** USART Polling mode Demo ****\r\n\
**** Entered line or character will be echoed back ****\r\n";
char newline[] = "\r\n";
char errorMessage[] = "\r\n**** USART error has occurred ****\r\n";
char receiveBuffer[RX_BUFFER_SIZE] = {};
static char data = 0;
char messageStart[] = "**** USART Polling mode Demo ****\r\n\
**** Entered line or character will be echoed back ****\r\n";
char newline[] = "\r\n";
char errorMessage[] = "\r\n**** USART error has occurred ****\r\n";
char receiveBuffer[RX_BUFFER_SIZE] = {};
static char data = 0;

The above code does the following:
- #define RX_BUFFER_SIZE 256 - Defines the size of the Universal Asynchronous Receiver Transmitter (UART) receive buffer as 256B.
- char messageStart[] = "**** USART Polling mode Demo ****\r\n\ **** Entered line or character will be echoed back ****\r\n"; - Stores the startup message to be sent to the terminal when the program begins.
- char newline[] = "\r\n"; - Stores the carriage return and line feed characters for line termination in serial communication.
- char errorMessage[] = "\r\n**** USART error has occurred ****\r\n"; - Stores the error message to be sent if a USART error is detected.
- char receiveBuffer[RX_BUFFER_SIZE] = {}; - Declares a buffer for storing received UART data, with a size defined by RX_BUFFER_SIZE.
- static char data = 0; - Declares a static variable to hold a single received character from UART.
Add the following code inside the main function outside while loop:
/* Send start message */
SERCOM3_USART_Write(&messageStart[0], sizeof(messageStart));
SERCOM3_USART_Write(&messageStart[0], sizeof(messageStart));
Inside while loop:
/* Check if there is a received character */
if(SERCOM3_USART_ReceiverIsReady() == true)
{
if(SERCOM3_USART_ErrorGet() == USART_ERROR_NONE)
{
SERCOM3_USART_Read(&data, 1);
// Echo the received character immediately
SERCOM3_USART_Write(&data, 1);
}
else
{
SERCOM3_USART_Write(&errorMessage[0], sizeof(errorMessage));
}
}
if(SERCOM3_USART_ReceiverIsReady() == true)
{
if(SERCOM3_USART_ErrorGet() == USART_ERROR_NONE)
{
SERCOM3_USART_Read(&data, 1);
// Echo the received character immediately
SERCOM3_USART_Write(&data, 1);
}
else
{
SERCOM3_USART_Write(&errorMessage[0], sizeof(errorMessage));
}
}

- SERCOM3_USART_Write(&messageStart[0], sizeof(messageStart)); - Sends the startup message to the UART terminal using the SERCOM3 USART peripheral.
- if(SERCOM3_USART_ReceiverIsReady() == true) - Checks if a character has been received and is ready to be read from the UART.
- if(SERCOM3_USART_ErrorGet() == USART_ERROR_NONE) - Checks if there are no USART errors before reading the received character.
- SERCOM3_USART_Read(&data, 1); - Reads one character from the UART and stores it in the variable data.
- SERCOM3_USART_Write(&data, 1); - Echoes the received character back to the UART terminal.
- SERCOM3_USART_Write(&errorMessage[0], sizeof(errorMessage)); - Sends an error message to the UART terminal if a USART error is detected.