Microchip® Advanced Software Framework

Quick start guide for Serial Interface service

This is the quick start guide for the Serial Interface module, with step-by-step instructions on how to configure and use the serial in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Serial use cases

Basic use case - transmit a character

In this use case, the serial module is configured for:

  • Using USARTD0
  • Baudrate: 9600
  • Character length: 8 bit
  • Parity mode: Disabled
  • Stop bit: None
  • RS232 mode

The use case waits for a received character on the configured USART and echoes the character back to the same USART.

Setup steps

Prerequisites

  1. System Clock Management (sysclk)

Example code

The following configuration must be added to the project (typically to a conf_uart_serial.h file, but it can also be added to your main application file.)

Note
The following takes SAM3X configuration for example, other devices have similar configuration, but their parameters may be different, refer to corresponding header files.
#define USART_SERIAL &USARTD0
#define USART_SERIAL_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT false

A variable for the received byte must be added:

uint8_t received_byte;

Add to application initialization:

.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
usart_serial_init(USART_SERIAL, &usart_options);

Workflow

  1. Initialize system clock:
  2. Create serial USART options struct:
    • static usart_serial_options_t usart_options = {
      .baudrate = USART_SERIAL_BAUDRATE,
      .charlength = USART_SERIAL_CHAR_LENGTH,
      .paritytype = USART_SERIAL_PARITY,
      .stopbits = USART_SERIAL_STOP_BIT
      };
  3. Initialize the serial service:

Usage steps

Example code

Add to application C-file:

usart_serial_getchar(USART_SERIAL, &received_byte);
usart_serial_putchar(USART_SERIAL, received_byte);

Workflow

  1. Wait for reception of a character:
  2. Echo the character back: