串口初始化USART1_Init();
串口初始化USART1_Init();
//USART1_Init(); /* 串口初始化,用于调试 */
void USART1_Init(void)
{
//定义配置结构体
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//时钟源启动
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
//PA9配置
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);
//PA10配置,用了PA9的GPIO_InitStructure通用数据
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位8
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位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);//串口使能
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//串口中断使能
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//中断优先级组配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//中断请求通道选择
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10;//相应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断请求通道使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
USART1->SR;//读取SR寄存器
}
SR串口状态寄存器: