stm32 外部中断

本文详细介绍了STM32微控制器外部中断的配置过程,包括GPIO与外部中断的连接配置、外部中断初始化及中断服务函数的编写。通过具体实例展示了如何使能时钟、配置IO口为输入模式,并正确配置外部中断线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STM32模块操作常用的方式,先配置GPIO与相对模块对接,然后对模块配置;同样在使用外部中断时,我们也是先配置io,然后配置中断,具体配置操作如下:
1. 外部中断配置,
配置相关的结构体,有三如下:
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
相关的头文件
#include “stm32f0xx_exti.h”
#include “stm32f0xx_syscfg.h”
#include “stm32f0xx_misc.h”
#include “stm32f0xx_gpio.h”

void Exti_intconfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//configure clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Configure PORTB pin as input pp */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Connect EXTI0 Line to PA0 pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource7);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource9);

/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_Init(&EXTI_InitStructure);

/* Enable and set EXTI0 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_15_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* Generate software interrupt: simulate a falling edge applied on Key Button EXTI line */ 
EXTI_GenerateSWInterrupt(EXTI_Line7|EXTI_Line9); 
//EXTI_GenerateSWInterrupt(EXTI_Line0);

}
有没有发现,我刚开始是用EXTI_Line0,但是我IO用 GPIO_Pin_7|GPIO_Pin_9,导致我中断无法产生,这里需要说明一点是配置EXTI_Line时,需要与gpio pin相对应,否则无法看到需要的效果。

  1. 在中断函数中增加处理.
    在stm32f0xx.h中查看EXTI_Line9和EXTI_Line7属于EXTI4_15_IRQn中断号。然后到中断文件中增加相应函数。
    中断默认在#include “stm32f0xx_it.c”
    void EXTI4_15_IRQHandler(void)//EXTI0_1_IRQn
    {
    static u8 i;

    if(EXTI_GetITStatus(EXTI_Line7) != RESET)
    {
    /* Toggle LED2 */
    if (i == 0)
    {
    i = 1;
    LED1_OFF;
    }
    else
    {
    i = 0;
    LED1_ON;
    }

    /* Clear the EXTI line 0 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line7);
    }

    if(EXTI_GetITStatus(EXTI_Line9) != RESET)
    {
    /* Toggle LED2 */
    if (i == 0)
    {
    i = 1;
    LED1_OFF;
    }
    else
    {
    i = 0;
    LED1_ON;
    }
    //printf(“EXTI0_1_IRQHandler”);

    /* Clear the EXTI line 0 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line9);
    }

  2. 设置完成后,在main里初始化一下,便可以实现相应中断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值