UART_HandleTypeDef Uart1Handle; void HAL_UART1_Port_Init() { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks ##*/ /* Enable GPIO TX/RX clock */ __HAL_RCC_GPIOB_CLK_ENABLE(); // PB6_USART1_TXD, PB7_USART1_RXD /* Enable USARTx clock */ __HAL_RCC_USART1_FORCE_RESET(); // USART1強迫重置 __HAL_RCC_USART1_RELEASE_RESET(); // USART1重置結束 __HAL_RCC_USART1_CLK_ENABLE(); // 啟用USART1的Clock /* Enable Alternative IO Clock AFIO AFIO Remap USART 1 Enable the remapping of USART1 alternate function TX and RX. ENABLE: Remap (TX/PB6, RX/PB7) */ __HAL_RCC_AFIO_CLK_ENABLE(); // 致能 Alternative IO Clock __HAL_AFIO_REMAP_USART1_ENABLE(); // 設定將USART1 Remap IO /*##-2- Configure peripheral GPIO ##*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); }