STM32 不断进入串口中断问题 解决方法
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);</span>?? ??? ??? ??? ??? ?// 打開中斷
?
}
?
 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;
?? ?} ? ?
}
 ?
這樣就可以解決,串口不斷進入中斷的問題
?
?
 ?
總結
以上是生活随笔為你收集整理的STM32 不断进入串口中断问题 解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: STM32F系列USART的IDLE中断
 - 下一篇: JFlash ARM对stm32程序的读