多个中断在一起怎么配置啊!

2019-07-21 01:12发布

串口中断、外部中断1.外部中断2.还有定时器中断,一个有四个中断,我不知道是不是后面的配置把前面的配置给覆盖了,就是没有外部中断,但是我如拿出单个实现,外部中断又可行。 [mw_shl_code=c,true]void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* 配置中断使用组合1*/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* USART*/ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*TIM2*/ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //选择中断通道2 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占式中断优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应式中断优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能中断 NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //选择中断通道3 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占式中断优先级设置为1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应式中断优先级设置为1 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能中断 NVIC_Init(&NVIC_InitStructure);[/mw_shl_code] 这是中断向量配置
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
15条回答
翱翔云端的鸟
2019-07-21 01:13
把我的多个中断NVIC配置代码贴上来,绝对原创,自我感觉好不错,使用条件编译,且分到同一组中,简直NICE! [mw_shl_code=c,true]void PA_NVIC_Configuration(NVIC_InitTypeDef NVIC_InitStructure1) { NVIC_InitStructure1.NVIC_IRQChannel = EXTI1_IRQn; NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure1); } void TIM3_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure2) { NVIC_InitStructure2.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure2); } void TIM2_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure3) { NVIC_InitStructure3.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure3.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure3.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure3); } void USART1_NVIC_Config(NVIC_InitTypeDef NVIC_InitStructure4) { NVIC_InitStructure4.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure4.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure4.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure4.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure4); } void NVIC_Configuration(int Interrupt_flag) { NVIC_InitTypeDef NVIC_InitStructure; /* 配置中断使用组合2 抢占式2位,响应式2位*/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); if(Interrupt_flag == 1) { /*外部中断1,Z相归零----优先级最高 0 0*/ PA_NVIC_Configuration(NVIC_InitStructure); } if(Interrupt_flag == 2) { /*TIM3中断,编码器解码----优先级 1 1*/ TIM3_NVIC_Config(NVIC_InitStructure); } if(Interrupt_flag == 3) { /*TIM2中断,数据帧接收完毕判断----优先级 2 2*/ TIM2_NVIC_Config(NVIC_InitStructure); } if(Interrupt_flag == 4) { /* USART1中断,数据接收----优先级 3 3*/ USART1_NVIC_Config(NVIC_InitStructure); } }[/mw_shl_code] 这里一共用到了四个中断,外部中断1,TIM3,TIM2,以及串口中断,这是NVIC配置

一周热门 更多>