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); /* 处理按键松开事件 */
}
}
/**********************键盘处理********************************/
//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); /* 处理按键松开事件 */
}
}
相关文章推荐
- c51上跑smallrtos 之lcd1602任务
- c51上跑smallrtos 之ds1302 存取日期
- 芯片启动过程 ARM C51 Soc - RTOS LINUX - 之一
- 嵌入式实时抢占操作系统PT(preemption thread)RTOS——任务设计(3)
- 基于TI-RTOS的CC2650DK开发(8)---任务
- 51单片机的4x4矩阵键盘扫描例程(C51)
- 实验1:small rtos51-------led控制
- C51中4X4键盘扫描(非反转法)
- C51 单片机矩阵键盘
- RTOS环境下多任务编程要点
- 再发一种基于stm32的4X4矩阵键盘定时器扫描消抖法,彻底告别delay函数! .
- RTOS下不同任务传值的方式
- RTOS姊妹花——Small RTOS与STOS++简介
- RTOS(2):任务状态转变
- 一、TRTOS的任务创建与注销方式
- RTOS基本原理:多任务和调度-FreeRTOS是如何工作的
- 嵌入式实时抢占操作系统PT(preemption thread)RTOS——任务状态和任务控制块(2)
- small rtos1.20.3的一点小问题
- 《建立一个属于自己的AVR的RTOS》笔记——任务调度对比
- RTOS学习之:多任务编程要点