//按键端口定义,有些电路是上拉,要特别注意原理图sch
//KEY1 : PB12 10K pull down 高电平有效
//KEY2 : PB13 10K pull down 高电平有效
//KEY3 : PB14 10K pull down 高电平有效
//KEY4 : PB15 10K pull down 高电平有效
//
- #ifndef __KEY_H
- #define __KEY_H
- #include "stm32f10x.h"
- #include "delay.h"
- typedef enum
- {
- KEY_NONE = 0X00, //
- KEY_UP = 0X10, //
- KEY_DOWN = 0x20, //
- KEY_LEFT = 0x40, //
- KEY_RIGHT = 0x80
- } KeyValue;
-
- #define KEY1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12)
- #define KEY2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)
- #define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)
- #define KEY4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)
- void KEY_Init(void);
- KeyValue KEY_Scan(void);
- #endif
复制代码 下面是C文件:
- #include "key.h"
- void KEY_Init(void) //IO初始化
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure; //初始化KEY
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // pull-DOWN input
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- KeyValue KEY_Scan(void)
- {
-
- if(KEY1==1||KEY2==1||KEY3==1||KEY4==1) //任意一个按键按下
- {
- delay_ms(10); //去抖动
-
- if(KEY1==1)
- {
- return KEY_UP; //keyvalue in your app
- }
- else if(KEY2==1)
- {
- return KEY_DOWN; //keyvalue in your app
- }
- else if(KEY3==1)
- {
- return KEY_LEFT; //keyvalue in your app
- }
- else if(KEY4==1)
- {
- return KEY_RIGHT; //keyvalue in your app
- }
- }
- else //if(KEY1&&KEY2)
- return (KEY_NONE);// 无按键按下
- return KEY_NONE;
- }
复制代码 main函数调用:
- KeyValue KeyCurrentValue = KEY_NONE;
-
- KeyCurrentValue = KEY_Scan();
- if(KeyCurrentValue==KEY_UP)printf("######## The KEY UP is OK ! \r\n");
- if(KeyCurrentValue==KEY_DOWN)printf("######## The KEY DOWN is OK ! \r\n");
- if(KeyCurrentValue==KEY_LEFT)printf("######## The KEY LEFT is OK ! \r\n");
- if(KeyCurrentValue==KEY_RIGHT)printf("######## The KEY RIGHT is OK ! \r\n");
复制代码 哈哈,很简洁,很好用,任何mcu都可以直接用!
|
key.zip
2015-1-13 09:00 上传
点击文件名下载附件
1.22 KB, 下载次数: 10
哈哈,我看看你都发了那些垃圾帖子。。。你还好意思说
无用,楼下保持队形
有用也好无用也罢,你们纯属娱乐
我的目的是分享,即使没有也没碍着你呀,呵呵