【STM32F1】——9轴姿态模块JY901与串口通信(下)

时间:2024-11-09 14:38:00
void UART1_Init(unsigned long baudrate) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART2时钟 //USART2_TX GPIOA.2初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO速率50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.2 //USART 初始化设置 USART_InitStructure.USART_BaudRate = baudrate; //串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式 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_Tx; //发模式 USART_Init(USART1, &USART_InitStructure); //初始化串口1 //中断开启设置 USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //开启检测串口空闲状态中断 USART_ClearFlag(USART1