标题: 51单片机矩阵键盘数据存储及数码管显示 [打印本页]

作者: zhangsir0    时间: 2019-12-3 11:18
标题: 51单片机矩阵键盘数据存储及数码管显示
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
uchar temp,j,b,count=0,flag=0;//全局变量用于表示按键个数
        
//函数声明
void delay(uchar z);
void delay1();
void display(uchar i);
uchar keyscan(void);

uchar a[]={0,0,0,0,0};//用于暂存按键
/*****************键盘***********************
                                0 1 2 3
                                4 5 6 7
                                8 9 A B
                                C D E F
*********************************************/
uchar code table[]=
{
        0x3f,0x06,0x5b,0x4f,
        0x66,0x6d,0x7d,0x07,
        0x7f,0x6f,0x77,0x7c,
        0x39,0x5e,0x79,0x71,
};

//延时函数
void delay(unsigned char i)
{
        unsigned char j,k;
  for(j=i;j>0;j--)
    for(k=125;k>0;k--);
}

void delay1()//按键延时消抖函数
{
        uchar i,j;
        for(i=20;i>0;i--)
        for(j=200;j>0;j--);
}
void display(uchar i)
{
        for(b=0;b<i;b++)//动态扫描
        {
                for(j=4;j>0;j--)
                {
                        P0=table[a[j]];
                        dula=1;
                        dula=0;
                        P0=_cror_(temp,j-1);//数组角标表示移了多少位数
                        wela=1;
                        wela=0;
                        delay(10);
                }
        }
}
//扫描函数
uchar keyscan(void)
{
        uchar sccode,recode,key;
        P3=0xf0;
        if((P3&0xf0)!=0xf0)                                                   //检测按键按下
        {
                delay1();
                while((P3&0xf0)!=0xf0)                                  //再次检测并循环直到按键松下
                {
                        sccode=0xfe;                                          //11111110
                        while((sccode&0x10)!=0)                          //00010000
                        {
                                P3=sccode;
                                if((P3&0xf0)!=0xf0)
                                {
                                        recode=P3&0xf0;                          //分别读取高四位和第四位
                                        sccode=sccode&0x0f;
                                        switch(sccode+recode)          //recode是高四位,sccode是低四位
                                        {
                                                case 0xee:key=0;break;
                                                case 0xde:key=1;break;
                                                case 0xbe:key=2;break;
                                                case 0x7e:key=3;break;
                                                case 0xed:key=4;break;
                                                case 0xdd:key=5;break;
                                                case 0xbd:key=6;break;
                                                case 0x7d:key=7;break;
                                                case 0xeb:key=8;break;
                                                case 0xdb:key=9;break;
                                                case 0xbb:key=10;break;
                                                case 0x7b:key=11;break;
                                                case 0xe7:key=12;break;
                                                case 0xd7:key=13;break;
                                                case 0xb7:key=14;break;
                                                case 0x77:key=15;break;
                                        }
                                        //count++;
                                        flag=1;                                           //按键按下标志位
                                        //return key;
                                }
                                else
                                        sccode=(sccode<<1)|0x01;
                                
                        }            display(10);
                }
        }
        return key;
}

//主函数
void main()
{        
        temp=0xf7;
        P0=0x3f;
        dula=1;
        dula=0;
        P0=0x00;
        wela=1;
        wela=0;
        while(1)
        {
                a[0]=keyscan();
                if(flag==1)
                {
                        for(j=4;j>0;j--)//存放数据
                        {
                                a[j]=a[j-1];
                        }
                        flag=0;
                        count++;        
                }
                if(count>4)                  //存满后置零
                {
                        count=0;
                        for(j=4;j>0;j--)
                                a[j]=0;
                }
                display(2);
        }
}

矩阵键盘数码管显示.rar

1.17 KB, 下载次数: 17, 下载积分: 黑币 -5






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1