STM32 进入不了中断
发布网友
发布时间:2022-08-18 20:14
我来回答
共3个回答
热心网友
时间:2023-10-20 01:03
/*我不知道你之前的程序,没法判断你错在哪。试试我调试成功的吧,你对照着看看有什么错误。正常情况PB5678循环亮,中断EXTI0_IRQHandler()发生后PB56循环亮,EXTI1_IRQHandler()发生后PB78循环亮,EXTI0的优先级高于EXTI1*/
#include "stm32f10x.h"
extern void Delay(vu32 nCount);
extern void delay(uint);
void RCC_Config(void);
void GPIO_Config(void);
void EXTI_Config(void);
void NVIC_Config(void);
int main(void)
{
//SystemInit();
RCC_Config();
GPIO_Config();
EXTI_Config();
NVIC_Config();
while (1)
{
GPIO_SetBits(GPIOB, GPIO_Pin_5);//GPIOB.5=1
Delay(0xfffff);
GPIO_SetBits(GPIOB, GPIO_Pin_6);//GPIOB.6=1
Delay(0xfffff);
GPIO_SetBits(GPIOB, GPIO_Pin_7);//GPIOB.7=1
Delay(0xfffff);
GPIO_SetBits(GPIOB, GPIO_Pin_8);//GPIOB.8=1
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);//GPIOB.5=0
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_6);//GPIOB.6=0
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_7);//GPIOB.7=0
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_8);//GPIOB.8=0
Delay(0xfffff);
}
}
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
/*开时钟*/
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;/*LED为B 5678*/
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_0|GPIO_Pin_1;//KEY为A 01
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void EXTI_Config(void)
{
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); //将A0接EXTI0
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断使能
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);//将A1接EXTI1
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断使能
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Config(void)//中断嵌套优先级设置
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//1组
/* Enable the EXTI0_IRQn Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the EXTI1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;//1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
extern void Delay(vu32 nCount) //简单的长时间延时函数
{
int i; //声明内部递减变量
for(; nCount!= 0; nCount--) //递减变量计数
{
for (i=0; i<0xffff; i++);
}
}
/*以下为中断函数,加到stm32f10x_it.c文件中*/
#include "stm32f10x_it.h"
void EXTI0_IRQHandler(void)
{
int i;
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
/* Clear the Key Button EXTI line pending bit */
EXTI_ClearITPendingBit(EXTI_Line0);//清中断标志位
for(i=0;i<2;i++)
{
GPIO_SetBits(GPIOB, GPIO_Pin_5);//GPIOB.5=1
Delay(0xfffff);
GPIO_SetBits(GPIOB, GPIO_Pin_6);//GPIOB.6=1
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);//GPIOB.5=0
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_6);//GPIOB.6=0
Delay(0xfffff);
}
}
}
void EXTI1_IRQHandler(void)
{
int i;
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
/* Clear the Key Button EXTI line pending bit */
EXTI_ClearITPendingBit(EXTI_Line1);//清中断标志位
for(i=0;i<2;i++)
{
GPIO_SetBits(GPIOB, GPIO_Pin_7);//GPIOB.7=1
Delay(0xfffff);
GPIO_SetBits(GPIOB, GPIO_Pin_8);//GPIOC.8=1
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_7);//GPIOB.7=0
Delay(0xfffff);
GPIO_ResetBits(GPIOB, GPIO_Pin_8);//GPIOB.8=0
Delay(0xfffff);
}
}
}
热心网友
时间:2023-10-20 01:03
同遇到问题,大概是这样的。
问题:串口接收中断跑飞,调试时,串口接一旦收到数据,PC指针就指向0xfffffffe。
背景:工程从别人那拿过来的,板子是VET6
解决:1、system_stm32f10x.c文件里边,128行
#define VECT_TAB_OFFSET 0x20000改成:#define VECT_TAB_OFFSET 0x0
2、魔术棒——target——IROM1由0x8020000改为0x8000000
热心网友
时间:2023-10-20 01:04
NVIC外部中断位开了吗?