首页 > 程序开发 > 综合编程 > 其他综合 >

stm32外部中断

2017-01-16

stm32外部中断:STM32模块操作常用的方式,先配置GPIO与相对模块对接,然后对模块配置;同样在使用外部中断时,我们也是先配置io,然后配置中断,具体配置操作如下

stm32外部中断: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相对应,否则无法看到需要的效果。

在中断函数中增加处理.
在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);
}
设置完成后,在main里初始化一下,便可以实现相应中断。
相关文章
最新文章
热点推荐