|
发表于 2010-3-30 13:46:14
|
显示全部楼层
以下是我写的按键判断核心部分
//
// key.h
//
#ifndef _H_KEY
#define _H_KEY
#include "main.h"
extern void Key_DoEvent(void);
#define KEY1 Key_CurValue,BIT(0)
#define KEY2 Key_CurValue,BIT(1)
#define KEY3 Key_CurValue,BIT(2)
#define KEY4 Key_CurValue,BIT(3)
#define KEY5 Key_CurValue,BIT(4)
typedef struct
{
u8 Tick;
u8 PreValue;
u8 RecValue;
} KEY_T;
extern KEY_T Key;
#endif
//
// key.c
//
#include "Key.h"
void Key_DoEvent(void)
{
//
// KEYn全部清零
//
u8 Key_CurValue = 0x00;
//
// 读取键值,低电平表示有键按下
//
if( READ(IO_KEY1)==0 ) FLAG_SET(KEY1);
if( READ(IO_KEY2)==0 ) FLAG_SET(KEY2);
if( READ(IO_KEY3)==0 ) FLAG_SET(KEY3);
if( READ(IO_KEY4)==0 ) FLAG_SET(KEY4);
//
// 稳态判断,按键状态不变维持40ms
//
if( Key_CurValue!=Key.PreValue )
{
Key.Tick = 0;
Key.PreValue = Key_CurValue;
return;
}
else
if( Key.Tick<MS(40) )
{
Key.Tick++;
return;
}
//
// 按键处理:允许多个按键被按下时,新按下的按键被响应
//
Key_CurValue ^= Key.RecValue; // 异或:获取和上一次有效按键的不同键值
Key_CurValue &= Key.PreValue; // 与:只获取新增按键值
Key.RecValue = Key.PreValue; // 保存这次有效键值
//
if( FLAG_CHK(KEY1) )
{
}
if( FLAG_CHK(KEY2) )
{
}
if( FLAG_CHK(KEY3) )
{
}
if( FLAG_CHK(KEY4) )
{
}
} |
|