打印
[51单片机]

单片机密码锁

[复制链接]
862|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
飞宇|  楼主 | 2015-8-20 13:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
求助大家,谢谢
功能:按一个键显示一位数码管,第二次按显示两位数码管,类推6个
问题:按键有16个,为啥左边8个能用,右边八个不能用,按右边8个键会出现0或者好几个0,急问为啥。


#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

uchar code table_dula[17]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar code table_wela[7]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xff};                       

sbit dula=P2^6;
sbit wela=P2^7;

uchar num,temp,a,b,c,d,e,f;

void delay(char z);                                         //延时z毫秒函数
void keyscan4x4();                                         //键盘扫描
void display(char shu,char wei);        //数码管显示

void main()                                                      //主函数
{       
        P3=0xf0;
        while(P3==0xf0);
        delay(10);
       
        keyscan4x4();
        a=num;
        P3=0xf0;
        while(P3==0xf0)
        display(a,0);
        delay(10);

        keyscan4x4();
        b=num;
        P3=0xf0;
        while(P3==0xf0)
        {display(a,0);
        display(b,1);}
        delay(10);

        keyscan4x4();
        c=num;
        P3=0xf0;
        while(P3==0xf0)
        {display(a,0);
        display(b,1);
        display(c,2);}
        delay(10);

        keyscan4x4();
        d=num;
        P3=0xf0;
        while(P3==0xf0)
        {display(a,0);
        display(b,1);
        display(c,2);
        display(d,3);}
        delay(10);

        keyscan4x4();
        e=num;
        P3=0xf0;
        while(P3==0xf0)
        {display(a,0);
        display(b,1);
        display(c,2);
        display(d,3);
        display(e,4);}
        delay(10);

        keyscan4x4();
        f=num;
        P3=0xf0;
        while(P3==0xf0)
        {display(a,0);
        display(b,1);
        display(c,2);
        display(d,3);
        display(e,4);
        display(f,5);}
        delay(10);
}

void delay(char z)                                        //延时z毫秒函数
{
        char x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);       
}               
void keyscan4x4()                                        //键盘扫描
{
        P3=0xfe;
        temp=P3;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P3;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xee:num=0;
                                        break;
                                case 0xde:num=1;
                                        break;
                                case 0xbe:num=2;
                                        break;
                                case 0x7e:num=3;
                                        break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
       
        P3=0xfd;
        temp=P3;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P3;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xed:num=4;
                                        break;
                                case 0xdd:num=5;
                                        break;
                                case 0xbd:num=6;
                                        break;
                                case 0x7d:num=7;
                                        break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
       
        P3=0xfb;
        temp=P3;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P3;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xeb:num=8;
                                        break;
                                case 0xdb:num=9;
                                        break;
                                case 0xbb:num=10;
                                        break;
                                case 0x7b:num=11;
                                        break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
               
        P3=0xf7;
        temp=P3;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P3;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xe7:num=12;
                                        break;
                                case 0xd7:num=13;
                                        break;
                                case 0xb7:num=14;
                                        break;
                                case 0x77:num=15;
                                        break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
}

void display(char shu,char wei)            //数码管显示
{
        P0=table_dula[shu];
        dula=1;
        dula=0;
        P0=table_wela[wei];
        wela=1;
        wela=0;
        P0=0xff;
        wela=1;
        wela=0;
}

相关帖子

沙发
飞宇|  楼主 | 2015-8-21 18:03 | 只看该作者
抱歉,原来是开发板的问题

使用特权

评论回复
板凳
dirtwillfly| | 2015-8-22 08:02 | 只看该作者
飞宇 发表于 2015-8-21 18:03
抱歉,原来是开发板的问题

解决了就好

使用特权

评论回复
地板
ningling_21| | 2015-8-22 10:17 | 只看该作者
飞宇 发表于 2015-8-21 18:03
抱歉,原来是开发板的问题

或许是杜邦连线没接触好

使用特权

评论回复
5
飞宇|  楼主 | 2015-8-22 15:35 | 只看该作者
应该是单片机芯片问题,这是自己组装的板

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝