Universal password lock controlled by 51 single chip microcomputer

Publisher:lidong4069Latest update time:2022-01-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Initial password: 12345678

This program is implemented by using 51 single chip microcomputer to control 74LS164 latch

The MCU and the memory use IIC communication to establish the cryptographic system communication.

Simulation starts and password is set

Universal password lock reference program source code:


#include

#include "intrins.h"

#include "key.h"

#include "xsh.h"

#include "24c02.h"

#define ulong unsigned long

bit BJCB=0; //Alarm flag

XG=0, //Change password flag

BJ=0, //Alarm flag

SJ=0; //Keyboard lock flag

// xg=0; //Change password flag

uchar PSWD0_0[16]={0}; //password input cache storage unit

//uchar code PSWD0_1[16]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8}; //User initial password storage unit

//uchar PSWD1_0[16]={0}; //Read the password cache storage unit

uchar PSWD1_1[16]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8}; Administrator initial password storage unit

sbit OUT=P3^5; //output port

sbit L1=P0^2; //Power-on indication

sbit L2=P0^1; //Unlock indication

sbit L3=P0^0; //Alarm indication

uint mg=0, //Number of passwords

KSC=0, //Number of unlocking times

BJC=0, //Number of alarms

TCNTY=0, //User key timing count accumulation

TCNTG=0, //Timed number of times the administrator enters the password

yhsh, //User input flag

glsh, //Administrator input flag

shw; // Password input completed flag

void yhmimaxig(); //password modification procedure

void glmimaxig(); //Administrator modifies the program

void bjyin(ulong t)

{

ulong c;

uint n;

for(c=0;c{

for(n=0;n<50;n++); //delay

BEEP=~BEEP; //Invert the signal output to the speaker

}

}

void delay_10ms(void)

{

uint i = 1000;

while(i–);

}

//Timer interrupt service function//

void timer0() interrupt 1

{

uint i;

TH0=(65536-50000)/256; //Assign 50MS timing to TH0 TL0

TL0=(65536-50000)%256; //Reload count initial value

if(yhsh1)

{

TCNTY++; //User timing count

if(shw1)

{

TR0=0;

TCNTY=0;

}

if(TCNTY400)

{

TR0=0; //turn off T0 timer

TCNTY=0;

mg=0; //Password number cleared

L1=1;

for(i=0;i<2;i++)//sound and light alarm circuit

{

L3=0;

bjyin(1000);

L3=1;

delay(1000);

}

L1=0;

}

}

else if(glsh1)

{

TCNTG++; //Administrator timing count

if(shw1)

{

TR0=0;

TCNTG=0;

}

if(TCNTG400)

{

TR0=0; //turn off T0 timer

TCNTG=0;

mg=0; //Password number cleared

L1=1;

for(i=0;i<2;i++)//sound and light alarm circuit

{

L3=0;

bjyin(1000);

L3=1;

delay(1000);

}

L1=0;

}

}

}


void main()

{

uint i;

TMOD=0x01; //Timer works in mode 1

ET0=1;

EA=1;

TH0=(65536-50000)/256; //Assign value to TH0 TL0

TL0=(65536-50000)%256; // Make the timer interrupt once every 0.05 seconds

//Read_page24c02(0x00,PSWD1_1,16); //Read the user's initial password from 24c02

//i2cWrite24LC16B(0,0X20);

/Write the initial administrator password, user password, and unlock times into 24c02 for the first time/

for(i=0;i<16;i++) //Write one at a time to 24c02

{

i2cWrite24LC16B(PSWD1_1[i],i);

delay_10ms;

}

for(i=0;i<16;i++) //Write one at a time to 24c02

{

i2cWrite24LC16B(PSWD1_1[i],0x10+i);

delay_10ms;

} //Used during proteus simulation, when burning the program, use it for the first time and not for the second time.

KSC=i2cRead24LC16B(0X20);

loop: L1=0; //The red light turns on when powered on

L2=1; //The green light turns on after unlocking successfully

L3=1; // Yellow light turns on to alarm when input is wrong

OUT=0; //Open signal close

display_0(KSC); //Display before unlocking


///Password input/

if(SJ0) //keyboard lock flag is 0

{

do

{

flag=0; //Keyboard flag

key();

if(flag1&&num12) //Judge whether the administrator function key is pressed

{

goto gl;

}

if(flag1&&num<=9)

{

PSWD0_0[mg]=num;

mg++; //Number of passwords

}

if(flag1&&num14&&mg>=1)//delete input error

{

mg=mg-1;

}

if(mg1) //A password key is pressed to start the timer

{

TR0=1;

yhsh=1;

}

}

while(num!=15);

if(mg!=16)

{

for(i=mg;i<16;i++)

{

PSWD0_0[i]=0;

}

}

/Password comparison circuit

if(flag1&&num==15)

{ flag=0;

yhsh=0;

shw=1;

num=0;

for(i=0;i<16;i++) //Read 16 passwords from 24c02

{

PSWD1_1[i]=i2cRead24LC16B(i);

delay_10ms;

}

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

{

if(PSWD1_1[i]!=PSWD0_0[i])

{

BJCB=1;

break;

}

else

BJCB=0;


}

}

if(BJCB!=0)

{

BJC++;

if(BJC!=3)

{

BJCB=0; //First and second alarms

delay(500);

mg=0; //Password number cleared

L1=1;

for(i=0;i<3;i++)//sound and light alarm circuit

{

L3=0;

display_3();

bjyin(1000);

L3=1;

delay(1000);

}

goto loop;

}

else if(BJC3)

{

BJC=0;

BJCB=0;

display_5(); //Failed to enter the password three times, self-lock

SJ=1; //Keyboard lock flag

mg=0; //Password number cleared

L1=1;

for(i=0;i<2;i++)//sound and light alarm circuit

{

L3=0;

bjyin(1000);

L3=1;

delay(1000);

}

goto loop;

}

}

else

if(BJCB0)

{

KSC++;

display_1(KSC);

i2cWrite24LC16B(KSC,0X20);

if(KSC100)

{

SJ=1;

}

L2=0;

L1=1;

OUT=1;

mg=0; //Password number cleared

loop1:do //keyboard scan and wait for function key to be pressed

{

flag=0;

key();

if(flag)

{

flag=0;

switch(num)

{

case 13:

{

display_2();

do

{

flag=0; //Keyboard flag

key();

if(flag1&&num<=9)

{

PSWD0_0[mg]=num;

mg++; //Number of passwords

}

if(flag1&&num14&&mg>=1)//delete input error

{

mg=mg-1;

}

if(mg16)

{

do //Keyboard scan waiting for function key to be pressed

{

flag=0;

key();

}

while(num!=15);

}

}

while(num!=15);

if(mg!=16&&num15)

{

for(i=mg;i<16;i++)

{

PSWD0_0[i]=0;

}

}

if(flag1&&num15)

{

flag=0;

mg=0;

for(i=0;i<16;i++) //Write one at a time to 24c02

{

i2cWrite24LC16B(PSWD0_0[i],i);

delay_10ms;

}

display_1(KSC);

do //Keyboard scan waiting for function key to be pressed

{

flag=0;

key();

}

while(num!=11); //Press the return key and the lock will return

{

goto loop;

}

}

};break;


case 11: {

goto loop;

//break;

}

}

}

}

while(flag!=0); //If the password key is pressed

goto loop1;

// i2cWrite24c02_page(0x00,PSWD1_1,mg);

}

}

else

{

do

{

flag=0;

key();

}

while(num!=12);//Judge whether the administrator function key is pressed

gl:do//Administrator enters password

{

flag=0;

key();

if(flag1&&num<=9)

{

PSWD0_0[mg]=num;

mg++; //Number of passwords

}

if(flag1&&num14&&mg>=1)//Delete key is pressed

{

mg=mg-1;

}

if(mg1)

{

TR0=1;

}//Open the timer to unlock in 5 seconds

}

while(num!=15);

if(mg!=16&&num15)

{

for(i=mg;i<16;i++)

{

PSWD0_0[i]=0;

}

}

//Compare the administrator password///

//Read_page24c02(0x10,PSWD0_1,16)

for(i=0;i<16;i++) //Read 16 passwords from 24c02

{

PSWD1_1[i]=i2cRead24LC16B((0x10+i));

delay_10ms;

}

if(flag1&&num==15)

{ flag=0;

shw=1;

num=0;

for(i=0;i{

if(PSWD1_1[i]!=PSWD0_0[i])

{ BJCB=1;

break;

}

else

BJCB=0;

}

}

if(BJCB!=0)

{

BJC++;

if(BJC!=3)

{

BJCB=0; //First and second alarms


mg=0; //Password number cleared

L1=1;

for(i=0;i<2;i++)//sound and light alarm circuit

{

L3=0;

display_3();

bjyin(1000);

L3=1;

delay(1000);

}

display_1(KSC);

goto loop;

}

else

{

BJCB=0;

display_5(); //Failed to enter the password three times, self-lock

mg=0; //Password number cleared

delay(10000);

display_1(KSC);

goto loop;

}

}

else

{

SJ=0;

display_4(); //Administrator unlock display

mg=0;

L2=0;

L1=1;

L3=0; //Password number cleared

do //Keyboard scan waiting for function key to be pressed

{

flag=0;

key();

}

while(flag0); //If the password key is pressed

if(flag1&&num13)

{

flag=0;

display_2();

do

{

flag=0; //Keyboard flag

key();

if(flag1&&num<=9)

{

PSWD0_0[mg]=num;

mg++; //Number of passwords

}

if(flag1&&num14&&mg>=1)//delete input error

{

mg=mg-1;

}

if(mg16)

{

do //Keyboard scan waiting for function key to be pressed

{

flag=0;

key();

}

while(num!=15);

}

}

while(num!=15);

if(mg!=16&&num15)

{

for(i=mg;i<16;i++)

{

PSWD0_0[i]=0;

}

}

if(flag1&&num15)

{

flag=0;

mg=0;

for(i=0;i<16;i++) //Write one at a time to 24c02

{

i2cWrite24LC16B(PSWD0_0[i],(0x10+i));

delay_10ms;

}

display_4();

do //Keyboard scan waiting for function key to be pressed

{

flag=0;

key();

}

while(num!=11); //Press the return key and the lock will return

{

goto loop;

}

}

}

if(flag1&&num10)

{

KSC=0;

i2cWrite24LC16B(KSC,0X20);

display_1(KSC);

do //Keyboard scan waiting for function key to be pressed

{

flag=0;

key();

}

while(num!=11);//Press the return key to return

goto loop;

}

if(flag1&&num11)//Press the return key and the lock will return

{

goto loop;

}

}

}

}

Reference address:Universal password lock controlled by 51 single chip microcomputer

Previous article:Design of a mobile phone Bluetooth remote control car controlled by 51 single chip microcomputer
Next article:Ultrasonic distance measurement based on STC51 single chip microcomputer

Latest Microcontroller Articles
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号