请大家帮我看看,为何无法进入中断呢??只看到灯循环点亮,按键却无法触发中断。

2019-03-23 20:33发布

下面的程序,是我从一个教程的代码上修改的,调了2天,还是不行,真急人。望大家多多指点,谢谢了
#include <stm32f10x_lib.h>
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ErrorStatus HSEStartUpStatus;
  void  RCC_Config(void);
  void  GPIO_Config(void );  
  void  NVIC_Config(void );
  void  EXTI_Config(void );
  void  Delay(vu32 ncount);


int main(void)
{
   RCC_Config();
   GPIO_Config();  
   NVIC_Config();
   EXTI_Config();


while(1)
{
  GPIO_SetBits(GPIOD , GPIO_Pin_3);
  GPIO_ResetBits(GPIOD , GPIO_Pin_4|GPIO_Pin_7|GPIO_Pin_13);
  Delay(0xfffff);
  Delay(0xfffff);
  Delay(0xfffff);
  GPIO_SetBits(GPIOD , GPIO_Pin_4);
  GPIO_ResetBits(GPIOD , GPIO_Pin_3|GPIO_Pin_7|GPIO_Pin_13);
  Delay(0xfffff);
  Delay(0xfffff);
  Delay(0xfffff);
  GPIO_SetBits(GPIOD , GPIO_Pin_7);
  GPIO_ResetBits(GPIOD , GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_13);
  Delay(0xfffff);
  Delay(0xfffff);
  Delay(0xfffff);
  GPIO_SetBits(GPIOD , GPIO_Pin_13);
  GPIO_ResetBits(GPIOD , GPIO_Pin_4|GPIO_Pin_7|GPIO_Pin_3);
  Delay(0xfffff);
  Delay(0xfffff);
  Delay(0xfffff);
}
}

//延时程序
void Delay(vu32 ncount)
{
for(;ncount!=0;ncount--);
}

//步骤一系统初始化,使用固件模板中自带的SystemInit();函数即可。
void RCC_Config(void)
{
SystemInit();
//该函数配置系统时钟,其函数原型在system_stm32f10x.c
//RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO , ENABLE);
//使能所用时钟,主要是各个GPIO时钟,不要忘记RCC_APB2Periph_AFIO时钟使能
}
//步骤二GPIO 配置 ,使用 GPIO_Config()函数
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
/*
* LD1 -> PD7 , LD2 -> PD13 , LD3 -> PD3 , LD4 -> PD4
* B3 -> PB8 , B4-> PC13
*/
RCC->APB2ENR|=0x09;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 |GPIO_Pin_13|GPIO_Pin_3|GPIO_Pin_4;  //配置PD为输出模式,由CPU驱动LED点亮
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置PB8为浮空输入模式FLOATING,触发中断1--IPU 上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置PC13为浮空输入模式,触发中断2
GPIO_Init(GPIOC, &GPIO_InitStructure);          
}
//步骤三EXTI 的配置。此步相当于单片机的中断配置,使用 EXTI_Config()函数
void EXTI_Config(void)
{

/* Initialize the EXTI Init Structure parameters */
EXTI_InitTypeDef EXTI_InitStructure;  //声明语句 要写在前面,一句就可以了

GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9); // 管脚选择
EXTI_ClearITPendingBit(EXTI_Line9);         //清除中断标志位,进入中断后,先做清除中断标志位,否则它会不断响应中断,不断进入中断函数。
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //上升下降沿都触发 Falling  Rising
EXTI_InitStructure.EXTI_Line = EXTI_Line9; //EXTI_Line8,这个表示的是中断线8。对于外部中断中的GPIO,有16 个中断线,分别是0-15,刚好对应于每个GPIO 端口的0-15 引脚。
EXTI_InitStructure.EXTI_LineCmd = ENABLE;        //启动中断的接收
EXTI_Init(&EXTI_InitStructure);           //外部中断启动
EXTI_GenerateSWInterrupt(EXTI_Line9);       

GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
EXTI_ClearITPendingBit(EXTI_Line13);         
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);       
EXTI_GenerateSWInterrupt(EXTI_Line13);         
}
//步骤四NVIC 配置。先解释下NVIC,它的中文全称是“嵌套向量中断控制器"。用的是NVIC_Config()函数
void NVIC_Config(void)
{

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);         //for中断1
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
       
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);           //for中断2
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);          
}
//步骤五中断服务子程序的编写。
void EXTI9_IRQHandler(void)                        //中断1
{
if ( EXTI_GetITStatus(EXTI_Line9) != RESET )  //判断外部中断1的标志位是否为1,
{
EXTI_ClearITPendingBit(EXTI_Line9);                   //若外部中断1的标志位为1,则清中断标志位
GPIO_ResetBits(GPIOD , GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7|GPIO_Pin_13);  //给PD口的3、4、7、13脚置0
Delay(0xfffff);
Delay(0xfffff);
Delay(0xfffff);
EXTI_ClearFlag( EXTI_Line9);           //清中断标志
}
}

void EXTI13_IRQHandler(void)                        //中断2
{
if ( EXTI_GetITStatus(EXTI_Line13) != RESET )
{
EXTI_ClearITPendingBit(EXTI_Line13);  //清中断
GPIO_SetBits(GPIOD , GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7|GPIO_Pin_13);        //给PD口的3、4、7、13脚置1(set)
Delay(0xfffff);
Delay(0xfffff);
Delay(0xfffff);
EXTI_ClearFlag( EXTI_Line13);           //清中断标志  
}  
   
} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。