板子硬件正常,因为使用USART_IT_IDLE中断时,串口正常工作。但是用到USART_IT_RXNE时,就一直进入中断,导致无法进入main的主循环。 由于没有JLINK或STLINK,无法判断到底是哪个引起的的中断。
下面是关于串口的配置代码:
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2; //USART1_TX usart2
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 | GPIO_Pin_3; //USART1_RX usart2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
it中:
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//如果寄存器中有数据
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
i = USART_ReceiveData(USART1);
USART_SendData(USART1,i);
}
|