您的位置 首页 汽车

中止之外部中止

ARMCortexM3共有256个中断。STM32F103CB共有43个外部中断和16个内部中断。具有相同数值编号的IO口被分配为同一组,共用一条中断请求线…

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>中止线上发生了挑选的边缘事情时置119位

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() 和 NV%&&&&&%_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); 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 }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/265524.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部