项目中使用GD32F103RB单片机,用到USART1、USART2、USART3,其中USART1和USART3都是通的,但是USART2怎么都不通,直接抓取Tx引脚也没无波形,开始怀疑代码问题,反复看了好多遍,找不出问题,先附上代码。
void Foundation_Uart2_Init(unsigned int uart_baud)
{
Foundation_Uart2_Nvic_Init(); //初始化串口2中断优先级配置
Foundation_Uart2_Gpio_Init(); //初始化串口2引脚配置
Foundation_Uart2_Perpheral_Init(uart_baud);//初始化串口2功能配置
}
void Foundation_Uart2_Nvic_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //先占优先级0,从优先级0-16
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}
void Foundation_Uart2_Gpio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);//使能USART2和GPIOA外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART2_TXD(PA.2)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART2_TXD(PA.2)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; //USART2_RXD(PA.3)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART2_RXD(PA.3)
}
void Foundation_Uart2_Perpheral_Init(unsigned int uart_baud)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
USART_InitStructure.USART_BaudRate = uart_baud; //设置波特率
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(USART2, &USART_InitStructure); //初始化串口2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能串口2接收中断
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //使能串口2空闲中断
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
主循环一直在通过USART2发送数据,但是Tx无波形,用的是ST的标准库,程序中也反复查看,没有该引脚被配置成其它外设的情况。 |