stm32常用中断配置



配置是stm32外部中断步骤:
1.使能IO时钟和AFIO时钟
2.配置EXIT中断线
3.配置中断控制器NVIC (EXTI15_10_IRQn,EXTI4_IRQn)
4.写中断服务子程序 (EXTI15_10_IRQHandler)
首先要明白中断IO对应的中断线EXTIx
其中
PA0-PE0 对应的是EXTI0
....... . . ........
PA4-PE4 对应的是EXTI4
共用中断线
PA5-PE5
...... 对应的是EXTI9_5
......
PA9-PE9
共用中短线
PA10-PE10
......... 对应的是EXTI15_10
.........
PA15-PE15
低电平-高电平(数据就绪)

例子:

GPIO_InitTypeDef GPIO_InitStructur;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructur.GPIO_Pin=GPIO_Pin_11; //ECSƬѡ½Å
GPIO_InitStructur.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructur.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructur);
GPIO_SetBits(GPIOD,GPIO_Pin_11);

GPIO_InitStructur.GPIO_Pin=GPIO_Pin_10; //ÍⲿÖжÏÒý½Å
GPIO_InitStructur.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructur.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructur);


EXTI_InitTypeDef EXTI_InitStructure; //ÅäÖÃÖжÏÏß
EXTI_ClearITPendingBit(EXTI_Line10); //Çå³ýÖжÏÏß10±ê־λ
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource10);
EXTI_InitStructure.EXTI_Line= EXTI_Line10;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;//ÉÏÉýÑØ´¥·¢
EXTI_InitStructure.EXTI_LineCmd=ENABLE; //ÖжÏʹÄÜ
EXTI_Init(&EXTI_InitStructure);

NVIC_InitTypeDef NVIC_InitStructure; //ÅäÖÃNVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //ÖжϷÖ×é
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //ÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÏìÓ¦ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ÖжÏͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler()
{


if (EXTI_GetITStatus(EXTI_Line11) != RESET)
EXTI_ClearITPendingBit(EXTI_Line10); //Çå³ýÖж
 
配置是stm32外部中断步骤:
 1.使能IO时钟和AFIO时钟
        2.配置EXIT中断线
 3.配置中断控制器NVIC  (EXTI15_10_IRQn,EXTI4_IRQn)
 4.写中断服务子程序     (EXTI15_10_IRQHandler)
首先要明白中断IO对应的中断线EXTIx
其中
PA0-PE0     对应的是EXTI0
.......      . . ........
PA4-PE4     对应的是EXTI4
共用中断线
PA5-PE5  
......      对应的是EXTI9_5
......       
PA9-PE9
共用中短线   
PA10-PE10
.........   对应的是EXTI15_10
.........
PA15-PE15
低电平-高电平(数据就绪)

例子:

GPIO_InitTypeDef GPIO_InitStructur;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE); 
 GPIO_InitStructur.GPIO_Pin=GPIO_Pin_11;   //ECSƬѡ½Å
 GPIO_InitStructur.GPIO_Mode=GPIO_Mode_Out_PP;
 GPIO_InitStructur.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOD,&GPIO_InitStructur);
 GPIO_SetBits(GPIOD,GPIO_Pin_11);
 
 GPIO_InitStructur.GPIO_Pin=GPIO_Pin_10;    //ÍⲿÖжÏÒý½Å
 GPIO_InitStructur.GPIO_Mode=GPIO_Mode_IPD;
 GPIO_InitStructur.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOD,&GPIO_InitStructur);
 
 
 EXTI_InitTypeDef EXTI_InitStructure;   //ÅäÖÃÖжÏÏß
 EXTI_ClearITPendingBit(EXTI_Line10);   //Çå³ýÖжÏÏß10±ê־λ
 GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource10);
 EXTI_InitStructure.EXTI_Line= EXTI_Line10;
 EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
 EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;//ÉÏÉýÑØ´¥·¢
 EXTI_InitStructure.EXTI_LineCmd=ENABLE;  //ÖжÏʹÄÜ
 EXTI_Init(&EXTI_InitStructure);
 
NVIC_InitTypeDef NVIC_InitStructure;     //
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  //ÖжϷÖ×é
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //ÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //ÏìÓ¦ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //ÖжÏͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler()
{
 
 
 if (EXTI_GetITStatus(EXTI_Line11) != RESET)
         EXTI_ClearITPendingBit(EXTI_Line10);  //Çå³ýÖжϱê־λ
}
 
 

你可能感兴趣的:(stm32)