.
ARM Cortex M3共有256个中断。
STM32F103CB共有43个外部中断和16个内部中断。
具有相同数值编号的IO口被分配为同一组,共用一条中断请求线,如下图:
所以共有15条中断线,另外
●EXTI线16连接到PVD输出
●EXTI线17连接到RTC闹钟事件
●EXTI线18连接到USB唤醒事件
寄存器:1、外部中断配置寄存器 AFIO_EXTxCR1------->用于选择输入中断源 Pin 0..3
AFIO_EXTxCR2------->用于选择输入中断源 Pin 0..7
AFIO_EXTxCR2------->用于选择输入中断源 Pin 8..11
AFIO_EXTxCR2------->用于选择输入中断源 Pin 12..15
(说明:5个端口PORTA..E,每个端口各有16位可供选择)
2、中断屏蔽寄存器 EXTI_IMR----------->屏蔽线上的中断请求 共19路
3、事件屏蔽寄存器 EXTI_EMR----------->屏蔽线上的事件 共19路
4、上升沿触发选择寄存器 EXTI_RTSR---------->设置相应线为上升沿触发 19位
5、下降沿触发选择寄存器 EXTI_FTSR---------->设置相应线为上升沿触发 19位
6、挂起寄存器 EXTI_PR------------>中断线上发生了选择的边沿事件时置1 19位
7、软件中断事件寄存器 EXTI_SWIER--------->写该位可以将EXTI_PR中相应的位值高,如果EXTI_IMR或EXTI_EMR使能,则可以产生相应的中断
配置流程:中断在使用前必须对中断线进行配置并激活
1、配置相应的IO口为输入,浮空输入时一定要加上拉或者是下拉
2、开启IO口复用时钟,设置IO与中断线的映射关系---->AFIO_EXTxCR1
3、设置相应中断触发条件---------->EXTI_IMR、EXTI_RTSR、EXTI_FTSR
4、在NVIC中开启并使能中断,对中断分组进行配置
5、编写中断服务函数
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------>
编写中断程序的步骤:(库函数版)
1.初始化系统时钟和端口时钟,在此需注意设置完端口时钟外还须将端口设置为功能复用IO时钟。RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE)
2.初始化GPIO。
3.中断配置,包括两部分(两个函数),中断配置和中断优先级配置。
中断配置:结构体:EXTI_InitTypeDef 。清空中断线、中断线映射CPU、中断线选择、中断模式、触发方式、使能。
优先级配置:结构体:NVIC_InitTypeDef。选组、选择IRQ通道、设置抢占优先级、设置从优先级、使能。
EG: EXTI_Configuration() 和 NVIC_Configuration()
分组情况:
优先级组别 | 抢占式优先级 | 副优先级 |
4 | 4位/16级 | 0位/0级 |
3 | 3位/8级 | 1位/2级 |
2 | 2位/4级 | 2位/4级 |
1 | 1位/2级 | 3位/8级 |
0 | 0位/0级 | 4位/6级 |
4.中断服务程序。比如0号中断线对应函数 void EXTI0_IRQHandler(void)
示例:按键改变LED的状态
1.主要功能是按键响应中断从而改变LED的状态。
2.按键所属端口:GPIOB,LED所属端口:GPIOB
3.按键管脚:PB0~PB7
4.LED管脚:PB8~PB15
5.PB0~PB5分别依次控制PB8~PB13
1 #include "pbdata.h" 2 #include "led.h" 3 #include "key.h" 4 #include "misc.h" 5 #include "stm32f10x_exti.h" 6 7 void RCC_Configration(void) 8 { 9 SystemInit(); 10 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 11 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //功能复用IO时钟 12 } 13 14 void GPIO_Configration(void) 15 { 16 GPIO_LED_Configration(); 17 GPIO_KEY_Configration(); 18 } 19 20 /* 21 *中断配置* 22 */ 23 void EXTI_Configuration(void) 24 { 25 EXTI_InitTypeDef EXTI_InitStructure; 26 27 EXTI_ClearITPendingBit(EXTI_Line0); //清空中断线 28 EXTI_ClearITPendingBit(EXTI_Line1); 29 EXTI_ClearITPendingBit(EXTI_Line2); 30 EXTI_ClearITPendingBit(EXTI_Line3); 31 EXTI_ClearITPendingBit(EXTI_Line4); 32 EXTI_ClearITPendingBit(EXTI_Line5); 33 EXTI_ClearITPendingBit(EXTI_Line6); 34 EXTI_ClearITPendingBit(EXTI_Line7); 35 36 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);//映射 37 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1); 38 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2); 39 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3); 40 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4); 41 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5); 42 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6); 43 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource7); 44 45 EXTI_InitStructure.EXTI_Line=EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3 | EXTI_Line4 | EXTI_Line5 | EXTI_Line6 | EXTI_Line7; //选择中断线 46 47 EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt; //设置模式 48 EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling; //设置触发方式 49 EXTI_InitStructure.EXTI_LineCmd=ENABLE; //使能 50 51 EXTI_Init(&EXTI_InitStructure); 52 } 53 54 void NVIC_Configuration0(void) //优先级设置函数 55 { 56 NVIC_InitTypeDef NVIC_InitStructure; 57 58 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 59 60 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //选择IRQ通道 61 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级设置 62 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //次优先级设置 63 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 64 NVIC_Init(&NVIC_InitStructure); 65 } 66 67 void NVIC_Configuration1(void) 68 { 69 NVIC_InitTypeDef NVIC_InitStructure; 70 71 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 72 73 NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; 74 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 75 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 76 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 77 NVIC_Init(&NVIC_InitStructure); 78 } 79 80 void NVIC_Configuration2(void) 81 { 82 NVIC_InitTypeDef NVIC_InitStructure; 83 84 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 85 86 NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; 87 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 88 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 89 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 90 NVIC_Init(&NVIC_InitStructure); 91 } 92 93 void NVIC_Configuration3(void) 94 { 95 NVIC_InitTypeDef NVIC_InitStructure; 96 97 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 98 99 NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; 100 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; 101 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 102 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 103 NVIC_Init(&NVIC_InitStructure); 104 } 105 106 void NVIC_Configuration4(void) 107 { 108 NVIC_InitTypeDef NVIC_InitStructure; 109 110 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 111 112 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; 113 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4; 114 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 115 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 116 NVIC_Init(&NVIC_InitStructure); 117 } 118 119 void NVIC_Configuration5(void) 120 { 121 NVIC_InitTypeDef NVIC_InitStructure; 122 123 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 124 125 NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; 126 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5; 127 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 128 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 129 NVIC_Init(&NVIC_InitStructure); 130 } 131 132 int main(void) 133 { 134 RCC_Configration(); 135 GPIO_Configration(); 136 EXTI_Configuration(); 137 NVIC_Configuration0(); 138 NVIC_Configuration1(); 139 NVIC_Configuration2(); 140 NVIC_Configuration3(); 141 NVIC_Configuration4(); 142 NVIC_Configuration5(); 143 while(1); 144 }
2014-08-23 23:12:45