arm7 键盘扫描程序

/****************************************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
***************************************************************************************/

  

转载于:https://www.cnblogs.com/flonlen/archive/2011/08/17/arm7_KeyScan.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值