void MiniBalance_EXTI_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//ҿאìѨҪʹŜAFIOʱד RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹŜGPIOࠚʱד GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //ࠚƤ׃ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ʏ-ˤɫ GPIO_Init(GPIOA, &GPIO_InitStructure); //ٹߝʨ֨ӎ˽ԵʼۯGPIO GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource12); EXTI_InitStructure.EXTI_Line=EXTI_Line12; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//ЂݵҘԥע EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); //ٹߝEXTI_InitStructאָ֨քӎ˽ԵʼۯʨEXTI݄զǷ NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //ʹŜдݼ̹՚քҿאͨր NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ȀռԅЈܶ2ì NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //ؓԅЈܶ1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹŜҿאͨր NVIC_Init(&NVIC_InitStructure); }
时间: 2024-04-27 10:20:16 浏览: 139
这是一个用于初始化外部中断的函数,其中包括对GPIO和EXTI的初始化,以及对NVIC中断的配置。其中,GPIOA的Pin12被配置为输入上拉模式,EXTI线路12被配置为下降沿触发中断模式。NVIC中断通道被配置为EXTI15_10,优先级为0x02和0x01,使能EXTI15_10_IRQn通道中断。
相关问题
在保持原来的代码逻辑的条件下,根据STM32F407的特性,把下面STM32F1的代码移植到STM32F407。void ExtiGpioInit(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5 ; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); } void ExtiNvicInit(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void ExtiModeInit(void) { EXTI_InitTypeDef EXTI_InitStructure; GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4); EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_Line = EXTI_Line4; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5); EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_Line = EXTI_Line5; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); }
void ExtiGpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5 ;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void ExtiNvicInit(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ExtiModeInit(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource4);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource5);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
int16_t Encoder_Num; void Encoder_Init (void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_InitTypeDef GPIO_Initstructure; GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1; GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_Initstructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0|GPIO_PinSource1); EXTI_InitTypeDef EXTI_Initstructure; EXTI_Initstructure.EXTI_Line=EXTI_Line0|EXTI_Line1; EXTI_Initstructure.EXTI_LineCmd=ENABLE; EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Falling; EXTI_Init(&EXTI_Initstructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_Initstructure; NVIC_Initstructure.NVIC_IRQChannel=EXTI0_IRQn; NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=1; NVIC_Initstructure.NVIC_IRQChannelSubPriority=1; NVIC_Init(&NVIC_Initstructure); NVIC_Initstructure.NVIC_IRQChannel=EXTI1_IRQn; NVIC_Initstructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority=2; NVIC_Initstructure.NVIC_IRQChannelSubPriority=2; NVIC_Init(&NVIC_Initstructure); } int Encoder_get() { int16_t temp; temp=Encoder_Num; Encoder_Num=0; return temp; }void EXTI0_IRQHandler () { if (EXTI_GetITStatus(EXTI_Line0)==SET) { if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0) { if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==1) { Encoder_Num--; } else { Encoder_Num++; } } EXTI_ClearITPendingBit(EXTI_Line0); } }
根据您提供的代码,有几个可能导致中断不触发的问题:
1. 外部中断线未正确配置:根据您的代码,外部中断线0已经正确配置为中断模式并连接到GPIOB的引脚0。但是,您需要确保外部中断线1也正确连接到GPIOB的引脚1,并进行相应的配置。
2. 中断优先级设置不正确:根据代码,中断优先级已经设置为EXTI0_IRQn的优先级为1,EXTI1_IRQn的优先级为2。但是,您需要确保这些优先级高于其他可能存在的中断。
3. 中断触发方式设置不正确:根据代码,中断触发方式被配置为下降沿触发。如果您期望使用上升沿触发或其他触发方式,请相应地调整`EXTI_Initstructure.EXTI_Trigger`参数。
请确保以上问题得到正确解决,并检查硬件连接是否正确。如果问题仍然存在,请提供更多关于硬件环境和其他相关代码的信息,以便更好地帮助您解决问题。
阅读全文