微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 简单键盘密码锁,但是为什么密码0开头才可以

简单键盘密码锁,但是为什么密码0开头才可以

时间:10-02 整理:3721RD 点击:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P1^4;
uchar i=0;uchar j;
bit flag;
uchar num;
uchar getdata[];
uchar code mima[]={0,7,3,5};
void delay(uint xms)
{
        uint m,n;
        for(m=xms;m>0;m--)
                for(n=110;n>0;n--);
}
uchar keyscan()
{
    uchar key,temp;
        P2=0xfe;
    temp=P2;
    temp=temp&0xf0;
    if(temp!=0xf0)
    {
        delay(10);
        temp=P2;
        temp=temp&0xf0;       
        if(temp!=0xf0)
            {
            temp=P2;
            switch(temp)
                    {
                case 0xee:key=12;break;
                case 0xde:key=13;break;
                case 0xbe:key=14;break;
                case 0x7e:key=15;break;
                    }
            while(temp!=0xf0)
                    {
                    temp=P2;
                temp=temp&0xf0;
                }
                         return key;                       
            }
    }
           
        P2=0xfd;
        temp=P2;
        temp=temp&0xf0;
            if(temp!=0xf0)
            {
        delay(10);
        temp=P2;
        temp=temp&0xf0;       
        if(temp!=0xf0)
            {
            temp=P2;
            switch(temp)
                    {
                case 0xed:key=8;break;
                case 0xdd:key=9;break;
                case 0xbd:key=10;break;
                case 0x7d:key=11;break;
                    }
            while(temp!=0xf0)
                    {
                    temp=P2;
                temp=temp&0xf0;
                }
                         return key;               
            }
    }
   
        P2=0xfb;
        temp=P2;
        temp=temp&0xf0;
            if(temp!=0xf0)
    {
        delay(10);
        temp=P2;
        temp=temp&0xf0;       
        if(temp!=0xf0)
            {
            temp=P2;
            switch(temp)
                    {
                case 0xeb:key=4;break;
                case 0xdb:key=5;break;
                case 0xbb:key=6;break;
                case 0x7b:key=7;break;
                    }
            while(temp!=0xf0)
                    {
                    temp=P2;
                temp=temp&0xf0;
                }
                         return key;               
            }
    }
   
        P2=  0xf7;
        temp=P2;
        temp=temp&0xf0;
            if(temp!=0xf0)
            {
        delay(10);
        temp=P2;
        temp=temp&0xf0;       
            if(temp!=0xf0)
            {
            temp=P2;
            switch(temp)
                    {
                case 0xe7:key=0;break;
                case 0xd7:key=1;break;
                case 0xb7:key=2;break;
                case 0x77:key=3;break;
                    }
            while(temp!=0xf0)
                    {
                    temp=P2;
                temp=temp&0xf0;
                }
                         return key;                       
            }
    }                       
                  return 0xff;
}
void main()
{
        while(1){
        num=keyscan();
        if(num!=0xff)
        {
                if(i<4)
                {
                        getdata[i]=num;
                }
                i++;
        if(i==4)
        {   flag=1;
            for(j=0;j<4;j++)
              {
                    flag=flag&&(mima[j]==getdata[j]);
              }
                if(flag==1)
                {
                        dula=1;
                        P0=0x60;
                        dula=0;
                        P1=0xff;
                }
                else
                {
                        dula=1;
                        P0=0xfc;
                        dula=0;
                        P1=0xff;       
                }
        }
        }
        }
}

每个口接的什么你也说明一下啊

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top