密码器

/*

时间:20151123:42

地点:宿舍

事件:帮同学做课程设计(密码器)

功能:19的数字,且最多5位密码

16个按键 015 

0   1   2   3

4   5   6   7 

8   9   del yes

*   *   *   *

原题

33. 4×键盘及 位数码管显示构成的电子密码锁

1. 实验任务

用 4×组成 0数字键及确认键。

用 位数码管组成显示电路提示信息,当输入密码时,只显示“8.”,当

密码位数输入完毕按下确认键时,对输入的密码与设定的密码进行比较,

若密码正确,则门开,此处用 LED 发光二极管亮一秒钟做为提示,同时发

出“叮咚”声;若密码不正确,禁止按键输入 秒,同时发出“嘀、嘀”

报警声;若在 秒之内仍有按键按下,则禁止按键输入 秒被重新禁止。

若在 秒之内仍有按键按下,则禁止按键输入 秒被重新禁止。时间仓促这一功能未能实现。

只要理解我那个抢答器,这个就可以理解。

*/

#include<AT89X51.H>

sbit s1=P0^0;

sbit s2=P0^1;

sbit s3=P0^2;

int led[22]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x8c,0xc1,0xce,0x91,0x00,0xff};

int temp;

int key;

int i,j,k1,k2,k3,h,l;

int to;

int password;

int pa[8]= {8,8,8,8,8,8,8,8};

void mima(void)

{

    P2=0x01;

    P1=led[pa[0]];

    for(l=0; l<10; l++);

    P2=0x02;

    P1=led[pa[1]];

    for(l=0; l<10; l++);

    P2=0x04;

    P1=led[pa[2]];

    for(l=0; l<10; l++);

    P2=0x08;

    P1=led[pa[3]];

    for(l=0; l<10; l++);

    P2=0x10;

    P1=led[pa[4]];

    for(l=0; l<10; l++);

    P2=0x20;

    P1=led[pa[5]];

    for(l=0; l<10; l++);

    P2=0x40;

    P1=led[pa[6]];

    for(l=0; l<10; l++);

    P2=0x80;

    P1=led[pa[7]];

    for(l=0; l<10; l++);

}

void main(void)

{

    to=0;

    password=12345;

    while(1)

    {

        mima();

        s1=1;

        s2=1;

        P3=0xff;

        P3_4=0;

        temp=P3;

        temp=temp&0x0f;

        if(temp!=0x0f)

        {

            switch(temp)

            {

            case 0x0e:

                key=0;

                break;

            case 0x0d:

                key=1;

                break;

            case 0x0b:

                key=2;

                break;

            case 0x07:

                key=3;

                break;

            }

            while(temp!=0x0f)

            {

                temp=P3;

                temp=temp&0x0f;

                mima();

            }

            to=to*10+key;

        }

        P3=0xff;

        P3_5=0;

        temp=P3;

        temp=temp&0x0f;

        if(temp!=0x0f)

        {

            switch(temp)

            {

            case 0x0e:

                key=4;

                break;

            case 0x0d:

                key=5;

                break;

            case 0x0b:

                key=6;

                break;

            case 0x07:

                key=7;

                break;

            }

            while(temp!=0x0f)

            {

                temp=P3;

                temp=temp&0x0f;

                mima();

            }

            to=to*10+key;

        }

        P3=0xff;

        P3_6=0;

        temp=P3;

        temp=temp&0x0f;

        if(temp!=0x0f)

        {

            switch(temp)

            {

            case 0x0e:

                key=8;

                break;

            case 0x0d:

                key=9;

                break;

            case 0x0b:

                key=10;

                break;

            case 0x07:

                key=11;

                break;

            }

            if(key==8||key==9)

            {

                while(temp!=0x0f)

                {

                    temp=P3;

                    temp=temp&0x0f;

                    mima();

                }

                to=to*10+key;

            }

            else if(key==10)

            {

                to=to/10;

            }

            else if(key==11)

            {

                if(to==password)

                {

                    mima();

                    s1=0;

                    s3=0;

                    for(i=7; i>=0; i--)

                    {

                        pa[i]=to%10;

                        to=to/10;

                    }

                    for(i=0; i<200; i++)

                        for(j=0; j<200; j++);

                    s1=1;

                    s3=1;

                    while(1)

                    {

                        mima();

                    }

                }

                else

                {

                    to=0;

                    s2=0;

                    s3=0;

                    for(k1=0; k1<=200; k1++)

                        for(k2=0; k2<=200; k2++)

                        {

                            mima();

                        }

                    s2=1;

                    s3=1;

                }

            }

        }

    }

}

密码器密码是12345

当正确输入密码按确认键则打开

否则为

原文地址:https://www.cnblogs.com/liudehao/p/4198300.html