学习到了EXTI中断,模仿例程改写了一个按键2(PD3)产生中断控制LED2(PD12)亮灭的程序但是按键时LED毫无反应。附上部分源码,求大神解释一下! /****************** stm32f10_it.c *******************************/
/* I/O线中断,线中断为PD3(KEY2) */ void EXTI3_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line3) != RESET) { // LED2(PD12) 取反 GPIO_WriteBit(GPIOD, GPIO_Pin_12, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_12)))); EXTI_ClearITPendingBit(EXTI_Line3); //清除中断标志位 } } /******************* exti.c *********************************/ /*函数名:NVIC_Configuration *描述 :配置嵌套向量中断控制器NVIC */ static void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*配置P[A|B|C|D|E]3为中断源*/ #include "stm32f10x.h" // Device header NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/* * 函数名:EXTI_PD3_Config * 描述:配置PD3为线中断口,并设置中断优先级 * */ void EXTI_PD3_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure;
/* config the extiline(PD3) clock and AFIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE); /* config the NVIC(PD3) */ NVIC_Configuration();
/* EXTI line gpio config(PD3) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOD, &GPIO_InitStructure);
/* EXTI line(PD3) mode config */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3); EXTI_InitStructure.EXTI_Line = EXTI_Line3; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); }
/******************* ***************************** ****/ /************************ LED.H *******************************************/ #ifndef __LED_H #define __LED_H
#include "stm32f10x.h"
/* the macro definition to trigger the led on or off * 1 - off - 0 - on */ #define ON 0 #define OFF 1
#define LED1(a) if (a) \ GPIO_SetBits(GPIOB,GPIO_Pin_5);\ else \ GPIO_ResetBits(GPIOB,GPIO_Pin_5)
#define LED2(a) if (a) \ GPIO_SetBits(GPIOD,GPIO_Pin_12);\ else \ GPIO_ResetBits(GPIOD,GPIO_Pin_12)
void LED_GPIO_Config(void);
#endif /* __LED_H */
/************************* LED.C *********************************/ **********************************************************************************/ #include "led.h"
/* * 函数名:LED_GPIO_Config *描述:配置LED用到的io口
*/ void LED_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_5); // turn off all led GPIO_SetBits(GPIOD, GPIO_Pin_12); }
/************************** main.c ************************************/ #include "stm32f10x.h" #include "led.h" #include "exti.h" int main(void) { /*初始化LED*/ LED_GPIO_Config(); LED2(ON); /**初始化 exit line**/ EXTI_PD3_Config(); /*等待中断*/ while(1) { } }
|