初中生
最后登录1970-1-1
在线时间 小时
注册时间2018-7-2
|
增加KEY2
bsp_exti.h
#ifndef _BSP_EXTI_H
#define _BSP_EXTI_H
#include "stm32f10x.h"
//KEY2宏定义
#define KEY1_INT_GPIO_PIN GPIO_Pin_0 //将KEY1对应的PIN宏定义,提高可读性
#define KEY1_INT_GPIO_PORT GPIOA
#define KEY1_INT_GPIO_CLK RCC_APB2Periph_GPIOA //将RCC.C文件中的APB2 CLK使能形参宏定义
//KEY2宏定义
#define KEY2_INT_GPIO_PIN GPIO_Pin_13 //将KEY2对应的PIN宏定义,提高可读性
#define KEY2_INT_GPIO_PORT GPIOC
#define KEY2_INT_GPIO_CLK RCC_APB2Periph_GPIOA //将RCC.C文件中的APB2 CLK使能形参宏定义
bsp_exti.c
void EXIT_Key_config(void);
void EXTI0_IRQHandler(void);
void EXTI15_10_IRQHandler(void);
#endif /*_BSP_EXTI_H*/
#include"bsp_exti.h"
static void EXTI_NVIC_Config(void) //初始化NVIC,类型为static,只能被EXIT_Key_config调用
{
NVIC_InitTypeDef NVIC_InitStruct;
//配置KEY1 NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置优先级分组
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; //配置channel
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //打开中断总开关
NVIC_Init(&NVIC_InitStruct); //调用NVIC 初始化函数
//配置KEY2 NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置优先级分组
NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn; //配置channel
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //打开中断总开关
NVIC_Init(&NVIC_InitStruct); //调用NVIC 初始化函数
}
void EXIT_Key_config(void) //首先初始化GPIO
{
GPIO_InitTypeDef GPIO_InitStruct; //定义初始化一个结构体,GPIO_InitStruct的名字可以任意,前面的GPIO_InitTypeDef对应头文件里面的结构体定义,所以格式命名不能随便修改
EXTI_InitTypeDef EXTI_InitStruct; //定义初始化EXTI结构体
//配置中断优先级
EXTI_NVIC_Config();
//初始化KEY1
RCC_APB2PeriphClockCmd(KEY1_INT_GPIO_CLK, ENABLE);//注意:时钟的初始化要放到GPIO函数初始化之前,否则初始化无效果,也就是说先打开时钟端口才可以初始化
GPIO_InitStruct.GPIO_Pin = KEY1_INT_GPIO_PIN ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //端口设置为输入时不需要设置速度
GPIO_Init(KEY1_INT_GPIO_PORT , &GPIO_InitStruct); //调用初始化函数
//初始化KEY2
RCC_APB2PeriphClockCmd(KEY2_INT_GPIO_CLK, ENABLE);//注意:时钟的初始化要放到GPIO函数初始化之前,否则初始化无效果,也就是说先打开时钟端口才可以初始化
GPIO_InitStruct.GPIO_Pin = KEY2_INT_GPIO_PIN ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //端口设置为输入时不需要设置速度
GPIO_Init(KEY2_INT_GPIO_PORT , &GPIO_InitStruct); //调用初始化函数
//初始化EXTI--KEY1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);//选择GPIO pin作为EXTI线
EXTI_InitStruct.EXTI_Line = EXTI_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
//初始化EXTI--KEY2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);//选择GPIO pin作为EXTI线
EXTI_InitStruct.EXTI_Line = EXTI_Line13;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct); //调用初始化函数初始化EXTI
}
// 1,初始化NVIC 2,初始化相关GPIO
it.c
void EXTI0_IRQHandler(void) //EXTI0中断函数
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
LED_G_TOGGLE;
}
EXTI_ClearITPendingBit(EXTI_Line0); //中断函数执行结束后清除中断标志位,退出中断
}
void EXTI15_10_IRQHandler(void) //EXTI15_10中断函数
{
if(EXTI_GetITStatus(EXTI_Line13) != RESET)
{
LED_G_TOGGLE;
}
EXTI_ClearITPendingBit(EXTI_Line13); //中断函数执行结束后清除中断标志位,退出中断
}
|
|