STM32的外部中断配置及使用

STM32的外部中断配置及使用

配置1:GPIO;

配置外部中断为输入模式;

配置2:EXTI;

配置外部中断线和触发模式;

配置3:NVIC;

配置外部中断源和中断优先级;

 

需要注意的是:RCC_APB2Periph_AFIO 时钟必须打开,容易忽略的一点;

 

 1 void EXTI_PORTC_CONFIG(void)
 2 {
 3         GPIO_InitTypeDef GPIO_InitStructure;
 4         EXTI_InitTypeDef EXTI_InitStructure;
 5         NVIC_InitTypeDef NVIC_InitStructure;
 6 
 7         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);
 8 
 9         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
10         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //内部上拉
11         GPIO_Init(GPIOC, &GPIO_InitStructure);
12     
13         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
14         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //内部上拉
15         GPIO_Init(GPIOC, &GPIO_InitStructure);
16     
17         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
18         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //内部上拉
19         GPIO_Init(GPIOC, &GPIO_InitStructure);
20 
21         GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource2);
22         GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3);
23         GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5);
24 
25         EXTI_InitStructure.EXTI_Line = EXTI_Line2; 
26         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
27         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
28         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
29         EXTI_Init(&EXTI_InitStructure);
30         
31         EXTI_InitStructure.EXTI_Line = EXTI_Line3; 
32         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
33         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
34         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
35         EXTI_Init(&EXTI_InitStructure);
36         
37         EXTI_InitStructure.EXTI_Line = EXTI_Line5; 
38         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
39         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
40         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
41         EXTI_Init(&EXTI_InitStructure);
42 
43         NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
44         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
45         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
46         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
47         NVIC_Init(&NVIC_InitStructure);
48         
49         NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
50         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
51         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
52         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
53         NVIC_Init(&NVIC_InitStructure);
54         
55         NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
56         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
57         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
58         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
59         NVIC_Init(&NVIC_InitStructure);
60 }

 

posted on 2016-02-29 11:42  HelloWorldTotti  阅读(948)  评论(0编辑  收藏  举报

导航