Microchip® Advanced Software Framework

USART receive character and echo back

In this use case, the USART 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

Example code

  1. The following configuration must be added to the project (typically to a conf_usart.h file, but it can also be added to your main application file.):
    #define USART_SERIAL &USARTD0
    #define USART_SERIAL_BAUDRATE 9600
    #define USART_SERIAL_CHAR_LENGTH USART_CHSIZE_8BIT_gc
    #define USART_SERIAL_PARITY USART_PMODE_DISABLED_gc
    #define USART_SERIAL_STOP_BIT false

A variable for the received byte must be added:

uint8_t received_byte;

Add to application initialization:

static usart_rs232_options_t USART_SERIAL_OPTIONS = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
usart_init_rs232(USART_SERIAL, &USART_SERIAL_OPTIONS);

Workflow

  1. Initialize system clock:
  2. Create USART options struct:
    • static usart_rs232_options_t USART_SERIAL_OPTIONS = {
      .baudrate = USART_SERIAL_BAUDRATE,
      .charlength = USART_SERIAL_CHAR_LENGTH,
      .paritytype = USART_SERIAL_PARITY,
      .stopbits = USART_SERIAL_STOP_BIT
      };
  3. Enable the clock for the USART module:
  4. Initialize in RS232 mode:

Usage steps

Example code

Add to, e.g., main loop in application C-file:

received_byte = usart_getchar(USART_SERIAL);
usart_putchar(USART_SERIAL, received_byte);

Workflow

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