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
- 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:
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
Workflow
- Initialize system clock:
- Create USART options struct:
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
- Enable the clock for the USART module:
- Initialize in RS232 mode:
Usage steps
Example code
Add to application C-file:
Workflow
- Send an 'a' character via USART