Microchip® Advanced Software Framework

Quick start guide for USART module

This is the quick start guide for the USART module, with step-by-step instructions on how to configure and use the driver 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.

Basic use case - transmit a character

USART use cases

Basic use case - transmit a character

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

Setup steps

Prerequisites

  1. System Clock Management

Example code

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

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 application C-file:

usart_putchar(USART_SERIAL, 'a');

Workflow

  1. Send an 'a' character via USART