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.
c

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // Enable clock for USART1 and GPIOA RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Enable clock for USART2

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).

USARTTx PinRx Pin
USART1PA9PA10
USART2PA2PA3
USART3PB10PB11
UART4PC10PC11
UART5PC12PD2

GPIO Initialization Example for USART1:

c

GPIO_InitTypeDef GPIO_InitStructure; // Configure USART1 Tx (PA9) as alternate function push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART1 Rx (PA10) as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);

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:

c

USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; // Set baud rate USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8-bit data USART_InitStructure.USART_StopBits = USART_StopBits_1; // 1 stop bit USART_InitStructure.USART_Parity = USART_Parity_No; // No parity USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // Enable Tx and Rx USART_Init(USART1, &USART_InitStructure); // Enable USART1 USART_Cmd(USART1, ENABLE);

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:

c

void USART1_SendChar(char c) { while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait until TX is empty USART_SendData(USART1, c); // Send data }

Data Reception Example:

c

char USART1_ReceiveChar(void) { while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait until data is received return USART_ReceiveData(USART1); // Read received data }

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:

c

NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // Enable USART Receive interrupt USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

6. Complete USART Configuration Flow

  1. Enable the clock for USART and GPIO.
  2. Configure GPIO pins for USART Tx and Rx.
  3. Initialize the USART peripheral with desired settings.
  4. Enable USART.
  5. 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.

评论

此博客中的热门博文

How to interface CPLD with microcontroller?

How to Make an Alarm System on an FPGA?

The difference between Microcontrollers and Microprocessors