新聞中心

中斷之外部中斷

作者: 時間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
ARM Cortex M3共有256個中斷。

STM32F103CB共有43個外部中斷和16個內(nèi)部中斷。

本文引用地址:http://m.butianyuan.cn/article/201611/318391.htm

具有相同數(shù)值編號的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、上升沿觸發(fā)選擇寄存器EXTI_RTSR->設(shè)置相應(yīng)線為上升沿觸發(fā)19位

5、下降沿觸發(fā)選擇寄存器EXTI_FTSR->設(shè)置相應(yīng)線為上升沿觸發(fā)19位

6、掛起寄存器EXTI_PR>中斷線上發(fā)生了選擇的邊沿事件時置119位

7、軟件中斷事件寄存器EXTI_SWIER>寫該位可以將EXTI_PR中相應(yīng)的位值高,如果EXTI_IMR或EXTI_EMR使能,則可以產(chǎn)生相應(yīng)的中斷

配置流程:中斷在使用前必須對中斷線進行配置并激活

1、配置相應(yīng)的IO口為輸入,浮空輸入時一定要加上拉或者是下拉

2、開啟IO口復(fù)用時鐘,設(shè)置IO與中斷線的映射關(guān)系->AFIO_EXTxCR1

3、設(shè)置相應(yīng)中斷觸發(fā)條件->EXTI_IMR、EXTI_RTSR、EXTI_FTSR

4、在NVIC中開啟并使能中斷,對中斷分組進行配置

5、編寫中斷服務(wù)函數(shù)

>

編寫中斷程序的步驟:(庫函數(shù)版)

1.初始化系統(tǒng)時鐘和端口時鐘,在此需注意設(shè)置完端口時鐘外還須將端口設(shè)置為功能復(fù)用IO時鐘。RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE)

2.初始化GPIO。

3.中斷配置,包括兩部分(兩個函數(shù)),中斷配置和中斷優(yōu)先級配置。

中斷配置:結(jié)構(gòu)體:EXTI_InitTypeDef 。清空中斷線、中斷線映射CPU、中斷線選擇、中斷模式、觸發(fā)方式、使能。

優(yōu)先級配置:結(jié)構(gòu)體:NVIC_InitTypeDef。選組、選擇IRQ通道、設(shè)置搶占優(yōu)先級、設(shè)置從優(yōu)先級、使能。

EG:  EXTI_Configuration() 和 NVIC_Configuration()

分組情況:

優(yōu)先級組別搶占式優(yōu)先級副優(yōu)先級
44位/16級0位/0級
33位/8級1位/2級
22位/4級2位/4級
11位/2級3位/8級
00位/0級4位/6級

4.中斷服務(wù)程序。比如0號中斷線對應(yīng)函數(shù) void EXTI0_IRQHandler(void)

示例:按鍵改變LED的狀態(tài)

1.主要功能是按鍵響應(yīng)中斷從而改變LED的狀態(tài)。
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);      //功能復(fù)用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;           //設(shè)置模式48     EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;       //設(shè)置觸發(fā)方式49     EXTI_InitStructure.EXTI_LineCmd=ENABLE;                        //使能50 51     EXTI_Init(&EXTI_InitStructure);52 }53 54 void NVIC_Configuration0(void)         //優(yōu)先級設(shè)置函數(shù)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;  //搶占優(yōu)先級設(shè)置62     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;            //次優(yōu)先級設(shè)置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); 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 }



關(guān)鍵詞: 中斷外部中

評論


技術(shù)專區(qū)

關(guān)閉