神舟IV學(xué)習(xí)筆記(八)外部中斷EXIT
外部中斷有點(diǎn)像51單片機(jī)的中斷INT一樣,都能夠設(shè)置為上升沿、下降沿和雙邊沿觸發(fā);不同之處在于STM32的大多數(shù)GPIO口都能設(shè)置為外部中斷輸入功能,這樣帶來的好處有:在PCB設(shè)計(jì)和需求大量外部中斷功能很有幫助。STM32F107VCT一共有5組GPIO,每組又有16個(gè)引腳。這里提供復(fù)用的方式提供了16個(gè)中斷源。PA0、PB0、PC0、PD0、PE0都是共有相同的中斷源EXIT0。
開發(fā)板上的按鍵對(duì)應(yīng)關(guān)系為:KEY1—PC4;KEY2—PB10;KEY3—PC13;KEY4—PA0。外接上拉電阻,在設(shè)置GPIO的狀態(tài)的時(shí)候,可以設(shè)置為輸入上拉模式。然后采用GPIO_EXTILineConfig庫(kù)函數(shù)將GPIO設(shè)置為EXTI模式。
/* Selects KEY1-PC.04 as EXTI Line 4 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4);
第三步是設(shè)置EXTI的模式,最后是編寫相應(yīng)的中斷函數(shù)。
需要注意的是中斷向量表中EXTI5到EXTI9是復(fù)用在一起的,名稱為EXTI9_5;
EXTI10到EXTI15是復(fù)用在一起的,名稱為EXTI15_10;
這部分主程序?yàn)椋?/p>
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the Priority Group to 2 bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the EXTI Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
/* Add your application code here
*/
LED_Configuration();
KEY_Configuration();//按鍵時(shí)鐘和端口配置
EXIT_Configuration();//外部中斷配置
NVIC_Configuration();
/* Infinite loop */
while (1)
{
}
}
void EXTI0_IRQHandler(void)/* Key 4 */
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
LED_ALL_OFF();
LED4_ON();
/* Clear the EXTI Line 0 */
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
/*******************************************************************************
* Function Name: EXTI4_IRQHandler
* Description: This function handles External interrupt Line 4 request.
* Input: None
* Output: None
* Return: None
*******************************************************************************/
void EXTI4_IRQHandler(void) /* Key 1 */
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET)
{
LED_ALL_OFF();
LED1_ON();
/* Clear the EXTI Line 4 */
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
/*******************************************************************************
* Function Name: EXTI15_10_IRQHandler
* Description: This function handles External lines 15 to 10 interrupt request.
* Input: None
* Output: None
* Return: None
*******************************************************************************/
void EXTI15_10_IRQHandler(void) /* Key 2 & 3 */
{
if(EXTI_GetITStatus(EXTI_Line10) != RESET)
{
LED_ALL_OFF();
LED2_ON();
/* Clear the EXTI Line 10 */
EXTI_ClearITPendingBit(EXTI_Line10);
}
if(EXTI_GetITStatus(EXTI_Line13) != RESET)
{
LED_ALL_OFF();
LED3_ON();
/* Clear the EXTI Line 13 */
EXTI_ClearITPendingBit(EXTI_Line13);
}
}