STM32外部中斷之二
1配置中斷 1、 分配中斷向量表: /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 2、 設置中斷優(yōu)先級: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //設置中斷優(yōu)先級 3、 初始化外部中斷: /*允許EXTI4中斷 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //中斷通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;//強占優(yōu)先級 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次優(yōu)先級 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中斷使能 NVIC_Init(&NVIC_InitStructure); //初始化中斷 注意:如果我們配置的外部針腳為PA4,或PB4,或PC4,PD4等,那么采用的外部中斷也必須是EXTI4,同樣,如果外部中斷針腳是PA1,PB1,PC1,PD1 那么中斷就要用EXTI1,其他類推。 2配置GPIO針腳作為外部中斷的觸發(fā)事件 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號,那么參數(shù)必須是GPIO_PinSource4 如果配置的針腳是3號,那么參數(shù)必須是GPIO_PinSource3 2、配置中斷邊沿 /*配置EXTI線0上出現(xiàn)下降沿,則產生中斷*/ EXTI_InitStructure.EXTI_Line = EXTI_Line4; 注意:如果配置的4號針腳,那么EXTI_Line4是必須的 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿觸發(fā) EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中斷線使能 EXTI_Init(&EXTI_InitStructure); //初始化中斷 EXTI_GenerateSWInterrupt(EXTI_Line4); //EXTI_Line4中斷允許 到此中斷配置完成,可以寫中斷處理函數(shù)。 舉例: 配置函數(shù) /************************************************************************* * 函數(shù)名 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); //設置中斷優(yōu)先級 /*允許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處理優(yōu)先級:優(yōu)先級以及子優(yōu)先級*/ } /************************************************************************ * 函數(shù)名 :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); } /************************************************************** * 函數(shù) SysTick_Configuration * 描述 設置SysTick * 輸入 無 * 輸出 無 * 返回值 無 ***************************************************************/ void SysTick_Configuration(void) { /*配置 HCLK 時鐘做為SysTick 時鐘源*/ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //系統(tǒng)時鐘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); } /****************************************************************************** * 函數(shù)名 EXTI_Configuration * 描述 配置EXTI線 * 輸入 無 * 輸出 無 * 返回值 無 ******************************************************************************/ void EXTI_Configuration(void){ /*將EXTI線0連接到PA0*/ GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4); /*配置EXTI線0上出現(xiàn)下降沿,則產生中斷*/ 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上出現(xiàn)下降沿產生中斷*/ 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); } 中斷函數(shù): 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時鐘 |
評論