小学生
最后登录1970-1-1
在线时间 小时
注册时间2020-10-28
|
exti.c程序
#include "bsp_exti.h"
static void EXTI_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStruct; //定义NVIC结构体
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 );//设置中断优先级分组,选择Group_1
NVIC_InitStruct.NVIC_IRQChannel = KEY1_INT_EXTI_IRQ ; //设置中断源,按键1,配置结构体中的每个成员进行初始化操作
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;//配置抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //配置子优先级
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能开总中断
NVIC_Init(&NVIC_InitStruct); //初始化结构体
//配置中断源,按键2,其他使用上面的相关配置。
NVIC_InitStruct.NVIC_IRQChannel = KEY2_INT_EXTI_IRQ ;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;//配置抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //配置子优先级
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void EXTI_Key_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
//配置NVIC中断
EXTI_NVIC_Config();
/*-------------------------------------------KEY1配置----------------------------------------------------*/
//初始化GPIO
RCC_APB2PeriphClockCmd(KEY1_INT_GPIO_CLK , ENABLE); //开启按键GPIO口的时钟
GPIO_InitStruct.GPIO_Pin = KEY1_INT_GPIO_PIN ; //选择按键用到的gpio的引脚pin
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//按键为输入,设置为浮空输入,默认为低电平
GPIO_Init(KEY1_INT_GPIO_PORT, &GPIO_InitStruct); //选择gpio端口,在gpio初始化函数里面
//初始化exti
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //exti的时钟也在APB2总线上
GPIO_EXTILineConfig( GPIO_PortSourceGPIOA, GPIO_PinSource0);//选择输入线EXTILine,选择GPIOA,Pin选择PinSouce0,已经进行了宏定义
EXTI_InitStruct.EXTI_Line = EXTI_Line0; //线配置为line0
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断模式
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;//设置边沿触发方式,上升沿触发
EXTI_InitStruct.EXTI_LineCmd = ENABLE;//打开使能中断屏蔽器
EXTI_Init(&EXTI_InitStruct);//调用EXTI_Init固件库函数,把结构体成员写到相应的寄存器中
/*-------------------------------------------KEY2配置----------------------------------------------------*/
RCC_APB2PeriphClockCmd(KEY2_INT_GPIO_CLK , ENABLE); //开启按键GPIO口的时钟
GPIO_InitStruct.GPIO_Pin = KEY2_INT_GPIO_PIN ; //选择按键用到的gpio的引脚pin
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//按键为输入,设置为浮空输入,默认为低电平
GPIO_Init(KEY2_INT_GPIO_PORT, &GPIO_InitStruct); //选择gpio端口,在gpio初始化函数里面
//初始化exti
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //exti的时钟也在APB2总线上
GPIO_EXTILineConfig( GPIO_PortSourceGPIOC, GPIO_PinSource13);//选择输入线EXTILine,选择GPIOA,Pin选择PinSouce0,已经进行了宏定义
EXTI_InitStruct.EXTI_Line = EXTI_Line0; //线配置为line0
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断模式
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;//设置边沿触发方式,上升沿触发
EXTI_InitStruct.EXTI_LineCmd = ENABLE;//打开使能中断屏蔽器
EXTI_Init(&EXTI_InitStruct);//调用EXTI_Init固件库函数,把结构体成员写到相应的寄存器中
}
exti.c程序
#ifndef __BSP_EXTI_H
#define __BSP_EXTI_H
#include "stm32f10x.h"
/*进行宏定义,方便移植和调用*/
#define KEY1_INT_GPIO_PIN GPIO_Pin_0
#define KEY1_INT_GPIO_PORT GPIOA
#define KEY1_INT_GPIO_CLK (RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO) //外设时钟
#define KEY1_INT_EXTI_PORTSOURCE GPIO_PortSourceGPIOA
#define KEY1_INT_EXIT_PINSOUCE GPIO_PinSouce0
#define KEY1_INT_EXTI_LINE EXTI_Line0
#define KEY1_INT_EXTI_IRQ EXTI0_IRQn
#define KEY1_IRQHandler EXTI0_IRQHandler
//加上一个key2,同样进行宏定义
#define KEY2_INT_GPIO_PIN GPIO_Pin_13
#define KEY2_INT_GPIO_PORT GPIOC
#define KEY2_INT_GPIO_CLK (RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO) //外设时钟
#define KEY2_INT_EXTI_PORTSOURCE GPIO_PortSourceGPIOC
#define KEY2_INT_EXIT_PINSOUCE GPIO_PinSouce13
#define KEY2_INT_EXTI_LINE EXTI_Line13
#define KEY2_INT_EXTI_IRQ EXTI15_10_IRQn //注意用的是 EXTI15_10_IRQ ,不是 EXTI13
#define KEY2_IRQHandler EXTI15_10_IRQHandler
it.c程序
void EXTI_Key_Config(void);
#endif /*__BSP_EXTI_H*/
void SysTick_Handler(void)
{
}
void KEY1_IRQHandler (void)
{
if(EXTI_GetITStatus(EXTI_Line0 )!=RESET) //不等于0就执行确保产生了中断
{
LED_G_TOGGLE; //LED_G取反
EXTI_ClearITPendingBit(EXTI_Line0);//清除中断标志位
}
}
void KEY2_IRQHandler (void)
{
if(EXTI_GetITStatus(EXTI_Line13 )!=RESET) //不等于0就执行确保产生了中断
{
LED_R_TOGGLE; //LED_R取反
EXTI_ClearITPendingBit( EXTI_Line13 );//清除中断标志位
}
}
|
|