標(biāo)題: STM32 外部中斷配置 [打印本頁(yè)]
作者: liuda 時(shí)間: 2015-1-23 03:48
標(biāo)題: STM32 外部中斷配置
1配置中斷
1、 分配中斷向量表:
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
2、 設(shè)置中斷優(yōu)先級(jí):
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //設(shè)置中斷優(yōu)先級(jí)
3、 初始化外部中斷:
/*允許EXTI4中斷 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;//強(qiáng)占優(yōu)先級(jí)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次優(yōu)先級(jí)
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號(hào)針腳,配置的中斷一定是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個(gè)針腳上
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4);
注意:如果配置的針腳是4號(hào),那么參數(shù)必須是GPIO_PinSource4
如果配置的針腳是3號(hào),那么參數(shù)必須是GPIO_PinSource3
2、配置中斷邊沿
/*配置EXTI線0上出現(xiàn)下降沿,則產(chǎn)生中斷*/
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
注意:如果配置的4號(hào)針腳,那么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中斷允許
到此中斷配置完成,可以寫(xiě)中斷處理函數(shù)。
舉例:
配置函數(shù)
/*************************************************************************
* 函數(shù)名 NVIC_Configration
* 描述 配置各個(gè)中斷寄存器
* 輸入 無(wú)
* 輸出 無(wú)
* 返回值 無(wú)
****************************************************************************/
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); //設(shè)置中斷優(yōu)先級(jí)
/*允許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)先級(jí):優(yōu)先級(jí)以及子優(yōu)先級(jí)*/
}
/************************************************************************
* 函數(shù)名 :GPIO_Configuration(void)
* 描述 :配置TIM2陣腳
* 輸入 :無(wú)
* 輸出 :無(wú)
* 返回 :無(wú)
************************************************************************/
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的第一個(gè)管角為浮動(dòng)輸入*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD,&GPIO_InitStructure);
/*配置GPIOB的第9個(gè)管腳為浮動(dòng)輸入*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
/**************************************************************
* 函數(shù) SysTick_Configuration
* 描述 設(shè)置SysTick
* 輸入 無(wú)
* 輸出 無(wú)
* 返回值 無(wú)
***************************************************************/
void SysTick_Configuration(void)
{
/*配置 HCLK 時(shí)鐘做為SysTick 時(shí)鐘源*/
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //系統(tǒng)時(shí)鐘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);//打開(kāi)中斷
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick_CounterCmd(SysTick_Counter_Clear);
}
/******************************************************************************
* 函數(shù)名 EXTI_Configuration
* 描述 配置EXTI線
* 輸入 無(wú)
* 輸出 無(wú)
* 返回值 無(wú)
******************************************************************************/
void EXTI_Configuration(void){
/*將EXTI線0連接到PA0*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4);
/*配置EXTI線0上出現(xiàn)下降沿,則產(chǎ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)下降沿產(chǎ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;
}
}
}
| 歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |