#include
#define uchar unsigned char
#define uint unsigned int
//宏定义
sbit P20= P2^0;
//端口位定义
//数码管段码定义
uchar SMG_CODE[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x88,0x83,0xC6,0xa1,0x86,0x8E,0xff};
uchar key_code = 16;
void delay()
{
int i;
for(i = 8; i< 120; i++);
}
//数码管显示数字初值为16是刚开始的显示全灭
//键盘扫描函数
uchar key_scan()
{
uchar key = 0,i,cr[2] =0,j= 0;
P3 = 0x0F; //P3口列值输入初始化if(P3 ?- 8XOF) 1/判断有没有键按下
if(P3!=0X0f)
{
delay();
if(P3 != 0X0F)
{
key = P3;
P3 = 0XF0;
key=key | P3;
while(P3 != 0xf0);
for(i=0;i<8;i++)
{
key=key>>1;
if(!CY)
{
cr[j++]=i ;
}
}
key_code =cr[0]+(cr[1]- 4)*4;//计算键值对应的数字
}
}
return key_code;
}
//返回键值对应的数字
void main()
{
P20= 0;
while(1)
{
P0=SMG_CODE[key_scan()];
}
//调用键盘扫描函数并输出对应的数字
}