STM32F105 Serial Port Configuration Method
The STM32F105 microcontroller features up to 5 USART peripherals (USART1–USART5) for serial communication. These USART peripherals can be configured for both asynchronous (UART) and synchronous communication. Below is a step-by-step guide to configure the serial ports on the STM32F105:
1. Clock Configuration
The USART peripherals are connected to APB1 and APB2 buses:
- USART1: Connected to APB2 bus (max clock: 72 MHz)
- USART2–USART5: Connected to APB1 bus (max clock: 36 MHz)
Steps to Enable the Clock:
- Enable the peripheral clock for USART and GPIO ports in the RCC (Reset and Clock Control) register.
2. GPIO Configuration
Each USART peripheral has dedicated Tx (Transmit) and Rx (Receive) pins, which need to be configured as Alternate Function Push-Pull (for Tx) and Floating Input (for Rx).
USART | Tx Pin | Rx Pin |
---|---|---|
USART1 | PA9 | PA10 |
USART2 | PA2 | PA3 |
USART3 | PB10 | PB11 |
UART4 | PC10 | PC11 |
UART5 | PC12 | PD2 |
GPIO Initialization Example for USART1:
3. USART Peripheral Configuration
The USART peripheral must be initialized with the following parameters:
- Baud Rate: Communication speed (e.g., 9600 or 115200 bps).
- Word Length: 8-bit or 9-bit data.
- Stop Bits: Number of stop bits (1, 1.5, or 2).
- Parity: None, Even, or Odd parity.
- Mode: Transmit (Tx), Receive (Rx), or both.
USART Initialization Example for USART1:
4. Data Transmission and Reception
Once USART is initialized, you can send and receive data using the USART_SendData and USART_ReceiveData functions.
Data Transmission Example:
Data Reception Example:
5. Interrupt Configuration (Optional)
If you want to enable interrupts for USART (e.g., for receiving data), configure the NVIC (Nested Vectored Interrupt Controller) and enable the USART interrupt.
USART Interrupt Example:
6. Complete USART Configuration Flow
- Enable the clock for USART and GPIO.
- Configure GPIO pins for USART Tx and Rx.
- Initialize the USART peripheral with desired settings.
- Enable USART.
- Implement data transmission and reception functions.
Summary
The serial port (USART) configuration in STM32F105 involves clock enabling, GPIO setup, USART peripheral initialization, and optional interrupt handling. It follows a similar procedure to the STM32F103 but includes considerations for additional peripherals like USB OTG and CAN.
评论
发表评论