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

本文介绍了一个基于C51的小型RTOS中4x4矩阵键盘任务的实现细节,包括键盘扫描函数、键盘驱动任务流程及按键处理逻辑。通过定时扫描键盘状态,实现了按键的去抖动、闭合与释放事件的处理。

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);                           /* 处理按键松开事件 */
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值