/** * @brief USART1 GPIO 配置,工作模式配置。115200 8-N-1 * @param 无 * @retval 无 */ void USART1_Config(uint32_t BaudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* 使能串口2接收中断 */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ClearFlag(USART1,USART_FLAG_TC);//先清除一下发送中断标志位,会解决第一个字节丢失的问题。 USART_Cmd(USART1, ENABLE); USART1_NVIC_Configuration(ENABLE); USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); }
static void USART1_Configuration(void) { // Periph CLK Init RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // GPIO Pins for UART1 USART1_GPIO_Configuration(); // NVIC for UART1 USART1_NVIC_Configuration(); // --------------- USART1 configuration --------------- // USART1 configured as follow: USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Configure the USART1 USART_Init(USART1, &USART_InitStructure); // Enable the USART Transmoit interrupt: this interrupt is generated when the // USART1 transmit data register is empty // USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // Enable the USART Receive interrupt: this interrupt is generated when the // USART1 receive data register is not empty USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable USART1 USART_Cmd(USART1, ENABLE); }
void USART1_Config(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); USART_ClockInitTypeDef USART_ClockInitStructure; GPIO_InitTypeDef UARTTX; UARTTX.GPIO_Pin = GPIO_Pin_9; UARTTX.GPIO_Mode = GPIO_Mode_AF_PP; UARTTX.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitTypeDef UARTRX; UARTRX.GPIO_Pin = GPIO_Pin_10; UARTRX.GPIO_Mode = GPIO_Mode_IPU; UARTRX.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &UARTTX); GPIO_Init(GPIOA, &UARTRX); USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART1, &USART_ClockInitStructure); USART_InitTypeDef UART1; UART1.USART_BaudRate = 9600; UART1.USART_WordLength = USART_WordLength_8b; UART1.USART_StopBits = USART_StopBits_1; UART1.USART_Parity = USART_Parity_No; UART1.USART_HardwareFlowControl = USART_HardwareFlowControl_None; UART1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &UART1); USART_Cmd(USART1, ENABLE); USART1_NVIC_Configuration(); }