#include <stc15.h>    	            //包含支持STC15W4K32S4单片机的头文件
#include <intrins.h>
#include <gpio.h>					 //I/O初始化文件
#define uchar unsigned char
#define uint  unsigned int
#include <display.h>
#define KEY P1
uchar key_volume;   //定义键值存放变量
void Delay10ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}
 /*----------------键盘扫描子程序-----------------------*/
uchar  keyscan()
{
 	uchar row,column;        //定义行、列变量
 	KEY=0x0f;                //先对 KEY置数 ,行全扫描
  	if(KEY!=0x0f)                //判断是否有键按下
  	{
    	Delay10ms();              //延时,软件去抖
    	if(KEY!=0x0f)   //确认按键按下;
     	{
      		KEY=0xef;     //0行扫描
			if(KEY!=0xef) 
			{
				row=0;
				goto colume_scan;
			}
			KEY=0xdf;     //1行扫描
			if(KEY!=0xdf) 
			{
				row=1;
				goto colume_scan;
			}
			KEY=0xbf;     //2行扫描
			if(KEY!=0xbf) 
			{
				row=2;
				goto colume_scan;
			}
		   KEY=0x7f;     //3行扫描
			if(KEY!=0x7f) 
			{
				row=3;
				goto colume_scan;
			}
			KEY=0xff;
			return(16);
colume_scan:
			if((KEY&0x01)==0)column=0;
				else if((KEY&0x02)==0)column=1;
					else if((KEY&0x04)==0)column=2;
						 else  column=3;
			key_volume=row*4+column;       	
     	}
  	}
   	else  
	KEY=0xff;
   	return (16);
}

/*--------------主程序------------------------------*/
main()
{
	gpio();
    KEY = 0xff;
	while(1)
	{
    	keyscan();
		Dis_buf[0]=key_volume;
		display();
	}
}
