Design of password lock controlled by single chip microcomputer

Publisher:钱老李Latest update time:2011-10-22 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Design of a password lock controlled by a single-chip microcomputer. The P1 pin of the AT89S52 single-chip microcomputer is connected to independent buttons S1-S8, which represent the number keys 0-5, the confirmation button, and the cancel button respectively. The single-chip microcomputer outputs 4 signals from P3.0-P3.3, which are an electromagnetic unlocking drive signal and a password error indication, an alarm output, and an unlocked indication signal, which are indicated by light-emitting diodes L1-L4 respectively. P3.4 is connected to an active buzzer to realize the prompt sound.
Basic requirements:
(1) The initial password is 123450. After entering, press the confirmation button to unlock the lock, and the cancel button to clear all inputs. Each key press has a short "beep" key prompt sound.
(2) After the password is entered correctly, an electromagnetic lock unlocking signal and an unlocked signal are output, and two short "beep" sounds are issued as a prompt. After 4 seconds, the unlocking signal and the unlocked indication are cleared.
(3) When the password is entered incorrectly, a long beep will sound and the password error indicator will light up. If the password is entered incorrectly for three times, a long beep will sound and the password error indicator will light up. The alarm indicator will light up and the password cannot be entered again within 15 seconds. After 15 seconds, all alarms and indications will be cleared.
(4) If there is no operation within 5 seconds, all input content will be cleared and wait for the next input.

3.3.1 Module 1 System Design
(1) Analyze the task requirements. Write out the overall design ideas of the system.
According to the requirements of the topic, the following tasks need to be considered: key input, password judgment, correct or incorrect password input timing, output signal control, etc.
Keyboard input: Since 6 digits need to be entered as the password, it is necessary to first determine whether the key is a numeric key or a function key. If it is determined that the key is pressed, the keyboard input content needs to be temporarily stored in an array in sequence. Each time a key is entered, a timer needs to be started to realize standby timing (5 seconds). If no content is entered within 5 seconds, the entered content is cleared.
Password judgment and timing: After pressing the confirmation key, the input content needs to be checked with the initial password. If the password is correct, the corresponding indication is output, and the timer is started to realize 4s timing. If the password is wrong, the error count variable is increased by 1, and the password indication signal is output at the same time. If the number of errors exceeds 3s, an alarm signal is output, and the timer is started to realize 15 seconds timing.
The control of the output signal is mainly determined by the verification of the key input and the password.
Overall program design concept:
The program is divided into two main parts: the main program and the interrupt service program. After the main program completes the initialization of variables and special function registers of the microcontroller, it enters a loop structure. In the loop, first determine whether a key is pressed. If a key is pressed, determine whether it is a numeric key or a function key, and execute the corresponding function according to the key situation. Then, according to the judgment of whether the password is correct, execute the corresponding operation. At the end of the loop, the content to be displayed is displayed on the digital tube through dynamic scanning.
The interrupt service program only needs to realize the timing of three states. When in standby mode, it takes 5 seconds to count, the password is correct, and it takes 5 seconds to count. If the password is entered incorrectly for 3 times, it takes 15 seconds to count. The current timing is determined by the main program based on the password judgment result.
(2) Select the microcontroller model and the required peripheral device model, and design the microcontroller hardware circuit schematic diagram
The MCS51 series microcontroller At89S51 is used as the main controller. The peripheral circuit devices include digital tube driver, buzzer output driver, independent keyboard and light-emitting diode output.
The digital tube driver uses two quadruple common cathode digital tubes for display. Due to the limited driving capability of the single-chip microcomputer, 74HC244 is used as the driver of the digital tube. A 100-ohm resistor is connected in series on the 7-segment code output line of the 74HC244 to limit the current.
The buzzer is driven by the PNP transistor 8550, and the low level is effective.
The independent button is connected using an upper pull-up circuit. When no key is pressed, the output is a high level. The light-emitting diode is connected in series with a 500-ohm resistor and then connected to the power supply. When the input is at a low level, the light-emitting diode is turned on and emits light.
The hardware circuit schematic is shown in Figure 3-9.

Figure 3-9 Schematic diagram of password lock circuit
(3) Analyze the software task requirements, write out the program design ideas, allocate the internal resources of the single-chip microcomputer, and draw the program flow chart.
The software task requirements mainly include key scanning, password judgment, dynamic scanning input content, timing, indication signal output, and buzzer prompt sound output. The main program mainly completes the initialization of variables and registers, key scanning and judgment, password judgment, and digital tube dynamic scanning display. The main program flow chart is shown in Figure 3-10.

Figure 3-10 Main program flow chart of password lock

The interrupt service program mainly completes three timing tasks, including ① the standby timing started after the key is pressed. When the standby time exceeds 5s, the input content is cleared. ② The timing after the password is entered correctly, the unlocking drive signal and the unlocked indication signal are cleared after 4s. ③ The timing after the password is entered incorrectly for 3 times, the timing is 15s, and the password cannot be entered again within 15s. After 15 seconds, all alarms and indications are cleared. The flow chart of the interrupt service program is shown in Figure 3-11.

Figure 3-11 Flowchart of password lock interrupt service program
MCU resource allocation and variable definition:
4 variables need to be defined for password input and judgment. The original password is stored in the array init_val[6]. The password entered by the keyboard is stored in the data show_val[6]. The value of the variable key_index indicates which digit of the six-digit password the current key is. The variable increases by one for each password digit entered. The number of incorrect password inputs is temporarily stored in the variable error_num.
The timing function requires 5 variables. The mode variable cnt_state stores the timing state. 0 indicates standby timing, 1 indicates correct password timing, and 2 indicates three incorrect password timings. Three variables (cnt_val_15s, cnt_val_5s, cnt_val_4s) implement the timing work of standby, correct password, and three incorrect passwords respectively. Timer T1 generates an interrupt every 250ms. The variable T1_cnt records the number of timer overflow interrupts. When the record reaches 4000, it means the timing is 1 second.
(4) Design system software debugging scheme, hardware debugging scheme and software and hardware joint debugging scheme
Software debugging scheme: In WeiFu software, in "File New File", create a new C language source program file and write the corresponding program. In the "File New Project" menu, create a new project and include the C language source program file in the project file.
In the "Project Compile" menu, compile the C source file and check for syntax errors and logical errors. After successful compilation, target files with "*.hex" and "*.bin" suffixes are generated.
Hardware debugging scheme: In the design platform, connect P1.0-P1.7 of the microcontroller to 8 independent keyboards through plug wires, connect P3.0-P3.3 to 4 light-emitting diodes, and connect P3.4 to the input of the buzzer. After
compiling the program file into a target file in WeiFu, install the download cable on the download cable interface of the experimental platform, run the "MCU Download Program", select the corresponding flash data file, and click the "Program" button to download the program file to the Flash of the microcontroller.
Then, power on and restart the microcontroller to check whether the written program meets the requirements of the question and whether it completes the content of the test question comprehensively and completely.

3.3.2 Programming
//Crystal oscillator 11.0592MHz, T1 interrupt every 250 microseconds, buttons P1.0-P1.7, LED connected to P3.0-P3.3, p3.4
/*Variable definition:
show_val[6]: displayed value
init_val[6]: password initial value
key_val: return key value 255-indicates no key is pressed
key_index: which password is the current key
T1_cnt: timer count overflow number
cnt_val_15s: alarm timing value
cnt_val_5s: standby time timing
cnt_val_4s: input is correct, wait 4 seconds to clear the unlock signal
cnt_state: timing status
error_num: number of errors
led_seg_code: digital tube 7-segment code
*/
#include "reg51.h"
/*Description key0=P1^0; key1=P1^1;key2=P1^2; key3=P1^3;key4=P1^4;key5=P1^5;enter=P1^6;esc=P1^7;*/
sbit relay_open=P3^0; //Electromagnetic lock unlocking drive
sbit pw_error=P3^1; //Password error signal
sbit alarm_out=P3^2; //Alarm output
sbit open_lock=P3^3; //Unlocked indication signal
sbit audio_out=P3^4; //Active buzzer
unsigned char data cnt_val_15s,cnt_val_5s,cnt_val_4s,cnt_state;
unsigned int data T1_cnt;
unsigned char data key_val,key_index,key_val_old;
unsigned char data state_val,error_num;
unsigned char data show_val[6];
char code init_val[6]={1,2,3,4,5,0};
char code led_seg_code[11]={0x3f,0x06,0x05b,0x04f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
//led_seg_code[0-9] represents 0-9 led_seg_code[10]=0x00 the digital tube does not display anything
//--------delay program----------------
void delay(unsigned int i)//delay
{ while(--i); }
//--------clear input content----------
void init_variant()
{unsigned char i;
for(i=0;i<6;i++)
show_val[i]=10; //led_seg_code[10]=0x00 means the digital tube does not display anything
key_index=0; //When there is no input or all inputs are cleared, save the current key position
}
//---------key scan---------------
unsigned char scan_key()
{ unsigned char i,k;
i=P1;
if (i==0xff && cnt_state!=2)
{ k=255; } //No key pressedelse
//A key is pressed
{ delay(500); //Delay de-jitterif
(i!=P1)
{k=255;}
else
{ TR1=1; //If a key is pressed, turn on the timer and start the standby
timingcnt_val_5s=0;
switch (i)
{ case 0xfe: k=0; break;
case 0xfd: k=1; break;
case 0xfb: k=2; break;
case 0xf7: k=3; break;
case 0xef: k=4; break;
case 0xdf: k=5; break;
case 0xbf: k=6; break;
case 0x7f: k=7; break;
}
}
}
return k;
}
//---------Digital tube display---------------
void led_show()
{P0=led_seg_code[show_val[0]];
P2=0xdf;
delay(500);
P0=led_seg_code[show_val[1]];
P2=0xef;
delay(500);
P0=led_seg_code[show_val[2 ]];
P2=0xf7;
delay(500);
P0=led_seg_code[show_val[3]]; P2=
0xfb;
delay(500);
P0=led_seg_code[show_val[4]];
P2=0xfd;
delay(500);
P0=led_seg_code[show_val[5]];
P2=0xfe;
delay(500);
}
//--------Timer T1 interrupt service routine-------------- ---
void timer1() interrupt 3 //T1 interrupt
{ T1_cnt++;
if(T1_cnt>3999) //If the count>3999, time 1s
{ T1_cnt=0;
switch (cnt_state)
{ case 0: //Standby, need to time 5s
if(cnt_val_5s<5)
{ cnt_val_5s++;}
else
{ cnt_val_5s=0;
init_variant();/ /When the standby timer reaches 5 seconds, clear the input content
TR1=0; //Stop timing
}
break;
case 1://The password is entered correctly, and the timer needs to be 4s
if(cnt_val_4s<4)
{ cnt_val_4s++;}
else
{ cnt_val_4s=0 ;
init_variant(); //The password is entered correctly. When the time reaches 4 seconds, clear the input content
open_lock=1; //The unlocked signal is cleared
relay_open=1; //The unlocked signal is cleared
cnt_state=0;
TR1=0; //Stop timing
}
break;
case 2: //Wrong password input 3 times, timing 15s
if(cnt_val_15s<15)
{ cnt_val_15s++;}
else
{ cnt_val_15s=0;
init_variant();//When the password is incorrect for three times, the timer will be 15 seconds and the input content will be cleared
open_lock=1; // Clear all indications and alarms
relay_open=1;
alarm_out=1;
pw_error=1;
cnt_state=0;
TR1=0; //Stop timing
}
break;
}
}
}
//--------Judge whether the keyboard input content is consistent with the password------
unsigned char check_input_pw()
{ unsigned char i,k;
k=1;
for(i=0;i<6;i++)
{ k=k && (show_val[i]==init_val[i]); }
return k;
}
//---------Main program----------------
main()
{ //Initialize variables
audio_out=1;
P3=0xff;
cnt_val_15s=0 ;
cnt_val_5s=0;
cnt_val_4s=0;
cnt_state=0;
//0-5s standby timer; 1-password correct, 4s timer; 2-password error three times, 15s timer.
T1_cnt=0;
error_num=0;
key_val_old=255;
init_variant();
//Initialize 51's register
TMOD=0x20; //Use T1 to time 8-bit automatic loading timing mode
TH1=0x19; //Overflow once every 500 microseconds; 250=(256-x)*12/11.0592 -> x=19
TL1=0x19;
EA=1; //Open interrupt
ET1=1;
TR1=0; //Open timer T1
while(1)
{ key_val=scan_key(); //Key input, key_val is 0-7 if a key is pressed, and key_val is 255 if no key is pressed.
if (key_val!=key_val_old)
{ key_val_old=key_val;
if (key_val!=255&& cnt_state!=2)
{ audio_out=0;
delay(100); //delay de-jitter
audio_out=1;
switch (key_val)
{ case 0:
case 1:
case 2: case
3:
case 4:
case 5:
if(key_index<6) //The password is 6 digits, more than 6 digits are considered invalid input
{ show_val[key_index]=key_val;
key_index++; }
break;
case 6: //Confirmation keyif
(check_input_pw())
{//Password is
correcterror_num=0; //The number of incorrect password inputs is cleared
//---------
pw_error=1; //Password error indicator is off
relay_open=0; //Unlock drive signal light is on open_lock=
0; //Unlocked signal light is on
//---------
delay(50000); //two short "beeps"
audio_out=0;
delay(50000);
audio_out=1 ;
delay(50000);
audio_out=0;
delay(50000);
audio_out=1;
//---------
cnt_state=1; //next state is 4 seconds timing state
TR0=1; //start timing
}
else
{ if (error_num<2)
{error_num++; //when the number of input errors is less than 3 times, error_num increases by one
pw_error=0; //password error indicator light is on
//-----------
delay(20000);//one long "beep" sound, prompting error
audio_out=0;
delay(60000);
audio_out=1;
//-----------
init_variant();//clear all inputs and wait for next input
}
else //the number of input errors exceeds 3 times
{ alarm_out=0; //alarm light is on
pw_error=0; //password error indicator light is on
error_num=0; //Reset the number of incorrect password input attempts
//----------
audio_out=0; //Long beep alarm
delay(60000);
delay(60000);
delay(60000
); delay
(60000); delay
(60000) ; delay
(60000); delay(60000);
delay(60000);
delay(60000);
audio_out=1;
//-------------
TR1=1; //Turn on the timer
cnt_state=2; //The next state is the 15-second timing state
}
}
break;
case 7://Cancel
keyinit_variant();
break;
}
}
}
led_show();
}
}
//-----End of program-----------------

Reference address:Design of password lock controlled by single chip microcomputer

Previous article:Design of Simple Timing Alarm Circuit Controlled by Single Chip Microcomputer
Next article:Design of automatic detection system for four parameters of battery based on AT89C52

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号