加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
exti_key.c 4.03 KB
一键复制 编辑 原始数据 按行查看 历史
think3r 提交于 2017-05-21 20:19 . 上传
#include "exti_key.h"
#include "../STM32_FWLIB/inc/stm32f10x_exti.h"
#include "../HardWare/include.h"
#include "./ch376s/HOST.H"
static void KEY_Init(void) //IO初始化
{
//定义初始化gpio的结构体
GPIO_InitTypeDef GPIO_initstruct;
//初始化端口时钟,使能POARTA & POARTC 的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
//初始化按键 key0 PC1 上拉输入'
GPIO_initstruct.GPIO_Speed=
GPIO_Speed_2MHz;
GPIO_initstruct.GPIO_Pin = GPIO_Pin_1;
GPIO_initstruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_initstruct);
//初始化按键 key1 PC13 上拉输入
GPIO_initstruct.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOC,&GPIO_initstruct);
//初始化按键 wk_up PA0 下拉输入
GPIO_initstruct.GPIO_Mode = GPIO_Mode_IPD;
GPIO_initstruct.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA,&GPIO_initstruct);
}
void EXTIX_Key_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
KEY_Init(); // 按键端口 GPIO 初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
//----------------------------------------------------------------------------------------
//GPIOC.1 key0 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
//GPIOC.13 key1 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_Init(&EXTI_InitStructure);
//GPIOA.0 WK_UP 中断线以及中断初始化配置 上升沿触发 PA0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
//----------------------------------------------------------------------------
//GPIOC.1 key0
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //使能按键key0所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
//GPIOC.13 key1
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能按键key1所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
//GPIOA.0 WK_UP
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键WK_UP所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
printf("按键中断初始化成功!\r\n");
}
//外部中断 1 服务程序 按键 key0
void EXTI1_IRQHandler(void)
{
delay_ms(20);//消抖
if(KEY0==0) //按键KEY2
{
led0 = !led0;
printf("按键 key0 被按下。\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line1); //清除LINE1上的中断标志位
}
//外部中断 13 服务程序 按键 key1
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13))
{
delay_ms(20);
if(KEY1==0)
{
led1 = !led1;
printf("按键 KEY1 被按下。\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line13); //清除LINE13上的中断标志位
}
}
//外部中断 0 服务程序 按键WK_UP
void EXTI0_IRQHandler(void)
{
delay_ms(5);//消抖
if(WK_UP==1) //WK_UP按键
{
SafeRemoveDisk();
printf("按键 WK_UP 被按下。\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line0); //清除LINE0上的中断标志位
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化