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
- 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
A variable for the received byte must be added:
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, e.g., main loop in application C-file:
Workflow
- Wait for reception of a character:
- Echo the character back: