新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STM32 外部中斷_ 外部中斷和事件

STM32 外部中斷_ 外部中斷和事件

作者: 時間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
1.GPIO 的正確設(shè)置
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOD clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

/* Configure PD.03, PC.04, as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3); //PD3作為外部中斷引腳

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

/**
* @brief Selects the GPIO pin used as EXTI Line.
* @param GPIO_PortSource: selects the GPIO port to be used as source for EXTI lines.
* This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).
* @param GPIO_PinSource: specifies the EXTI line to be configured.
* This parameter can be GPIO_PinSourcex where x can be (0..15).
* @retval None
*/
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)

2.中斷初始化
EXTI_InitTypeDef EXTI_InitStructure;

// Configure EXTI Line9 to generate an interrupt on falling edge
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource4);

// Configure EXTI Line9 to generate an interrupt on falling edge
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);
3.在NVIC_Configuration 中Configures the NVIC and Vector Table base address.

/* Configure the Priority Group to 2 bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* Enable the EXTI3 Interrupt on PD.3 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the EXTI4 Interrupt on PD.4 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
4.在stm32f0x_it.c 中寫入 中斷處理函數(shù)
/*******************************************************************************
* Function Name : EXTI3_IRQHandler
* Description : This function handles External interrupt Line 3 request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
// Led_RW_ON();
GPIO_SetBits(GPIOC, GPIO_Pin_4| GPIO_Pin_5 );


/* Clear the EXTI line 9 pending bit */
EXTI_ClearITPendingBit(EXTI_Line3);
}
}

/*******************************************************************************
* Function Name : EXTI4_IRQHandler
* Description : This function handles External interrupt Line 4 request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET)
{
//Led_RW_OFF();
GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5);
/* Clear the EXTI line 9 pending bit */
EXTI_ClearITPendingBit(EXTI_Line4);
}
}

=====================================================

STM32的“外部中斷”和“事件”怎么理解?

事件是中斷的觸發(fā)源,開放了對應(yīng)的中斷屏蔽位,則事件可以觸發(fā)相應(yīng)的中斷。

事件還是其它一些操作的觸發(fā)源,比如DMA,還有TIM中影子寄存器的傳遞與更新;而中斷是不能觸發(fā)這些操作的,所以要把事件與中斷區(qū)分開。

參考下圖:當(dāng)你只要產(chǎn)生中斷而不想觸發(fā)其它操作時,就可以用事件屏蔽寄存器實(shí)現(xiàn)。




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

評論


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

關(guān)閉