基础实验-UART串口通讯实验
给出关键函数
/************************************************************************\ * 函数名:USART1_Initialization * 函数功能概述:串口1初始化 * 参数说明:baud-波特率 * 返回值说明: \************************************************************************/ void USART1_Initialization(uint32_t baud) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);//使能时钟 //PA9-TXD 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); //PA10-RXD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; 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); USART_Cmd(USART1, ENABLE); } /************************************************************************\ * 函数名:USART1_SendByte * 函数功能概述:串口1发送字节 * 参数说明:data-数据 * 返回值说明: \************************************************************************/ void USART1_SendByte(uint8_t data) { USART_SendData(USART1, data); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} }
主要是配置好IO复用,USART的波特率,数据位,停止位等等,就可以了