/****************************************Copyright (c)**************************************************
** driver program for lpc2100
**
**--------------文件信息--------------------------------------------------------------------------------
** 文件名: key.c
** 创建人: 林傅荣
** 日 期: 2011.8.4
** 描 述: 按键相关函数的实现
**
**--------------当前版本修订------------------------------------------------------------------------------
** 修改人:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include "config.h"
//Key_GPIO
#define K1 8
#define K2 9
#define K3 10
#define K4 11
#define K5 12
#define K7 13
#define K6 14
#define K8 15
int8u KeyScanLine(int8u column);
//按键初始化
void KeyInit(int8u stat)
{
if(0 == stat)
{
IO0DIR = IO0DIR & (~((1 << K1) |
(1 << K2) |
(1 << K3) |
(1 << K4) |
(1 << K5) |
(1 << K6) |
(1 << K7) |
(1 << K8)));
}
else
{
IO0DIR = IO0DIR & (~((1 << K5) |
(1 << K6) |
(1 << K7) |
(1 << K8)));
IO0DIR = IO0DIR | (1 << K1) |
(1 << K2) |
(1 << K3) |
(1 << K4);
}
}
//按键扫描
int8u KeyScan(int8u stat)
{
int32u KeyGPIO;
int8u key = 0;
if(0 == stat)
{
KeyGPIO = ((1 << K1) |
(1 << K2) |
(1 << K3) |
(1 << K4) |
(1 << K5) |
(1 << K6) |
(1 << K7) |
(1 << K8));
if((KeyGPIO & IO0PIN) != KeyGPIO)
{
OSTimeDly(50);
if((KeyGPIO & IO0PIN) != KeyGPIO)
{
if(0 == (IO0PIN & (1 << K1)))
key = 1;
else if(0 == (IO0PIN & (1 << K2)))
key = 2;
else if(0 == (IO0PIN & (1 << K3)))
key = 3;
else if(0 == (IO0PIN & (1 << K4)))
key = 4;
else if(0 == (IO0PIN & (1 << K5)))
key = 5;
else if(0 == (IO0PIN & (1 << K6)))
key = 6;
else if(0 == (IO0PIN & (1 << K7)))
key = 7;
else if(0 == (IO0PIN & (1 << K8)))
key = 8;
while((KeyGPIO & IO0PIN) != KeyGPIO);
}
}
}
else
{
IO0CLR = (1 << K1);
IO0SET = (1 << K2) |
(1 << K3) |
(1 << K4);
key = KeyScanLine(1);
if(key != 0)
return key;
IO0CLR = (1 << K2);
IO0SET = (1 << K1) |
(1 << K3) |
(1 << K4);
key = KeyScanLine(2);
if(key != 0)
return key;
IO0CLR = (1 << K3);
IO0SET = (1 << K1) |
(1 << K2) |
(1 << K4);
key = KeyScanLine(3);
if(key != 0)
return key;
IO0CLR = (1 << K4);
IO0SET = (1 << K1) |
(1 << K2) |
(1 << K3);
key = KeyScanLine(4);
if(key != 0)
return key;
}
return key;
}
//扫描每行
int8u KeyScanLine(int8u column)
{
int8u key = 0;
if(0 == (IO0PIN & (1 << K5)))
{
key = (column-1)*4 + 1;
while(0 == (IO0PIN & (1 << K5)));
}
else if(0 == (IO0PIN & (1 << K6)))
{
key = (column-1)*4 + 2;
while(0 == (IO0PIN & (1 << K6)));
}
else if(0 == (IO0PIN & (1 << K7)))
{
key = (column-1)*4 + 3;
while(0 == (IO0PIN & (1 << K7)));
}
else if(0 == (IO0PIN & (1 << K8)))
{
key = (column-1)*4 + 4;
while(0 == (IO0PIN & (1 << K8)));
}
return key;
}
/***************************************************************************************
** End File
***************************************************************************************/