您的位置:首页 > 其它

c51上跑smallrtos 之4x4 矩阵键盘任务

2017-08-01 11:29 162 查看
c51上跑smallrtos 之4x4 矩阵键盘任务

/**********************键盘处理********************************/
//uint8 const KeyDownCommnd[17] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
//uint8 const KeyUpCommnd[17] = {0,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
//uint8 const KeyInDownCommnd[17] = {0,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48};

//保存当前闭合的键的位图
uint8 const KeyNameArray[17] = {0,'D','C','B','A','#','9','6','3','0','8','5','2','*','7','4','1'};

/*********************************************************************************************************
** 函数名称: KeyScan
** 功能描述: 获取当前瞬间闭合按键的位图
** 输 入: 无
** 输 出: 闭合按键的位图
** 全局变量: KeyBordData
** 调用模块: 无

uint8 KeyBordData;
uint8 KeyScan(void)
{
return KeyBordData;
}
********************************************************************************************************/

/*********************************************************************************************************
** 函数名称: KeyInput
** 功能描述: 键盘驱动任务
** 输 入: 无
** 输 出: 无
** 全局变量: CommandData
** 调用模块: OSQCreate(),OSWait(),Key(),KeyScan()

********************************************************************************************************/

uint8 KeyboardScan4x4(void)
{

static uint8 key;
static uint8 temp;

temp = 0;
P0=0xF0; //高四位输入 行为高电平 列为低电平

if(P0!=0xF0)
{
temp=P0; //读P口
temp=temp&0xF0; //屏蔽低四位
temp=~((temp>>4)|0xF0);
//1248->1 2 3 4
if(temp==8)
key=4;
else if(temp==4)
key=3;
else
key= temp ;

P0=0x0F; //低四位输入 列为高电平 行为低电平

if(P0!=0x0F)
{
temp=P0; //读P口
temp=temp&0x0F;
temp=~(temp|0xF0);
//1248->0 4 8 12
if(temp==8)
key+=12;
else if(temp==4)
key+=8;
else if(temp==2)
key+=4;
else
key+=0;

//键值缓存 1-16
return (key & 0x1f);
}else{
return 0;
}
}else{
return 0;
}
}

void KeyInput(void)
{
static uint8 i;
static uint8 temp;

//OSQCreate(CommandData,16); /* 创建主任务使用的消息队列 */
while (1)
{
OSWait(K_TMO,(OS_TICKS_PER_SEC / 50) + 1); /* 延时 20ms */
temp = KeyboardScan4x4(); /* 获取闭合按键位图 */
if (temp == NO_KEY)
{
continue;
}

OSWait(K_TMO,OS_TICKS_PER_SEC / 50); /* 去抖(延时 20ms)*/
if (temp != KeyboardScan4x4())
{
continue;
}
OSQPost(CommandData,temp);
//Key(temp,KEY_DOWN); /* 处理按键闭合事件 */
/* 第一次连击延时 */
i = KEY_START / (OS_TICKS_PER_SEC / 50);
do
{
OSWait(K_TMO,OS_TICKS_PER_SEC / 50);
if (temp != KeyboardScan4x4())
{
goto KeyEnd; /* 按键松开 */
}
} while (--i != 0);

/* 后续连击延时 */
while (1)
{
OSQPost(CommandData,temp+32);
//Key(temp,KEY_ALWAYS); /* 处理按键连击事件 */

i = KEY_DELAY / (OS_TICKS_PER_SEC / 50);
do
{
OSWait(K_TMO,OS_TICKS_PER_SEC / 50);
if (temp != KeyboardScan4x4())
{
goto KeyEnd; /* 按键松开 */
}
} while (--i != 0);
}

KeyEnd:
OSQPost(CommandData,temp+16);
//Key(temp,KEY_UP); /* 处理按键松开事件 */
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: