Design of password lock based on 51 microcontroller – LCD1602 display

Password lock design based on 51 microcontroller

(Simulation + Program + Schematic + Design Report)

Specific functions:

1. Use the 4*4 matrix keyboard to set an 8-digit password. If the password is correct, the lock will open. LCD1602 displays open!

2. You can change the password (8-digit password), and the lock must be open to change the password. To prevent misuse, you need to enter the password twice.

3. If the incorrect password is entered more than 3 times, the buzzer will sound an alarm and the keyboard will be locked.

4. It has the function of resetting and saving the password after power failure.

5. The brightness of the LCD screen will automatically adjust according to the light.

Code

void DisplayInput(void)
{
unsigned char i;
if(CorrectCont==1)
{
//DisplayListChar(0,0,Input);
write_1602com(er);
for(i=0;i<16;i + + )
{
write_1602dat(Input[i]); //Display Input
}
}
}


//========================Reset password====================== =============================
//================================================ ==================================
void ResetPassword(void)
{
unsigned char i;
unsigned char j;
if(pass==0)
{
pass=0;
DisplayChar(); //Display error
ThreeAlam(); //When the lock is not unlocked, press the reset password and the alarm will sound 3 times
}
else //The password reset procedure can only be performed when the lock is unlocked.
{
    if(ReInputEn==1) //In the unlock state, ReInputEn is set to 1, and password reset is allowed.
{
if(N==8) //Enter 8-digit password
{
ReInputCont + + ; //Password count
if(ReInputCont==2) //Enter password twice
{
for(i=0;i<8;)
{
if(TempPassword[i]==InputData[i]) //Compare the new password entered twice
i + + ;
else //If the two passwords are different
{
//DisplayListChar(0,1,Error);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(Error[j]); //Display error Error
}
ThreeAlam(); //Error message
pass=0; //Close lock
ReInputEn=0; //Turn off the reset function,
ReInputCont=0;
DisplayChar();
// ReInputCont=0; //
break;
}
}
if(i==8)
{
//DisplayListChar(0,1,ResetOK);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(ResetOK[j]); //Password changed successfully, display
}

TwoAlam(); //Operation success prompt
WrToROM(TempPassword,0,8); //Write the new password to 24C02 storage
ReInputEn=0;
}
ReInputCont=0;
CorrectCont=0;
}
else //When entering a password once
{
OneAlam();
//DisplayListChar(0, 1, again); //Display input again
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(again[j]); //Display input again
}
for(i=0;i<8;i + + )
{
TempPassword[i]=InputData[i]; //Temporarily store the first input data
}
}
N=0; //Clear the input data digit counter
}
}
}

}



//========================If you enter the wrong password more than three times, the alarm will be reported and the keyboard will be locked============== =========
void Alam_KeyUnable(void)
{
P1=0x00;
{
ALAM=~ALAM; //The buzzer keeps flashing and sounding
ALAM_LED=!ALAM_LED;
Delay5Ms();
}
}


//========================Cancel all operations====================== ======================
void Cancel(void)
{
unsigned char i;
unsigned char j;
//DisplayListChar(0, 1, start_line);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(start_line[j]); //Display the boot password input interface
}
TwoAlam(); //prompt tone
for(i=0;i<8;i + + )
{
InputData[i]=0; //Clear the input password to zero
}
KEY=1; //Close the lock
ALAM=1; //alarm off
ALAM_LED=1;
operation=0; //Clear the operation flag bit
pass=0; //clear password correct flag
ReInputEn=0; //Reset input permission flag to zero
ErrorCont=0; //Reset the number of incorrect password inputs to zero
CorrectCont=0; //Reset the number of correct password entries to zero
ReInputCont=0; //Reset password input times to zero
open_led=1; //Unlock LED turns off (optional)
s3_keydown=0;
key_disable=0; //Clear the lock keyboard flag to zero
N=0; //Clear the input digit counter
}


//========================== Confirm key and execute the corresponding function through the corresponding flag =========== ====================
void Ensure(void)
{
unsigned char i,j;
RdFromROM(CurrentPassword,0,8); //Read the stored password from 24C02
    if(N==8)
{
TR1=0;
sec=0;
if(ReInputEn==0) //Reset password function is not enabled
{
for(i=0;i<8;)
   {
if(CurrentPassword[i]==InputData[i]) //Determine whether the input password is the same as the password in 24c02
{
i + + ; //The same bit i is + 1
}
else //if there are different passwords
{
ErrorCont + + ; //Number of errors + +
if(ErrorCont==3) //When the wrong input count reaches three times, alarm and lock the keyboard
{
write_1602com(er);
for(i=0;i<16;i + + )
{
write_1602dat(Error[i]);
}
do
Alam_KeyUnable();
while(1);
}
else //When the number of errors is less than 3, lock the keyboard for 3 seconds, and then you can enter again
{
TR0=1; //Turn on timing
key_disable=1; //Lock the keyboard
pass=0; //clear pass bit
break; //jump out
}
}
}

if(i==8) //Password input time
{
CorrectCont + + ; //Enter the correct variable + +
if(CorrectCont==1) //Correct input count, when there is only one correct input, unlock
{
//DisplayListChar(0,1,LockOpen);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(LockOpen[j]); //Display the open unlock screen
}
TwoAlam(); //Successful operation sound
KEY=0; //Unlock
pass=1; //Set the correct flag
TR0=1; //Turn on timing
open_led=0; //Unlock indicator light is on (optional)
for(j=0;j<8;j + + ) //Clear the input
{
InputData[i]=0; //Clear the input bit after unlocking
}
}
else //When entered correctly twice, turn on the password reset function
{
//DisplayListChar(0,1,SetNew);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(SetNew[j]); //Display password reset interface
}
TwoAlam(); //Operation success prompt
ReInputEn=1; //Allow reset password input
CorrectCont=0; //Correct counter cleared
}
}
\t
else //==========================When using it for the first time or forgetting the password, you can use 131420 to initialize the password====== ======
{
if((InputData[0]==5) & amp; & amp;(InputData[1]==1) & amp; & amp;(InputData[2]==2) & amp; & amp;(InputData[ 3]==1) & amp; & amp;(InputData[4]==1) & amp; & amp;(InputData[5]==3) & amp; & amp;(InputData[6]==1 ) & amp; & amp;(InputData[7]==4))
{
WrToROM(initpassword,0,8); //Force the initial password to be written to 24C02 storage
//DisplayListChar(0,1,initword); //Display initialization password
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(initword[j]); //Display initialization password
}
TwoAlam(); //success sound
Delay400Ms(); //Delay 400ms
TwoAlam(); //success sound
N=0; //Clear the input digit counter
}
else //Password input error
{
//DisplayListChar(0,1,Error);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(Error[j]); //Display error information
}
 ThreeAlam(); //Error sound
pass=0;
}
}
}

else //When the password reset function is turned on and the unlock key is pressed,
{
//DisplayListChar(0,1,Er_try);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(Er_try[j]); //Error, please re-enter
}
ThreeAlam(); //Error sound
}
}

else //When the password is not entered into 8 digits, when the confirm key is pressed
{
//DisplayListChar(0,1,Error);
write_1602com(er);
for(j=0;j<16;j + + )
{
write_1602dat(Error[j]); //Display error
}

 ThreeAlam(); //Error sound
pass=0;
}
\t
N=0; //Clear the input data counter to prepare for the next input

operation=1;
}


//==============================Main function================ ===============
void main(void)
{
 unsigned char KEY,NUM;
unsigned char i,j;
 P1=0xFF;
TMOD=0x11;
 TL0=0xB0;
 TH0=0x3C;
TH1=0x4b;
TL1=0xff;
 EA=1;
 ET0=1;
ET1=1;
 TR0=0;
TR1=0; //Turn on the timer switch
 Delay400Ms(); //Start waiting and wait for LCM to enter the working state
 lcd_init(); //LCD initialization
write_1602com(yi);//Calendar display fixed symbols start displaying after the 0th position on the first line
for(i=0;i<16;i + + )
{
write_1602dat(name[i]);//Write the startup screen to the LCD screen
}
write_1602com(er);
for(i=0;i<16;i + + )
{
write_1602dat(start_line[i]);//Write input password waiting interface
}
write_1602com(er + 9); //Set the cursor position
write_1602com(0x0f); //Set the cursor to flash
 Delay5Ms(); //Delay for a moment (optional)

 N=0; //Initialize the number of data input digits
 while(1) //Enter the loop
 {
if(key_disable==1) //Lock keyboard flag is 1
Alam_KeyUnable(); //Alarm keyboard lock
else
ALAM=1; //turn off alarm

KEY=keynum(); //Read the position code of the key
if(KEY!=0) //When a key is pressed
{
if(key_disable==1) //Lock keyboard flag is 1
{
second=0; //clear seconds
}
else //When the keyboard is not locked
{
NUM=coding(KEY); //Encode the key according to its position, and assign the encoding value to NUM
{
switch(NUM) //Determine the key value
{
case ('A'): ; break;
case ('B'): ; break;
case ('C'): ; break; //ABC is an undefined key
case ('D'): ResetPassword(); break; //Reset password
case ('*'): Cancel(); break; //Cancel the current input
case ('#'): Ensure(); break; //Confirm key,
default: //If the function key is not pressed, it is the numeric key.
{
//DisplayListChar(0,1,Input);
if(N==0)
{
write_1602com(er);
for(i=0;i<16;i + + )
{
write_1602dat(Input[i]); //Display input screen
}
}
\t\t\t\t\t\t\t\t
operation=0; //Operation flag cleared
if(N<8) //When the entered password is less than 6 characters, accept the input and save it. If it is greater than 6 characters, it will be invalid.
{
OneAlam(); //Button tone
//DisplayOneChar(6 + N,1,'*');
for(j=0;j<=N;j + + )
{
write_1602com(er + 6 + j); //The number of displayed digits increases as the input increases
write_1602dat('*'); //But do not display the actual number, use * instead
}
InputData[N]=NUM; //Assign the code of the numeric key to the InputData[] array for temporary storage
N + + ; //Password digits added
}
else //After the input data digits are greater than 6, ignore the input
{
N=8; //When the password input is more than 6 characters, the input will not be accepted.
break;
}
if(ReInputEn==0)
{
TR1=1;
}
}
}
}
}
}
}
}

//************************************Interrupt service function************ ***************************
void time0_int(void) interrupt 1 //Timer T0
{
 TL0=0xB0;
 TH0=0x3C; //Reassign timer initial value
 //TR0=1;
 countt0 + + ; // Timing variable addition, 50ms when added once
  if(countt0==20) //Add to 20 times and it will be 1s
   {
countt0=0; //clear variable
second + + ; //Add seconds
if(pass==1) //When in unlocking state
{
if(second==1) //When the second is added to 1s
{

TR0=0; //Close timer
TL0=0xB0;
 TH0=0x3C; //Assign initial value again
second=0; //clear seconds
}
}
else //Not in the unlocking state
{
if(second==3) //When seconds add to 3
{
TR0=0; //Close the timer
ALAM_LED=1;
second=0; //clear seconds
key_disable=0; //Lock keyboard clear
s3_keydown=0;
TL0=0xB0;
TH0=0x3C; //Reassign initial value
}
else
TR0=1; //Open timer
}
\t\t\t
   }
}

void time1_int(void) interrupt 3
{
uchar i1,m;
TH1=0x4b;
TL1=0xff;
m + + ;
if(m==20)
{
m=0;
sec++;
if(sec==10)
{
sec=0;
// ALAM_LED=1;
if(pass==1)
TR1=0;
else if(pass==0)
{
for(i1=0;i1<30;i1 + + )
{
ThreeAlam();
\t\t\t\t\t
}
\t\t\t\t
N=0;
write_1602com(er);
for(i1=0;i1<16;i1 + + )
{
write_1602dat(Input[i1]);
}
TR1=0;
}
}
}
}

Hardware Design

Use components:

Microcontroller: STC89C52;

(Note: The microcontroller is universal, whether it is 51 or 52, whether it is stc or at, the pin functions are the same. The program is also the same.)

IC seat: DIP40;

Memory chip: AT24c02; IC holder: DIP8;

LCD screen: LCD1602; Pin header: 5.08*16;

Seat arrangement: 5.08*16; Potentiometer: 103;

Buzzer: 5V active; Transistor: 9012;

Resistor: 1k; exclusion: 1k*9;

Electrolytic capacitor: 10uf; Button: 6*6;

Relay: 5V; Terminal: 2P;

Terminal block: 3P; Self-locking switch: 8*8;

Universal board: 9*15; Wires: several;

Flowchart:

Design information

01 Simulation diagram

This design uses two versions of proteus7.8 and proteus8.7, and is backward compatible, no need to worry! Specifically as shown in the picture!

02 Schematic

The schematic diagram of this system is designed using Altium Designer19, as shown in the figure!

03 Program

This design is programmed using two versions of the software keil4 and keil5, so there is no need to worry! Specifically as shown in the picture!

04 Design Report

Fifteen thousand words design report, details are as follows!

05 Data Map

All materials include simulation source files, programs (including comments), AD schematics, proposal reports, reference papers, flow charts, task books, etc. The specific content is as follows, the most complete on the entire network! !

The following is the design information of 51 microcontroller

?Everyone learns and makes progress together:

Redirecting…icon-default.png?t=N7T8https://link.zhihu.com/?target=https://pan.baidu.com/s/1xJgNMuQVEg0J3KoUuVfhcw?pwd =tt1s

Extraction code: tt1s

?Like and share to learn and progress together!