1 #ifndef __BSP_UART_H 2 #define __BSP_UART_H 3 #include "main.h" 4 5 void User_Uart1_Init(void); 6 7 #endif
1 #include "bsp_uart.h" 2 3 UART_HandleTypeDef huart1; 4 5 void User_Uart1_Init(void) 6 { 7 GPIO_InitTypeDef GPIO_Instructure; 8 9 __HAL_RCC_GPIOA_CLK_ENABLE(); 10 __HAL_RCC_USART1_CLK_ENABLE(); 11 12 GPIO_Instructure.Pin = GPIO_PIN_9; //TX 13 GPIO_Instructure.Pull = GPIO_PULLUP; 14 GPIO_Instructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; //50-200MHz 15 GPIO_Instructure.Mode = GPIO_MODE_AF_PP; 16 GPIO_Instructure.Alternate = GPIO_AF7_USART1; 17 HAL_GPIO_Init(GPIOA,&GPIO_Instructure); 18 19 GPIO_Instructure.Pin = GPIO_PIN_10; //RX 20 GPIO_Instructure.Pull = GPIO_NOPULL; 21 GPIO_Instructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; //50-200MHz 22 GPIO_Instructure.Mode = GPIO_MODE_INPUT; 23 GPIO_Instructure.Alternate = GPIO_AF7_USART1; 24 HAL_GPIO_Init(GPIOA,&GPIO_Instructure); 25 26 huart1.Instance = USART1; 27 huart1.Init.BaudRate = 115200; 28 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; 29 huart1.Init.Mode = UART_MODE_TX_RX; 30 huart1.Init.OverSampling = UART_OVERSAMPLING_16; 31 huart1.Init.Parity = UART_PARITY_NONE; 32 huart1.Init.StopBits = UART_STOPBITS_1; 33 huart1.Init.WordLength = UART_WORDLENGTH_8B; 34 HAL_UART_Init(&huart1); 35 36 HAL_NVIC_SetPriority(USART1_IRQn,0,0); 37 HAL_NVIC_EnableIRQ(USART1_IRQn); 38 } 39 int fputc(int ch,FILE *f) 40 { 41 HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF); 42 while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TXE) != SET); 43 return ch; 44 }
原文:https://www.cnblogs.com/pathfinder-world/p/14587323.html