本帖最后由 泥团 于 2017-7-31 19:58 编辑
求问,EXTI进入不了中断函数,灯一直亮着。
IC:STM32F103VET6
灯是PB5控制,拉高为亮,低为灭
按键是PB15,按下去是直接接地,PB15上拉
主函数:
int main(void)
{
//初始化用的GPIO口
LED_GPIO_Config();
//KEY_GPIO_Config();
EXTI_GPIO_Config();
LED3(ON);
while (1);
}
函数exti.h:
#ifndef _EXTI_H
#define _EXTI_H
//头文件引用
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
void EXTI_GPIO_Config(void)
{
//GPIO的复用功能必须打开
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//GPIO结构体
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//配置外部中断源并进行中断源和GPIO的连接
EXTI_InitTypeDef EXTI_InitStructure;
//清空中断标志
//EXTI_ClearITPendingBit(EXTI_Line15);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource15);
EXTI_InitStructure.EXTI_Line = EXTI_Line15;//选择中断线路15
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//设置为中断请求,非事件请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //设置中断触发方式为上下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//外部中断使能
EXTI_Init(&EXTI_InitStructure);
//配置NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;//选择中断通道5
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2; //抢占式中断优先级设置为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //响应式中断优先级设置为1
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
}
/*键盘中断(EXTI15)服务程序*/
void EXTI15_10_IRQHandler(void)
{
LED_GPIO_Config();
//if (EXTI_GetITStatus(EXTI_Line15) == SET)
//if(key0=0)
delay_ms(100);
LED3(OFF);
delay_ms(100);
EXTI_ClearITPendingBit(EXTI_Line15);//清除标志
}
#endif
函数led.h:
#ifndef _LED_H
#define _LED_H
#include "stm32f10x.h"
//初始化LED对应的GPIO,并设置为高电平,点亮对应的LED
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB ,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init (GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
}
#define ON 1
#define OFF 0
#define LED1(a) if (a)
GPIO_SetBits(GPIOB,GPIO_Pin_3);
else
GPIO_ResetBits (GPIOB,GPIO_Pin_3);
#define LED2(a) if (a)
GPIO_SetBits(GPIOB,GPIO_Pin_4);
else
GPIO_ResetBits (GPIOB,GPIO_Pin_4);
#define LED3(a) if (a)
GPIO_SetBits(GPIOB,GPIO_Pin_5);
else
GPIO_ResetBits (GPIOB,GPIO_Pin_5);
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);试一下
一周热门 更多>