打印
[应用相关]

STM32之中断与事件---一个使用GPIO作为外部中断的示例

[复制链接]
1579|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
paotangsan|  楼主 | 2018-10-11 15:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原文网址:https://blog.csdn.net/flydream0/article/details/8209042

1.GPIO 的正确设置

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作为外部中断引脚



其中GPIO_EXITLineConfig函数的声明如下:


/**
* @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)


沙发
paotangsan|  楼主 | 2018-10-11 15:26 | 只看该作者
2.中断初始化
EXTI_InitTypeDef EXTI_InitStructure;

// Configure EXTI Line3 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 Line4 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);



使用特权

评论回复
板凳
paotangsan|  楼主 | 2018-10-11 15:26 | 只看该作者
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);



使用特权

评论回复
地板
paotangsan|  楼主 | 2018-10-11 15:27 | 只看该作者
4.在stm32f0x_it.c 中写入 中断处理函数


/*******************************************************************************
* 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);
    }
}


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

53

主题

4119

帖子

0

粉丝