单片机的串口是没有任何连接的,但调试程序发现,主要串口发送数据,会一直进入串口1中断里,中断收到的数据很乱毫无规律,即使清空中断标志位也还会进入中断中,请问是什么原因?
void USART1_int ( void )
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 , ENABLE);
//串口1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;//Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//Rx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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_Rx |USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
//////////////////中断处理
void USART1_IRQHandler(void)
{
u8 rx_byte=0;
if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET) {
USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_FLAG_ORE);
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
|