static void ConfigUART(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO_InitStructure用于存放GPIO的参数
USART_InitTypeDef USART_InitStructure; //USART_InitStructure用于存放USART的参数
NVIC_InitTypeDef NVIC_InitStructure; //NVIC_InitStructure用于存放NVIC的参数
//使能RCC相关时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIOA的时钟
//配置TX的GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //设置TX的引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //设置TX的模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置TX的I/O口输出速度
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //设置TX的输出类型为推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //设置TX为上拉模式
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据参数初始化TX的GPIO
//配置RX的GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //设置RX的引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //设置RX的模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //设置RX输出类型为推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //设置RX为上拉模式
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据参数初始化RX的GPIO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); //将引脚PA10连接到USART1_RX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //将引脚PA9连接到USART1_TX
//配置USART的参数
USART_StructInit(&USART_InitStructure); //初始化USART_InitStructure
USART_InitStructure.USART_BaudRate = bound; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置数据字长度
USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //设置奇偶校验位
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //设置模式
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //设置硬件流控制模式
USART_Init(USART1, &USART_InitStructure); //根据参数初始化USART1
//配置NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断通道号
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //设置抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //设置子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure); //根据参数初始化NVIC
//使能USART1及其中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收缓冲区非空中断
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //使能发送缓冲区空中断
USART_Cmd(USART1, ENABLE); //使能USART1
}