在使用stm32的中断线的时候,搞不清EXTI0-19分别对应哪些中断,看启动文件也只是能看出一共有哪些中断来,但是分别对应到哪个中断,也没有具体说明,通过查找资料,以及对于前辈写的程序的分析,大概是这样的(有不对的地方欢迎给我指出来,可以加WX:zjlcy56789,说明csdn上看到的),自己做一下记录:
参考资料出处:https://zhidao.baidu.com/question/1512653606550702220.html
外部中断/事件控制器由19个产生事件/中断要求的边沿检测器组成。每个输入线可以独立地配置
输入类型(脉冲或挂起)和对应的触发事件(上升沿或下降沿或者双边沿都触发)。每个输入线都可以被独立的屏蔽。挂起寄存器保持着状态线的中断要求。
=========================================分割线=========================================
19个中断对应如下:
17——EXTI线16连接到PVD输出
18——EXTI线17连接到RTC闹钟事件
19——EXTI线18连接到USB唤醒事件
EXTI0连接的引脚为PA0、PB0、PC0、PD0、PE0、PF0、PG0,
其他外部中断EXTI1——EXTI15类似。所以在使用时尽量将需要的外部中断配置在不同的EXTIx上。
例如需要3个外部中断,我们可以配置到PA0、PB4、PG3上,此时每个中断都有自己的中断处理程序段。如果配置到PA0、PB0、PC1,则PA0和PB0将公用一个中断程序段。当然如果特殊需要,也
可以这样设计。
=========================================分割线=========================================
配置使用方法:
初始化相应的GPIO管脚配置外部中断源并进行中断源和GPIO的连接编写相应中断源的中断处理程序
=========================================分割线=========================================
程序代码
void exti_init(const exti_config_t *config)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_TypeDef* GPIOX;
uint8_t pin_source;
uint16_t pin_mode;
GPIOX=(GPIO_TypeDef *)gpio_hardware_map[config->gpio].base;
RCC_AHBPeriphClockCmd(gpio_hardware_map[config->gpio].clock, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Pin = config->pin;
pin_mode=config->mode;
switch(pin_mode){
case INPUT_PULL_UP:
GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
break;
case INPUT_PULL_DOWN:
GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
break;
case INPUT_HIGH_IMPEDANCE:
GPIO_InitStructure.GPIO_OType =GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
break;
default:
GPIO_InitStructure.GPIO_OType =GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
break;
}
GPIO_Init(GPIOX, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
pin_source=gpio_pin_source(config->pin);
SYSCFG_EXTILineConfig(gpio_port_source(config->gpio),pin_source);
EXTI_InitStructure.EXTI_Line = (uint32_t)config->pin;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = config->triger;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = exti_irq_map[pin_source];
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
exti_callback[pin_source]=config->callback;
}
void EXTI0_IRQnHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
if(exti_callback[0]!=NULL)
exti_callback[0]();
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQnHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET) {
if(exti_callback[1]!=NULL)
exti_callback[1]();
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQnHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line2) != RESET) {
if(exti_callback[2]!=NULL)
exti_callback[2]();
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQnHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) != RESET) {
if(exti_callback[3]!=NULL)
exti_callback[3]();
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
void EXTI4_IRQnHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line4) != RESET) {
if(exti_callback[4]!=NULL)
exti_callback[4]();
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
void EXTI9_5_IRQnHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line5) != RESET) {
if(exti_callback[5]!=NULL)
exti_callback[5]();
EXTI_ClearITPendingBit(EXTI_Line5);
}
if (EXTI_GetITStatus(EXTI_Line6) != RESET) {
if(exti_callback[6]!=NULL)
exti_callback[6]();
EXTI_ClearITPendingBit(EXTI_Line6);
}
if (EXTI_GetITStatus(EXTI_Line7) != RESET) {
if(exti_callback[7]!=NULL)
exti_callback[7]();
EXTI_ClearITPendingBit(EXTI_Line7);
}
if (EXTI_GetITStatus(EXTI_Line8) != RESET) {
if(exti_callback[8]!=NULL)
exti_callback[8]();
EXTI_ClearITPendingBit(EXTI_Line8);
}
if (EXTI_GetITStatus(EXTI_Line9) != RESET) {
if(exti_callback[9]!=NULL)
exti_callback[9]();
EXTI_ClearITPendingBit(EXTI_Line9);
}
}
void EXTI15_10_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line10) != RESET) {
if(exti_callback[10]!=NULL)
exti_callback[10]();
EXTI_ClearITPendingBit(EXTI_Line10);
}
if (EXTI_GetITStatus(EXTI_Line11) != RESET) {
if(exti_callback[11]!=NULL)
exti_callback[11]();
EXTI_ClearITPendingBit(EXTI_Line11);
}
if (EXTI_GetITStatus(EXTI_Line12) != RESET) {
if(exti_callback[12]!=NULL)
exti_callback[12]();
EXTI_ClearITPendingBit(EXTI_Line12);
}
if (EXTI_GetITStatus(EXTI_Line13) != RESET) {
if(exti_callback[13]!=NULL)
exti_callback[13]();
EXTI_ClearITPendingBit(EXTI_Line13);
}
if (EXTI_GetITStatus(EXTI_Line14) != RESET) {
if(exti_callback[14]!=NULL)
exti_callback[14]();
EXTI_ClearITPendingBit(EXTI_Line14);
}
if (EXTI_GetITStatus(EXTI_Line15) != RESET) {
if(exti_callback[15]!=NULL)
exti_callback[15]();
EXTI_ClearITPendingBit(EXTI_Line15);
}
}
如RTC的中断配置:
void platform_rtc_init(void)
{
uint32_t timeout = 600000;
RTC_InitTypeDef RTC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
RTC_WaitForSynchro();
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {
timeout--;
if (timeout == 0)
break;
}
if (timeout == 0) {
RCC_LSICmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
;
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
} else {
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Select the RTC Clock Source */
}
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro(); /* Wait for RTC APB registers synchronisation */
/* Configure the RTC data register and RTC prescaler */
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) { //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎
RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
RTC_InitStructure.RTC_SynchPrediv = 0xFF;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
}
RTC_WaitForSynchro();
//非待机模式,RTC Wakeup timer中断配置及中断处理
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the RTC Wakeup Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; //RTC_Alarm_IRQn RTC_WKUP_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
}