STM32 外部中止装备
1装备中止 1、 分配中止向量表: /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 2、 设置中止优先级: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中止优先级 3、 初始化外部中止: /*答应EXTI4中止 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //中止通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;//侵占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中止使能 NVIC_Init(&NVIC_InitStructure); //初始化中止 留意:假如咱们装备的外部针脚为PA4,或PB4,或PC4,PD4等,那么选用的外部中止也有必要是EXTI4,相同,假如外部中止针脚是PA1,PB1,PC1,PD1 那么中止就要用EXTI1,其他类推。 2装备GPIO针脚作为外部中止的触发事情 1、 挑选IO针脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 留意,假如的针脚是端口的4号针脚,装备的中止一定是EXTI4 2、 装备针脚为输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 3、 初始化针脚 GPIO_Init(GPIOD,&GPIO_InitStructure); 3装备EXTI线,使中止线和IO针脚线连接上 1、 将EXTI线连接到IO端口上 将EXTI线4连接到端口GPIOD的第4个针脚上 GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4); 留意:假如装备的针脚是4号,那么参数有必要是GPIO_PinSource4 假如装备的针脚是3号,那么参数有必要是GPIO_PinSource3 2、装备中止边缘 /*装备EXTI线0上呈现下降沿,则发生中止*/ EXTI_InitStructure.EXTI_Line = EXTI_Line4; 留意:假如装备的4号针脚,那么EXTI_Line4是有必要的 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中止线使能 EXTI_Init(&EXTI_InitStructure); //初始化中止 EXTI_GenerateSWInterrupt(EXTI_Line4); //EXTI_Line4中止答应 到此中止装备完结,能够写中止处理函数。 举例: 装备函数 /************************************************************************* * 函数名 NVIC_Configration * 描绘 装备各个中止寄存器 * 输入 无 * 输出 无 * 回来值 无 ****************************************************************************/ void NVIC_Configration(void) { NVIC_InitTypeDef NVIC_InitStructure; //#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //#else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //#endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中止优先级 /*答应EXTI4中止 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*答应EXTI9中止*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*装备SysTick处理优先级:优先级以及子优先级*/ } /************************************************************************ * 函数名 :GPIO_Configuration(void) * 描绘 :装备TIM2阵脚 * 输入 :无 * 输出 :无 * 回来 :无 ************************************************************************/ void GPIO_Configuration(void){ /* GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); /*装备GPIOD的第一个管角为起浮输入*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD,&GPIO_InitStructure); /*装备GPIOB的第9个管脚为起浮输入*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB,&GPIO_InitStructure); } /************************************************************** * 函数 SysTick_Configuration * 描绘 设置SysTick * 输入 无 * 输出 无 * 回来值 无 ***************************************************************/ void SysTick_Configuration(void) { /*装备 HCLK 时钟做为SysTick 时钟源*/ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //体系时钟8分频 72MHz NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 8,2); /*SysTick Interrupt each 1000Hz with HCLK equal to 72MHz*/ SysTick_SetReload(9000);//中止周期1ms /*Enable the SysTick Interrupt */ SysTick_ITConfig(ENABLE);//翻开中止 SysTick_CounterCmd(SysTick_Counter_Enable); SysTick_CounterCmd(SysTick_Counter_Clear); } /****************************************************************************** * 函数名 EXTI_Configuration * 描绘 装备EXTI线 * 输入 无 * 输出 无 * 回来值 无 ******************************************************************************/ void EXTI_Configuration(void){ /*将EXTI线0连接到PA0*/ GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4); /*装备EXTI线0上呈现下降沿,则发生中止*/ EXTI_InitStructure.EXTI_Line = EXTI_Line4; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_GenerateSWInterrupt(EXTI_Line4); /*将EXTI线9连接到PB9上*/ GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9); /*将EXTI线9上呈现下降沿发生中止*/ EXTI_InitStructure.EXTI_Line = EXTI_Line9; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_GenerateSWInterrupt(EXTI_Line9); } 中止函数: void EXTI4_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line4)!= RESET){ EXTI_ClearITPendingBit(EXTI_Line4); if(Ledflag == 0){ Ledflag = 1; GPIOC->ODR |= 0X00000080; } else{ Ledflag = 0; GPIOC->ODR &= 0XFFFFFF7F; } } } 注:时钟设置的时分最好加上这句: RCCRCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);// 使能AFIO时钟 |