4.1.7.5. USART
4.1.7.5.1. Introduction to USART
Universal Synchronous Asynchronous Receiver Transmitter (USART) is a communication protocol used for transmitting and receiving serial data. It’s commonly used to communicate with devices such as serial monitors, GPS modules, and other microcontrollers.
4.1.7.5.2. Key Configurations
- Baud Rate: Determines the speed of communication (e.g., 9600 bits per second). The baud rate is calculated as: 
- USART_DIV=SystemClockBaudRateUSART_DIV = frac{SystemClock}{BaudRate}USART_DIV=BaudRateSystemClock 
- This value is programmed into the BRR register. 
- Data Format: Includes settings for word length, stop bits, and parity. These are configured using the CR1 and CR2 registers: 
Word Length: 8 or 9 bits (configured via M bit in CR1).
Stop Bits: 1 or 2 stop bits (configured via STOP bits in CR2).
Parity: None, Even, or Odd (configured via PCE and PS bits in CR1).
- Control Registers: 
CR1: Enables USART (UE bit) and configures word length, parity, and transmitter/receiver.
CR2: Configures stop bits.
CR3: Optional features like hardware flow control.
4.1.7.5.3. Example
- Enable USART clock - Enable GPIO clock RCC_AHB1ENR and USART clock RCC_APB1ENR or RCC_APB2ENR. 
- Configure GPIO pins for USART - TX and RX pins are set to alternate function mode. 
- Set Baud rate - We calculate baud rate with the formula and set word length, parity and stop bits. 
- Enable USART - We set the UE bit in CR1. 
- Transmit data - The TXE bit is checked and data is written in USART_DR. 
- Receive data - RXNE is checked and the data is read from USART_DR