微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 用51单片机想写一个密码锁的程序,但是按键没有反应

用51单片机想写一个密码锁的程序,但是按键没有反应

时间:10-02 整理:3721RD 点击:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit c1=P0^0;
uchar temp,num,i;
uchar code pass[]={0,1,2,3};
uchar mima[5];

void delay(uint z);
uchar keyscan();
void main()
{
        c1=0;
        while(1)
        {
                for(i=0;i<4;i++)
                {
                        mima[i]=keyscan();       
                }
                if(mima[0]==pass[0]&&mima[1]==pass[1]&&mima[2]==pass[2]&&mima[3]==pass[3])
                {
                        c1=1;//打开继电器
                }
        }
}

void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
          for(y=110;y>0;y--);
}

/*键盘扫描*/
uchar keyscan()
{
        P1=0xfe;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {       
                        delay(10);
                        P1=0xfe;
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                 temp=P1;
                                 switch(temp)
                                 {
                                    case 0xee:num=0;break;
                                        case 0xde:num=1;break;
                                        case 0xbe:num=2;break;
                                        case 0x7e:num=3;break;
                                 }
                                 if(temp!=0xf0)
                                 {
                                         temp=P1;
                                        temp=temp&0xf0;       
                                 }         
                         }                         
                }
               
               
               
                P1=0xfd;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {       
                        delay(10);
                        P1=0xfd;
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                 temp=P1;
                                 switch(temp)
                                 {
                                    case 0xed:num=4;break;
                                        case 0xdd:num=5;break;
                                        case 0xbd:num=6;break;
                                        case 0x7d:num=7;break;
                                 }
                                 if(temp!=0xf0)
                                 {
                                         temp=P1;
                                        temp=temp&0xf0;       
                                 }         
                         }                         
                }
               
               
               
               
                P1=0xfb;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {       
                        delay(10);
                        P1=0xfb;
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                 temp=P1;
                                 switch(temp)
                                 {
                                    case 0xeb:num=8;break;
                                        case 0xdb:num=9;break;
                                        case 0xbb:num=10;break;
                                        case 0x7b:num=11;break;
                                 }
                                 if(temp!=0xf0)
                                 {
                                         temp=P1;
                                        temp=temp&0xf0;       
                                 }         
                         }                         
                }
               
               
               
                P1=0xf7;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {       
                        delay(10);
                        P1=0xf7;
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                 temp=P1;
                                 switch(temp)
                                 {
                                    case 0xe7:num=12;break;
                                        case 0xd7:num=13;break;
                                        case 0xb7:num=14;break;
                                        case 0x77:num=15;break;
                                 }
                                 if(temp!=0xf0)
                                 {
                                         temp=P1;
                                        temp=temp&0xf0;       
                                 }        
                         }                         
                }       
        return        num;
}

请上QQ52529158上送资料给你

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

网站地图

Top