新手入门
- 积分
- 18
- 金钱
- 18
- 注册时间
- 2019-7-8
- 在线时间
- 9 小时
|
10金钱
我想开启UART3的接收中断,但是一直卡在中断服务函数里面出不来,请问中断服务函数中是否需要清空中断标志位?如果需要的话怎么清空?
还有,USART_IT_RXNE和USART_FLAG_RXNE有什么区别?应该访问哪一个来清除中断标志位?
这是我的代码:
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
//使能USART1,GPIOA,GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.10
//USART3_RX GPIOB.11初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.11
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART3, &USART_InitStructure); //初始化串口3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART3, ENABLE); //使能串口3
}
//以下为uart3中断服务函数
//定义标志变量来判断是否已经接收完毕
int flag_uart3;
//out传递读取到的星期几
u8 out;//[5];
//int pointer=0;
void USART3_IRQHandler(void) //串口3中断服务程序
{
u8 Res;
flag_uart3=0;
printf("time is going to be prepared\r\n");
// uart3_printf(ch1);
Res =USART_ReceiveData(USART3); //可能是没有跳出uart3的中断服务函数
printf("RXNE=%d\r\n",USART3->SR&0x0020);
//res[pointer]=Res;
printf("time %d is got\r\n",Res);
if (Res!='!')//表示接受次STM32时间数据完毕
{
out=Res;
//out[pointer]=Res;
//pointer=pointer+1;
printf("time %d is stored\r\n",Res);
flag_uart3=1;
printf("total time is ok\r\n");
}
else
{
printf("time %d is abandoned\r\n",Res);
//pointer=0;
//表示时间已经接收完毕
flag_uart3=1;
printf("total time is ok\r\n");
}
//不知为何,无法跳出USART3的中断
USART_GetITStatus(USART3,USART_IT_RXNE);
}
|
|