#include
#include
#define uchar unsigned char
#define uint unsigned int
unsigned char code seg7[16]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};//0到f的字型码
unsigned char code hao[4]={0x0e,0x0d,0x0b,0x07};
unsigned char code key_[16]={1,2,3,15,4,5,6,14,7,8,9,13,0,10,11,12}; //决定了键盘按键布局
//======================================
void delay(uint n)        //延时程序
{
    while(n--)
    {_nop_();}
}
//======================================
//======================================
uchar scan()             // 键盘扫描程序
{
    uchar temp,h,j,i,high,low;
    bit find=0;           //定义位find标志 并赋值零
    P0=0xf0; 
    temp=P0;           //行低电平 列高电平
    if(temp!=0xf0)      //检测是否有键按下
    {
        delay(1000);      //去抖
        if(temp!=0xf0) //确定有键按下
       {
            find=1;          //标志有键按下
            high=(temp>>4); //将列的状态存入high
            P0=0x0f;        //翻转行列电平
            temp=P0;        //再次读取P0
            low=(temp&0x0f); //将行的状态存入low
            for(i=0;i<4;i++) //确定按键的坐标
            {
                if(hao[i]==high){j=i;}
                if(hao[i]==low){h=i;}
            }
        }
     }
     if(find==0){return 16;}
       else return(4*h+j);
}
//=============================================
void main()
{
    uchar a,key=0;
    while(1)
    {
        a=scan();
        if(a!=16)
        {
            key=key_[a];
        }
     P1=seg7[key];
     P2=0xfe;
     delay(200);
     }
}