STM32 有时候会不断进入中断,解决方法如下
1.串口初始化配置时,需要打开ORE 溢出中断,如下红色代码所示
void Usart_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 开启串口时钟
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 57600; // 配置波特率为115200
USART_InitStructure.USART_StopBits = USART_WordLength_8b; // 配置数据长度为8
USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置停止位
USART_InitStructure.USART_Parity = USART_Parity_No; // 配置奇偶校验为NONE
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 配置硬件流为NONE
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 打开Rx接收和Tx发送功能
USART_Init(USART1,&USART_InitStructure); // 配置
USART_Cmd(USART1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 选择中断通道
NVIC_InitStructure.NVIC_IRQChannelPriority = 2; // 抢断优先1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能中断
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 打开中断
USART_ITConfig(USART1, USART_IT_ORE, ENABLE); // 打开中断
}
void USART1_IRQHandler(void)
{
u8 dat;
if (USART_GetITStatus(USART1, USART_IT_ORE) == SET)
{
USART_ClearITPendingBit(USART1,USART_IT_ORE);
USART_ReceiveData( USART1 );
}
if( USART_GetITStatus(USART1,USART_IT_RXNE) != RESET ) // 等价于if( !RI ) 检查串口数据是否已就位
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
dat = USART_ReceiveData( USART1 );
uart_rec_buf[uart_len++]=dat;
RX_TIM=UART_INIT_TIM;
}
}
这样就可以解决,串口不断进入中断的问题