STM32简单的外部IO中断一直调试不成功,请大神帮忙!!!!
include"stm32f10x.h"void_NVIC_INIT(void)//中断优先级配置{NVIC_InitStructure;NVIC_PriorityGro...
include "stm32f10x.h"
void _NVIC_INIT(void) //中断优先级配置
{
NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure);
}
void _GPIO_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO,ENABLE); //开启相关时钟
_NVIC_INIT(); //调用优先级配置
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8; //用PB8做一个LED以显示状态
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PC5作为外部中断输入(其实就是一个按键)
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5); //外部中断相关配置
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); }
int main(void)
{
_GPIO_INIT();
GPIO_ResetBits(GPIOB,GPIO_Pin_8); //开始时点亮LED
while(1);
}
中断程序写在it.c里。
void EXTI9_5IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
{
GPIO_SetBits(GPIOB,GPIO_Pin_8);
EXTI_ClearITPendingBit(EXTI_Line5);
}
} 展开
void _NVIC_INIT(void) //中断优先级配置
{
NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure);
}
void _GPIO_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO,ENABLE); //开启相关时钟
_NVIC_INIT(); //调用优先级配置
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8; //用PB8做一个LED以显示状态
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PC5作为外部中断输入(其实就是一个按键)
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5); //外部中断相关配置
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); }
int main(void)
{
_GPIO_INIT();
GPIO_ResetBits(GPIOB,GPIO_Pin_8); //开始时点亮LED
while(1);
}
中断程序写在it.c里。
void EXTI9_5IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
{
GPIO_SetBits(GPIOB,GPIO_Pin_8);
EXTI_ClearITPendingBit(EXTI_Line5);
}
} 展开
2个回答
展开全部
看了一下没发现有什么问题,是否是硬件问题或者是程序还初始化了其他外设导致的:
贴出一个我的代码:
#define XC4X_CE_GPIO_PORT GPIOA
#define XC4X_CE_GPIO_PORT_CLK RCC_APB2Periph_GPIOA
#define XC4X_CE_GPIO_PIN GPIO_Pin_8
#define XC4X_IRQ_GPIO_PORT GPIOA
#define XC4X_IRQ_GPIO_PORT_CLK RCC_APB2Periph_GPIOA
#define XC4X_IRQ_GPIO_PIN GPIO_Pin_9
#define XC4X_IRQ_GPIO_EXIT_SOURCE GPIO_PortSourceGPIOA
#define XC4X_IRQ_GPIO_EXIT_PIN GPIO_PinSource9
#define XC4X_IRQ_GPIO_EXIT_LINE EXTI_Line9
#define XC4X_IRQ_EXIT_NO EXTI9_5_IRQn
NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x00 );
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
/* XC4X-IRQ引脚--中断功能 */
RCC_APB2PeriphClockCmd(XC4X_IRQ_GPIO_PORT_CLK | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = XC4X_IRQ_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(XC4X_IRQ_GPIO_PORT, &GPIO_InitStructure);
GPIO_EXTILineConfig(XC4X_IRQ_GPIO_EXIT_SOURCE, XC4X_IRQ_GPIO_EXIT_PIN);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = XC4X_IRQ_GPIO_EXIT_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* 注意此处设置优先级大于等于configMAX_SYSCALL_INTERRUPT_PRIORITY */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = XC4X_IRQ_EXIT_NO;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0B;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
贴出一个我的代码:
#define XC4X_CE_GPIO_PORT GPIOA
#define XC4X_CE_GPIO_PORT_CLK RCC_APB2Periph_GPIOA
#define XC4X_CE_GPIO_PIN GPIO_Pin_8
#define XC4X_IRQ_GPIO_PORT GPIOA
#define XC4X_IRQ_GPIO_PORT_CLK RCC_APB2Periph_GPIOA
#define XC4X_IRQ_GPIO_PIN GPIO_Pin_9
#define XC4X_IRQ_GPIO_EXIT_SOURCE GPIO_PortSourceGPIOA
#define XC4X_IRQ_GPIO_EXIT_PIN GPIO_PinSource9
#define XC4X_IRQ_GPIO_EXIT_LINE EXTI_Line9
#define XC4X_IRQ_EXIT_NO EXTI9_5_IRQn
NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x00 );
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
/* XC4X-IRQ引脚--中断功能 */
RCC_APB2PeriphClockCmd(XC4X_IRQ_GPIO_PORT_CLK | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = XC4X_IRQ_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(XC4X_IRQ_GPIO_PORT, &GPIO_InitStructure);
GPIO_EXTILineConfig(XC4X_IRQ_GPIO_EXIT_SOURCE, XC4X_IRQ_GPIO_EXIT_PIN);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = XC4X_IRQ_GPIO_EXIT_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* 注意此处设置优先级大于等于configMAX_SYSCALL_INTERRUPT_PRIORITY */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = XC4X_IRQ_EXIT_NO;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0B;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
追问
硬件就一个按键,一端接IO口另一端接地。硬件仿真单步执行查看相关寄存器没问题,万用表测量端口有电平跳变,但内部就是触发不了中断!
追答
你换个引脚看看,搞不好引脚里面挂掉了
或者使用我的代码试试,实测ok的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询