STM32F4外部中断怎么都解决不了,求助!!

2019-07-21 04:53发布

这样配置有木有错??求各位指点,初学者不太懂!
void GPIO_Configuaration(void)//定义PA0口,初始化
{
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
 GPIO_Init(GPIOA,&GPIO_InitStructure);
  
}
void EXTI_Config(void)
{
 EXTI_InitTypeDef EXTI_InitStructure;
 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,GPIO_PinSource0);   
 EXTI_InitStructure.EXTI_Line=EXTI_Line0;
 EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
 EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
 EXTI_InitStructure.EXTI_LineCmd=ENABLE;
 EXTI_Init(&EXTI_InitStructure);
}
外部中断,
void NVIC_Config(void)
{
 NVIC_InitTypeDef NVIC_InitStructure;
 //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
 NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
 NVIC_Init(&NVIC_InitStructure);
} 发生中断时,就是LED状态反转!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
Admin
1楼-- · 2019-07-21 06:19
[mw_shl_code=c,true]#include "exti.h" #include "delay.h" #include "led.h" #include "key.h" #include "beep.h" //外部中断0服务程序 void EXTI0_IRQHandler(void) { delay_ms(10); //消抖 if(WK_UP==1) { BEEP=!BEEP; } EXTI_ClearITPendingBit(EXTI_Line0); } //外部中断2服务程序 void EXTI2_IRQHandler(void) { delay_ms(10); //消抖 if(KEY2==0) { LED0=!LED0; LED1=!LED1; } EXTI_ClearITPendingBit(EXTI_Line2); } //外部中断3服务程序 void EXTI3_IRQHandler(void) { delay_ms(10); //消抖 if(KEY1==0) { LED1=!LED1; } EXTI_ClearITPendingBit(EXTI_Line3); //清除LINE3上的中断标志位 } //外部中断4服务程序 void EXTI4_IRQHandler(void) { delay_ms(10); //消抖 if(KEY0==0) { LED0=!LED0; } EXTI_ClearITPendingBit(EXTI_Line4);//清除LINE4上的中断标志位 } //外部中断初始化程序 //初始化PE2~4,PA0为中断输入. void EXTIX_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; KEY_Init(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);//PE2 连接到中断线2 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);//PE3 连接到中断线3 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);//PE4 连接到中断线4 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);//PA0 连接到中断线0 /* 配置EXTI_Line0 */ EXTI_InitStructure.EXTI_Line = EXTI_Line0;//LINE0 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE0 EXTI_Init(&EXTI_InitStructure);//配置 /* 配置EXTI_Line2,3,4 */ EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3 | EXTI_Line4; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断线使能 EXTI_Init(&EXTI_InitStructure);//配置 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//外部中断0 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道 NVIC_Init(&NVIC_InitStructure);//配置 NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;//外部中断2 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道 NVIC_Init(&NVIC_InitStructure);//配置 NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//外部中断3 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;//抢占优先级2 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道 NVIC_Init(&NVIC_InitStructure);//配置 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中断4 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道 NVIC_Init(&NVIC_InitStructure);//配置 } [/mw_shl_code]



[mw_shl_code=c,true]void KEY_Init(void) //IO初始化 { GPIO_InitTypeDef GPIO_InitStructure; //初始化KEY0-->GPIOA.13,KEY1-->GPIOA.15 上拉输入 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//PE2~4 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入 GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4 //初始化 WK_UP-->GPIOA.0 下拉输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0 }[/mw_shl_code]

taizonglai
2楼-- · 2019-07-21 07:23
中断配置貌似没有问题,你试一下把中断分组改为组2,抢占优先级和子优先级也改一下。
zengjie
3楼-- · 2019-07-21 10:29
 精彩回答 2  元偷偷看……

一周热门 更多>