Digital tube password lock based on 51 single chip microcomputer

The digital tube password lock based on 51 single-chip microcomputer is a security device that can set a password and unlock it by entering the password. The combination lock uses 51 single-chip microcomputer as the control board, cooperates with digital tube display module, key module, electromagnetic lock and other components to realize the function of combination lock.

The implementation steps are as follows:

1. Hardware connection: connect the 51 single-chip microcomputer with the digital tube, key module and electromagnetic lock and other components. The digital tube is used to display the password and unlock status, the button module is used to input the password, and the electromagnetic lock is used to control the switch of the door lock.

2. Program design: use assembly or C language to write programs. The program mainly includes functions such as password setting, password input and verification. In the password setting mode, the user can enter the password by pressing the key and save it in EEPROM. In the password input mode, the user can enter the password by pressing the key and compare it with the password saved in the EEPROM. If the password matches, the electromagnetic lock will be unlocked, otherwise it will display that the password is wrong.

3. System test: Burn the program into the 51 single-chip microcomputer, connect the system and test it. During the test, you can try to set a password, enter the password, and observe the system's response.

It should be noted that in order to increase the security of the password, measures to prevent password cracking can be added to the program design, such as limiting the number of wrong passwords, password input timeout, etc. In addition, you can also consider using other security components, such as fingerprint recognition modules or card swiping modules, to improve the security of the combination lock.

  • The basic principle of digital combination lock 

The basic principle of the 51 single-chip digital tube combination lock is to compare the password entered by the user with the initial password, and if the two match, the lock will be unlocked, otherwise the lock will be closed, and the password can be changed after unlocking. STC89C52 single-chip microcomputer development board is selected as the core component of this design, and various hardware circuits such as programming design, I/O ports, digital tubes and buttons on the board are used to design the combination lock function, and the function is realized with self-created code. The matrix keyboard of the single-chip microcomputer is used for the input of the password, the control of the password reset and the change of the password, the storage of the password without power failure, and the real-time display of the input password by using the digital tube. The digital tube is responsible for displaying the password, and the matrix keys are responsible for inputting, changing and resetting the password.

  • Hardware Design of Digital Combination Lock   

(1) Schematic design

 

Figure 1 Schematic diagram of digital combination lock

(2) Principles of each part of the schematic diagram

The main control circuit is composed of STC89C52 chip and 12M crystal oscillator. The important function is to control the input and output states of P1, P2, and P3 ports by writing codes to achieve the function of controlling the entire circuit.

The reset button circuit is composed of a reset button and a capacitor. When the reset button is pressed, RES will be pulled high, and the RES interface on the STC89C52 chip will be given a high level to achieve the function of the reset circuit.

The matrix button circuit is composed of sixteen buttons, using a matrix structure of 4 rows and 4 columns, in which each row is connected to a programmable I/O port, and each column is connected to a button switch. When a key is pressed, the input of the programmable I/O port changes from high to low. At this time, the row and column number corresponding to the key can be obtained by scanning the matrix of 4 rows and 4 columns.

The digital tube display circuit is composed of two four-digit common anode code tubes. It is a commonly used display and control circuit. It can display numbers and letters in different formats, as well as corresponding graphics. The digital tube circuit is controlled by a 51 single-chip microcomputer. The digital tube position selection is connected to the output port P2 of the single-chip microcomputer, and the digital tube segment selection is connected to the output port P3 of the single-chip microcomputer. The signal is driven by the circuit, and a digital tube is controlled within a certain period of time to display the corresponding state. Then through fast switching, the human eye observes that several digital tubes light up at the same time.

serial number

Device name

parameter

quantity

1

USB socket

none

1

2

usb cable

none

1

3

Serial download cable

none

1

4

104 capacitance

0.1uF

7

5

led light

none

1

6

resistance

2k

1

7

buzzer

none

1

8

STC89C52

none

1

9

crystal oscillator

12MHz

1

10

button

none

16

11

Reset button

none

1

12

resistance

10K

1

13

40-pin header

none

1

14

Triode 9015

none

9

15

resistance

330

1

16

16-pin header

none

1

17

MAX232

none

1

18

capacitance

20pF

2

19

4 LEDs

none

2

20

resistance

330

8

21

resistance

4.7K

8

Table 2 List of components

  • Software Design of Digital Combination Lock

The program flow chart is as follows:

 

The program code is as follows:

#include <REGX52.H>

typedef unsigned int u16; //对系统默认数据类型进行重定义

typedef unsigned char u8;

#define KEY_MATRIX_PORT P3 //使用宏定义矩阵按键控制口

#define SMG_A_DP_PORT P0 //使用宏定义数码管段码口

//共阳极数码管显示0~9 N F 的段码数据

u8 nixie[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x37,0x71};

unsigned int Code = 0;//用于保存密码的变量

unsigned int TempCode = 0;//用于暂时保存密码的变量

unsigned int NewCode = 0;//用于保存新密码的变量

unsigned int LNew=0,New=0;//用于判断是否开始录入新密码

unsigned int Newlate=0;//用于确定是否新密码录入结束

void wei(unsigned char pizz);//用于显示第几个数码管的函数声明

unsigned int key=0;//保存按键按下的位数

unsigned int T=0;//密码输入正确判断

unsigned int TLL=0;//密码输入正确判断

unsigned int F=0;//密码输入错误判断

static unsigned char i = 0;//刷新数码管

void delay_10us(u16 ten_us)//延时函数

{

while(ten_us--);

}

u8 key_matrix_ranks_scan(void)

{

u8 key_value=0;



KEY_MATRIX_PORT=0xf7;//给第一列赋值0,其余全为1

if(KEY_MATRIX_PORT!=0xf7)//判断第一列按键是否按下

{

delay_10us(500);//消抖

switch(KEY_MATRIX_PORT)//保存第一列按键按下后的键值

{

case 0x77: key_value=1;break;

case 0xb7: key_value=5;break;

case 0xd7: key_value=9;break;

case 0xe7: key_value=13;break;

}

}

while(KEY_MATRIX_PORT!=0xf7);//等待按键松开



KEY_MATRIX_PORT=0xfb;//给第二列赋值0,其余全为1

if(KEY_MATRIX_PORT!=0xfb)//判断第二列按键是否按下

{

delay_10us(500);//消抖

switch(KEY_MATRIX_PORT)//保存第二列按键按下后的键值

{

case 0x7b: key_value=2;break;

case 0xbb: key_value=6;break;

case 0xdb: key_value=10;break;

case 0xeb: key_value=14;break;

}

}

while(KEY_MATRIX_PORT!=0xfb);//等待按键松开



KEY_MATRIX_PORT=0xfd;//给第三列赋值0,其余全为1

if(KEY_MATRIX_PORT!=0xfd)//判断第三列按键是否按下

{

delay_10us(500);//消抖

switch(KEY_MATRIX_PORT)//保存第三列按键按下后的键值

{

case 0x7d: key_value=3;break;

case 0xbd: key_value=7;break;

case 0xdd: key_value=11;break;

case 0xed: key_value=15;break;

}

}

while(KEY_MATRIX_PORT!=0xfd);//等待按键松开



KEY_MATRIX_PORT=0xfe;//给第四列赋值0,其余全为1

if(KEY_MATRIX_PORT!=0xfe)//判断第四列按键是否按下

{

delay_10us(500);//消抖

switch(KEY_MATRIX_PORT)//保存第四列按键按下后的键值

{

case 0x7e: key_value=4;break;

case 0xbe: key_value=8;break;

case 0xde: key_value=12;break;

case 0xee: key_value=16;break;

}

}

while(KEY_MATRIX_PORT!=0xfe);//等待按键松开



return key_value;

}

u8 key_matrix_flip_scan(void)

{

static u8 key_value=0;



KEY_MATRIX_PORT=0x0f;//给所有行赋值0,列全为1

if(KEY_MATRIX_PORT!=0x0f)//判断按键是否按下

{

delay_10us(500);//消抖

if(KEY_MATRIX_PORT!=0x0f)

{

KEY_MATRIX_PORT=0x0f;//测试列

switch(KEY_MATRIX_PORT)//保存行为0,按键按下后的列值

{

case 0x07: key_value=1;break;

case 0x0b: key_value=2;break;

case 0x0d: key_value=3;break;

case 0x0e: key_value=4;break;

}

KEY_MATRIX_PORT=0xf0;//测试行

switch(KEY_MATRIX_PORT)//保存列为0,按键按下后的键值

{

case 0x70: key_value=key_value;break;

case 0xb0: key_value=key_value+4;break;

case 0xd0: key_value=key_value+8;break;

case 0xe0: key_value=key_value+12;break;

}

while(KEY_MATRIX_PORT!=0xf0);//等待按键松开

}

}

else

key_value=0;

return key_value;

}

void smg_display(unsigned char pizz)

{

    switch(pizz)//位选

{

case 1: P2=0x7f;break;//第一个灯亮

case 2: P2=0xbf;break;//第二个灯亮

case 3: P2=0xdf;break;//第三个灯亮

case 4: P2=0xef;break;//第四个灯亮

case 5: P2=0xf7;break;//第五个灯亮

case 6: P2=0xfb;break;//第六个灯亮

case 7: P2=0xfd;break;//第七个灯亮

case 8: P2=0xfe;break;//第八个灯亮

}

}

void main()

{

  unsigned char k;//限制按下次数

while(1)

{

  key=key_matrix_ranks_scan();

  if(key!=0)

 {

         if(key<=10)

         {

           if(k<4)

            {

 if(T==0)

 {

               Code*=10;            

               Code+=key%10;

  if(TLL!=1)

{

                    k++;

}

 }

if(TLL==1)

{

    TempCode*=10;            

             TempCode+=key%10;

Code=TempCode;

             k++;

     LNew=1;

T=0;

}

         }    

else

{

 k=0;

  if(Code==8888&&Newlate==0)

  {

T=1;

Code=8888;

TLL=1;

   }

  else if(Code==NewCode&&Newlate==1)

  {

   T=1;

   LNew=0;

   TLL=0;

  }

       else

 {

  F=1;

 }

  }

            }

            else if(key==16)

            {

                k=0;

                Code=0;

T=0;

  F=0;

  LNew=0;

  New=0;

            }

            else if(key==15)

            {

               if(LNew==1)

  {

 NewCode=TempCode;

Newlate=1;

 TLL=0;

 Code=0;

 k=0;

 }

            }

        }



if(F==0&&T==0&&Code!=0)

    {

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

      {

   switch(i)

        {  

        case 0:smg_display(1);P0=~nixie[Code/1000%10];break;

        case 1:P0=0xff;break;

        case 2:smg_display(3);P0=~nixie[Code/10%10];break;

        case 3:P0=0xff;break;

        case 4:smg_display(2);P0=~nixie[Code/100%10];break;

        case 5:P0=0xff;break;

        case 6:smg_display(4);P0=~nixie[Code%10];break;

        case 7:P0=0xff;break;

        }

delay_10us(50);

   }

}

 if(T==1&&LNew==0)

 {

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

     {

    switch(i)

        {  

case 0:P0=0xff;break;

        case 1:smg_display(7);P0=~nixie[0];break;

        case 2:P0=0xff;break;

case 3:smg_display(8);P0=~nixie[10];break;

        case 4:P0=0xff;break;

        }

delay_10us(50);

}

}

 if(F==1)

 {

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

     {

   switch(i)

       {  

case 0:P0=0xff;break;

        case 1:smg_display(6);P0=~nixie[0];break;

        case 2:P0=0xff;break;

case 3:smg_display(7);P0=~nixie[11];break;

        case 4:P0=0xff;break;

case 5:smg_display(8);P0=~nixie[11];break;

        case 6:P0=0xff;break;

       }

delay_10us(50);

 }

 }

 if(Code==0)

{

  P0=0x00;  //未输入密码  数码管全部不亮

}

    

}

}
  • Design results

The matrix button function diagram is as follows:

 

After downloading the program, press the four passwords at random. Here, take pressing the keys key1, key2, key3, and key4 as an example. The digital tube displays the input password 1234 as shown in Figure 1; after the password input reaches four digits, press any One button, if the password is not the initial password 8888, the digital tube will display OFF as shown in Figure 2. At this time, press the button key13 to reset. After reset, you can continue to enter the password. At this time, the input password here is 8888, which is the same as the initial password as shown in Figure 3. Press any button again, and the digital tube will display ON as shown in Figure 4; when the input password is correct Finally, you can choose to press the reset button and continue to enter the password, or choose to change the password. Here, the new password 5678 is composed of key5, key6, key7, and key8, and then press key14 to save the new password, so that the password of the combination lock is changed from the initial password 8888 It has changed to 5678, and you can continue to enter the passwords 8888 and 5678 for verification, as shown in Figures 5 and 6.

                         Figure 1 Figure 2

 

                                              Figure 3 Figure 4

                                    Figure 5 Figure 6

Design summary

First of all, I would like to thank the teacher for providing us with valuable teaching resources and rich teaching experience in this course. In addition, I would also like to thank my teammates for their cooperative spirit and excellent performance when completing the course design task, so that we can finally complete the course design task smoothly. In the process of course design, we learned knowledge about single-chip microcomputers such as digital tubes, delay design, key debounce, etc., and used practical applications to conduct comprehensive assessments. Through this course design, I deeply understand that single-chip microcomputer can realize various functions, and because of its small size and low power consumption, it can also be used in large-scale projects. Finally, I want to emphasize that we must be good at summarizing, absorbing, and thinking, and strive to integrate what we have learned so as to better realize our own development and progress.

Guess you like

Origin blog.csdn.net/qq_58404700/article/details/131486942