神舟IV学习笔记(八)外部中断EXIT
扫描二维码
随时随地手机看文章
外部中断有点像51单片机的中断INT一样,都能够设置为上升沿、下降沿和双边沿触发;不同之处在于STM32的大多数GPIO口都能设置为外部中断输入功能,这样带来的好处有:在PCB设计和需求大量外部中断功能很有帮助。STM32F107VCT一共有5组GPIO,每组又有16个引脚。这里提供复用的方式提供了16个中断源。PA0、PB0、PC0、PD0、PE0都是共有相同的中断源EXIT0。
开发板上的按键对应关系为:KEY1—PC4;KEY2—PB10;KEY3—PC13;KEY4—PA0。外接上拉电阻,在设置GPIO的状态的时候,可以设置为输入上拉模式。然后采用GPIO_EXTILineConfig库函数将GPIO设置为EXTI模式。
/* Selects KEY1-PC.04 as EXTI Line 4 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4);
第三步是设置EXTI的模式,最后是编写相应的中断函数。
需要注意的是中断向量表中EXTI5到EXTI9是复用在一起的,名称为EXTI9_5;
EXTI10到EXTI15是复用在一起的,名称为EXTI15_10;
这部分主程序为:
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();//按键时钟和端口配置
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);
}
}