标题:
stm32的4*4矩阵扫描按键驱动代码
[打印本页]
作者:
阿拉听听的天涯
时间:
2017-8-7 08:14
标题:
stm32的4*4矩阵扫描按键驱动代码
矩阵扫描,没有按键按下一直返回0 ,使用时最最好加判断和延时,不然太快了看不到
全部资料下载地址:
KEY.zip
(2.61 KB, 下载次数: 68)
2017-8-7 08:12 上传
点击文件名下载附件
下载积分: 黑币 -5
stm32单片机源程序:
#include "stm32f10x.h"
#include "key.h"
#include "sys.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK战舰STM32开发板
//按键驱动代码
//版本:V1.0
//版权所有,盗版必究。
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
/* 定义相关变量*/
#define GPIO_Pin_R (GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7)
#define GPIO_Pin_L (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3)
ErrorStatus HSEStartUpStatus;
u16 const Key_Tab[4][4]=//键盘编码表
{
{1,2,3,10},
{4,5,6,11},
{7,8,9,12},
{13,14,15,16}
};
/******************************************************************************************
name:void Delay_nus(u32 n)
function:延时Nus
parameter:n
return:无
*******************************************************************************************/
void Delay_nus(u32 n)
{
u32 j;
while(n--)
{ j=8;
while(j--);
}
}
/******************************************************************************************
name:void Delay_nms(u32 n)
function:延时Nms
parameter:n
return:无
*******************************************************************************************/
void Delay_nms(u32 n)
{
while(n--)
Delay_nus(1100);
}
//按键初始化函数
void KEY_Init(void) //IO初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//KEY0-KEY2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4
//初始化 WK_UP-->GPIOA.0 下拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
}
//按键处理函数
//返回按键值
//mode:0,不支持连续按;1,支持连续按;
//0,没有任何按键按下
//1,KEY0按下
//2,KEY1按下
//3,KEY2按下
//4,KEY3按下 WK_UP
//注意此函数有响应优先级,KEY0>KEY1>KEY2>KEY3!!
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;//按键按松开标志
if(mode)key_up=1; //支持连按
if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
{
delay_ms(10);//去抖动
key_up=0;
if(KEY0==0)return KEY0_PRES;
else if(KEY1==0)return KEY1_PRES;
else if(KEY2==0)return KEY2_PRES;
else if(WK_UP==1)return WKUP_PRES;
}else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;
return 0;// 无按键按下
}
u16 Get_KeyValue(void)//使用PF0~PF7
{
GPIO_InitTypeDef GPIO_InitStructure;
u8 i=5,j=5;
u16 temp1,temp2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_DeInit(GPIOF);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_L);//扫描列值
if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
return 0;
else
{
Delay_nms(70);//按键消抖
if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
return 0;
else
temp1=GPIO_ReadInputData(GPIOF)&0x00f0;
}
switch(temp1)
{
case 0x00e0:j=0;break;
case 0x00d0:j=1;break;
case 0x00b0:j=2;break;
case 0x0070:j=3;break;
default:break;
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_R);//扫描行值
if((GPIO_ReadInputData(GPIOF)&0x000f)==0x000f)
return 0;
else
{//这里不再延时再扫描,因为已经确定了不是抖动才会进入本步操作
temp2=GPIO_ReadInputData(GPIOF)&0x000f;
}
switch(temp2)
{
case 0x000e:i=0;break;
case 0x000d:i=1;break;
case 0x000b:i=2;break;
case 0x0007:i=3;break;
default:break;
}
if((i==5)||(j==5))
return 0;
else
return (Key_Tab[i][j]);
}
复制代码
作者:
seafly
时间:
2018-9-14 21:19
代码很好,多谢!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1