现在遇到一个奇怪的问题,STM32的串口接收中断进不去,接收标志位一直是RESET。但我用查询的方法是可以接收到数据的。Debug的时候,中断寄存器也打开了,我感觉配置是没有问题的,但一直找不到原因,请教大家。。代码如下(用的485协议)
1、初始化
[mw_shl_code=c,true]void USART_Configuration(unsigned long bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure );
//RS485 DIR位
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
USART_Cmd(USART2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}[/mw_shl_code]
2、主函数
[mw_shl_code=c,true] SYS_Configuration(); //系统配置
PHO_DISP(hangdian);//显示图形杭电Logo
delay_100ms(10);
PHO_DISP(biankuang_PC);//显示边框PC工作模式
display( 1, 0, " mA");
display( 2, 0, " M");
display( 3, 0, " M");
GPIO_ResetBits(GPIOA, GPIO_Pin_1);//拉低485接收数据
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //开启UART2中断,放在485接收初始化之后,防止系统开机时出现乱码
IWDG_Enable();
GPIO_SetBits(GPIOA, GPIO_Pin_1);//拉高485发送数据
printf("USart ok\r\n");
delay_ms(1);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);//拉低485接收数据
delay_ms(1000);
display(1,4,"0000");
while(1)
{
if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)
{
RxData = USART_ReceiveData(USART2);
GPIO_SetBits(GPIOA, GPIO_Pin_1);//拉高485发送数据
printf("%d", RxData);
delay_ms(1);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);//拉低485接收数据
delay_ms(1000);
}
}[/mw_shl_code]
3、中断函数
[mw_shl_code=c,true]void USART2_IRQHandler(void) //RS485接收中断
{
u8 j;
u16 RxData = 9999;
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
RxData = USART_ReceiveData(USART2);
GPIO_SetBits(GPIOA, GPIO_Pin_1);//拉高485发送数据
printf("%d", RxData);
delay_ms(1);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);//拉低485接收数据
delay_ms(1000);
}
}[/mw_shl_code]
|