#ifndef __4x4key__H_
#define __4x4key__H_

#include "regx52.h"
#define uchar unsigned char
#define uint unsigned int

uchar programflag=0;	//不同程序切换标志	
uint disnum;
bit keytime_10ms_ok=0; //扫描时间

void matrixkey_csan()
{
	static uchar keystate=0;				//键状态
	static uchar keybuf1=0;			//键值缓存1
	static uchar keybuf=0;
 				//键值缓存
   	switch(keystate)				//判断状态
	 {
	  case 0:						//状态0
      P3=0X0F;						//赋0X0F给P2
      if(P3!=0x0f)					//判断有无按下
	   {					   
	    keystate=1;					//按下状态为1
	   }
	   break;						//跳出SWITCH语句
		
	
	
	  case 1:						//状态1
	  if(P3!=0x0F)					//再判断有无按下
	   {
	    keystate=2;					//有则改为状态2
		keybuf1=P3;				//将当前键值给缓存1
		P3=0xf0;					//交换行列线电平
		keybuf=P3;				//再将第二键值赋给缓存0
	    }
				
	   else
	    {
		keystate=0;					//如果没有按下 则返回状态1
		}
		
		break;						//跳出SWITCH语句

	  case 2:						//状态2

	  P3=0xf0;
	  if(P3==0xf0)					//判断松开了吗
	   {
	   	keystate=0;				   //松开则状态改为0  


						//********************//键值处理

	  keybuf=keybuf|keybuf1;		//2个键值合并
	  keybuf=~keybuf;				//键值取反

	  			switch(keybuf)					//判断键值
	   				{
				   	case 0x11 :programflag=1;break;			//1
				   	case 0x21 :programflag=2;break;			//2
				   	case 0x41 :programflag=3;break;			//3
				   	case 0x81 :programflag=4;break;			//。。
					case 0x12 :programflag=5;break;			//。。
					case 0x22 :programflag=6;break;
				   	case 0x42 :programflag=7;break;
				   	case 0x82 :programflag=8;break;
				   	case 0x14 :programflag=9;break;
					case 0x24 :programflag=0;break;			//0
	   				}
	 }	 
	 break;
	}	   
}   											 





#endif
