STM32为什么出不了中断
时间:10-02
整理:3721RD
点击:
/*实现功能:按键之前D2常亮;
按下INT2键(C13端口,对应EXTI13外部中断线),进入中断程序D2常灭,恢复后D2常亮*/
#include "stm32f10x.h"
/*****************************/
void LED_GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); //开启GPIO_G外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //板上LED编号 D2 PG14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚速率
GPIO_Init(GPIOG, &GPIO_InitStructure); //调用库函数,初始化GPIO_G
GPIO_SetBits(GPIOG,GPIO_Pin_14); //点亮D2
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOG, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOD,GPIO_Pin_13);
}
void NVIC_Config(void) //NVIC中断控制器初始化配置
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置中断优先级分组1
NVIC_InitStructure.NVIC_IRQChannel =EXTI15_10_IRQn; //设定中断源为EXTI13
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_Config(void) //外部中断配置 PC13
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
NVIC_Config();
/*EXTI LINE GPIO CONFIG */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //C13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*EXTI LINE MODE CONFIG*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式为中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿出发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断线
EXTI_Init(&EXTI_InitStructure); //根据参数初始化中断寄存器
}
EXTI15_10_IRQHandler() //外部中断13中断服务函数
{
if(EXTI_GetITStatus(EXTI_Line13)!=RESET) //进入中断后,调用EXTI_GetITStatus()函数再次检查是否产生了EXTI_Line中断
{
EXTI_ClearFlag(EXTI_Line13);
GPIO_ResetBits(GPIOG,GPIO_Pin_14);
//GPIO_SetBits(GPIOD,GPIO_Pin_13); //灭D2
EXTI_ClearITPendingBit(EXTI_Line13); //清除中断标志位再退出中断服务程序
}
}
int main()
{
LED_GPIO_Config();
EXTI_Config();
while(1); //等待中断
}
按下INT2键(C13端口,对应EXTI13外部中断线),进入中断程序D2常灭,恢复后D2常亮*/
#include "stm32f10x.h"
/*****************************/
void LED_GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); //开启GPIO_G外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //板上LED编号 D2 PG14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚速率
GPIO_Init(GPIOG, &GPIO_InitStructure); //调用库函数,初始化GPIO_G
GPIO_SetBits(GPIOG,GPIO_Pin_14); //点亮D2
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOG, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOD,GPIO_Pin_13);
}
void NVIC_Config(void) //NVIC中断控制器初始化配置
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置中断优先级分组1
NVIC_InitStructure.NVIC_IRQChannel =EXTI15_10_IRQn; //设定中断源为EXTI13
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_Config(void) //外部中断配置 PC13
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
NVIC_Config();
/*EXTI LINE GPIO CONFIG */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //C13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*EXTI LINE MODE CONFIG*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式为中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿出发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断线
EXTI_Init(&EXTI_InitStructure); //根据参数初始化中断寄存器
}
EXTI15_10_IRQHandler() //外部中断13中断服务函数
{
if(EXTI_GetITStatus(EXTI_Line13)!=RESET) //进入中断后,调用EXTI_GetITStatus()函数再次检查是否产生了EXTI_Line中断
{
EXTI_ClearFlag(EXTI_Line13);
GPIO_ResetBits(GPIOG,GPIO_Pin_14);
//GPIO_SetBits(GPIOD,GPIO_Pin_13); //灭D2
EXTI_ClearITPendingBit(EXTI_Line13); //清除中断标志位再退出中断服务程序
}
}
int main()
{
LED_GPIO_Config();
EXTI_Config();
while(1); //等待中断
}
我觉得应该是你GPIOC13的接入方式不对吧,你注释的是上拉输入,但是你用的却是GPIO_Mode_IPD,这个应该是下拉输入模式吧,你再看看!