Peoteus software simulation implementation of mobile phone password lock based on 51 single-chip microcomputer

Knowledge points involved in this project

  • hardware
    • Matrix keyboard principle
    • Digital tube dynamic display
    • timer interrupt
  • software
    • Key event response
    • Password lock status switching
  • Summarize

Hardware

Matrix keyboard principle

 very basic

Dynamic display of digital tube

 is also very basic. The main problem encountered at the beginning was the afterglow problem of the LED (because after switching the bit code,
Afterglow stayed for a short time due to some principle, resulting in the appearance of garbled characters. Mainly to suspend Proteus simulation,
The nixie tube has no afterglow, but still can't see the problem. )

Timer interrupt

 Slightly more advanced. After the game timer was interrupted, I didn't even bother to use Delay to delay the timing.

Software

 The most troublesome place

Key event response

 Adhering to the idea of high cohesion and low coupling, try to modularize the functions.

Password lock status switch

 is simpler



\t?

Summary

 One-time note-taking, mainly because it is troublesome to flip through it from the computer.
In addition, the program also has areas that can be improved. I think there are two points that can be improved:
1. When the button is pressed, the program enters an endless loop until the button is released.
It can be modified to trigger the system event response when the button is pressed, that is, the system only responds to the falling edge of the button (level).
2. The most repetitive part of the whole program lies in the specific implementation of event response. My design is to separate each state and make judgments separately.
This also leads to verbosity in the code. I believe that this part of the code here will definitely have a more efficient and concise implementation, but the current execution logic is also good, but the code does not look good.

May add something later if the whim strikes.

Proteus circuit diagram
Please add picture description

#include "reg52.h"
//Imitate mobile phone password lock, 6-digit password.
//The phone is locked for 10s after the user enters wrongly for 3 consecutive times
//The default password is 000000;
//It has the functions of clearing, backspace, changing password, locking, and locking screen.

// There are 4 states in total:
//Default state 0: pending input state;
//Status 1: Unlocked status All digital tubes display: U(unlock)
//Status 2: Lock mode All digital tubes display: L(lock)
//Status 3: password modification mode The decimal points of all digital tubes light up, indicating that it is currently in password modification mode.

//16 buttons:
//The ten keys are used to represent the numbers 0~9,
//The remaining six keys: one key represents backspace, one key represents backspace, one key represents confirmation, one represents lock screen, and the remaining two keys are reserved (meaningless)


//Timer section:
void T0_Init()//timer initialization
{<!-- -->
TMOD|=0X01;//Select as timer 0 mode, working mode 1; select as timer 1 mode, working mode 1
TH0=0XFC; //Assign initial value to timer 0, timing 1ms
TL0=0X18;

EA=1;//Open total interrupt
ET0=1;//Enable timer 0 interrupt enable

TR0=0;//Turn off timer 0, one purpose is to time the screen lock time, and it will be turned on after the screen lock event occurs.
}


//add function:
unsigned char NumOfConsecutiveErrors = 0;//The number of consecutive errors, if it reaches 3 times, the screen will be locked for 10s.

//The default is to lock the screen and wait for the password to be entered. At this time, the user needs to press the key to enter the password.
unsigned char code NumTable[]={<!-- --> 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};//0~9
unsigned char DuanMa[6]={<!-- --> 0xf7, 0xf7, 0xf7, 0xf7, 0xf7, 0xf7};
unsigned char PassWord[6]={<!-- -->0x00,0x00,0x00,0x00,0x00,0x00};//The default password is 6 zeros.
unsigned char InputWord[6]={<!-- -->0xff,0xff,0xff,0xff,0xff,0xff};//Record the password entered by the user.



unsigned char WeiMa[]={<!-- --> 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 };
//Password input part:
unsigned char nowtoInputBit = 0; //0 1 2 3 4 5 , indicating which nixie tube corresponds to the number to be input. from left to right.
unsigned char nowState = 0;//The default state 0 is the password input state, 1 represents the unlocked state, 2 represents the locked state,
unsigned char keyMap[]={<!-- -->0x01,0x02,0x03,0xff,
0x04,0x05,0x06,0xff,
0x07,0x08,0x09,0xff,
0xff,0x00,0xff,0xff,}; //The range of ButtonIndex is 0~15, and the key mapping is realized through this array.


//Matrix button part
unsigned char Row[4]={<!-- -->0xfe,0xfd,0xfb,0xf7};
sbit L1 = P3^4;
sbit L2 = P3^5;
sbit L3 = P3^6;
sbit L4 = P3^7;
unsigned char ButtonIndex;

//function part:
void Delayms(unsigned int ms) //delay function
{<!-- -->
unsigned int i,j;
for (i=0;i<ms;i + + )
for(j=0;j<123;j++);
}
//Password input:
void ButtonPressedAction()
{<!-- -->
static unsigned char i,j;
switch(nowState)
{<!-- -->
case 0://Password input status:
switch(ButtonIndex)
{<!-- -->
case 3://clear key
for(i=0;i<6;i ++ )//clear and reset.
{<!-- -->
DuanMa[i]=0xf7;
}
nowtoInputBit=0;
break;
case 7://backspace key
if(nowtoInputBit>0)
{<!-- -->
nowtoInputBit--;
DuanMa[nowtoInputBit]=0xf7;
}
break;
case 11://change password key
//The password input state naturally does not allow changing the password, so this key is invalid.
break;
case 12://fingerprint key
break;
case 14://bit left shift key
//The password input state naturally does not allow changing the password, so this key is invalid.
break;
case 15://value increase key
//The password input state naturally does not allow changing the password, so this key is invalid.
break;
default: //A total of 16 keys, 6 keys are excluded, and the rest are numbers 0~9.
DuanMa[nowtoInputBit]=NumTable[keyMap[ButtonIndex]];
InputWord[nowtoInputBit]=keyMap[ButtonIndex];//Record the entered password.
nowtoInputBit++;
break;
}
if(nowtoInputBit==6)//All 6-digit passwords have been entered
{<!-- -->
nowtoInputBit=0;//"pointer homing"
//Check if the password is correct:
for(i=0;i<6;i ++ )
{<!-- -->
if(InputWord[i]!=PassWord[i])
{<!-- -->
//The password is wrong, execute the command:
NumOfConsecutiveErrors + + ;//Add one to the number of consecutive wrong passwords.
if(NumOfConsecutiveErrors==3)//If there are three consecutive errors, it will be locked, and if it is 5 times, it will be too many times.
{<!-- -->
// Enter lock mode. Lasts for 10s.
nowState=2;
NumOfConsecutiveErrors=0;
for(j=0;j<6;j ++ )//clear and reset. //Using j is to avoid i error. (see below)
{<!-- -->
DuanMa[j]=0xc7;//Display "L" (means locked)
}
// Turn on the timer and switch to the default input mode after 10s.
TR0=1;
break;

}
for(j=0;j<6;j ++ )//clear and reset. //Using j is to avoid i error. (see below)
{<!-- -->
DuanMa[j]=0xf7;
}
break;

}
}
if(i==6)//The password is correct, execute the command:
{<!-- -->
NumOfConsecutiveErrors=0;//Clear the number of consecutive password errors.
nowState=1; //Switch the current state to the unlocked state, and change DuanMa to display 6 U (meaning: unlock)
for(i=0;i<6;i ++ )
{<!-- -->
DuanMa[i]=0xc1;
}
}
}
break;
case 1:// In unlocked state: display 6 U (meaning: unlock)
if(ButtonIndex==11)//Enter the password change mode, and the decimal points of all digital tubes light up in this mode.
{<!-- -->
nowState=3;
for(i=0;i<6;i ++ )//clear and reset.
{<!-- -->
DuanMa[i]=0x7f;
}
}
if(ButtonIndex==15)//Enter the default input mode
{<!-- -->
nowState=0;
for(i=0;i<6;i ++ )//clear and reset.
{<!-- -->
DuanMa[i]=0xf7;
}
}
break;
case 2://in locked state:
break;
case 3://in password change mode:
switch(ButtonIndex)
{<!-- -->
case 3://clear key
for(i=0;i<6;i ++ )//clear and reset.
{<!-- -->
DuanMa[i]=0x7f;
}
nowtoInputBit=0;
break;
case 7://backspace key
if(nowtoInputBit>0)
{<!-- -->
nowtoInputBit--;
DuanMa[nowtoInputBit]=0x7f;
}
break;
case 11://change password key
//The password input state naturally does not allow changing the password, so this key is invalid.
break;
case 12://reserved key
break;
case 14://reserved key
break;
case 15://lock screen key
// Only valid in the unlocked state.
break;
default: //A total of 16 keys, 6 keys are excluded, and the rest are numbers 0~9.
DuanMa[nowtoInputBit]=NumTable[keyMap[ButtonIndex]] & amp;0x7f; //Light up the decimal point to let the user know that the password is currently being changed.
PassWord[nowtoInputBit]=keyMap[ButtonIndex];//Overwrite the original password.
nowtoInputBit++;
break;
}
if(nowtoInputBit==6)//All 6-digit new passwords have been entered
{<!-- -->
nowtoInputBit=0;
nowState=0;//Switch to the default input mode;
for(i=0;i<6;i ++ )//clear and reset.
{<!-- -->
DuanMa[i]=0xf7;
}
}
break;
}
}
//The digital tube display:
void ShowSeg()
{<!-- -->
static unsigned char i;
for(i=0;i<6;i ++ )
{<!-- -->
P2=WeiMa[i];
P0=DuanMa[i];
\t\t
Delayms(5);
P2=0x00;
}
}
//Implementation of the matrix keyboard:
void ScanMatrix()
{<!-- -->
static unsigned char i;
for(i=0;i<4;i ++ )
{<!-- -->
P3=Row[i];//Scan row
if(P3!=Row[i])
{<!-- -->
Delayms(8);
if(P3!=Row[i])
{<!-- -->
if(L1==0){<!-- -->
ButtonIndex = i*4;
ButtonPressedAction();//Trigger the event that the button is pressed.
while(!L1);
break;}
else if(L2==0){<!-- -->
ButtonIndex = i*4 + 1;
ButtonPressedAction();//Trigger the event that the button is pressed.
while(!L2);
break;}
else if(L3==0){<!-- -->
ButtonIndex = i*4 + 2;
ButtonPressedAction();//Trigger the event that the button is pressed.
while(!L3);
break;}
else if(L4==0){<!-- -->
ButtonIndex = i*4 + 3;
ButtonPressedAction();//Trigger the event that the button is pressed.
while(!L4);
break;}
}
}
}
}

void main()
{<!-- -->
T0_Init();
while(1)
{<!-- -->
ScanMatrix();
ShowSeg();
P1= ButtonIndex;
}
}
void time0() interrupt 1 //timer 0 interrupt
{<!-- -->
static unsigned int i;//Define static variable i
TH0=0XFC; //Assign an initial value to the timer, timing 1ms
TL0=0X18;
i + + ;
if(i==10000)//Delay 10000ms, that is, 10s
{<!-- -->
// perform the operation.
//Switch from locked state to default input state:
nowState=0;
for(i=0;i<6;i + + )//Reset the nixie tube to zero.
{<!-- -->
DuanMa[i]=0xf7;
}
\t\t
i=0;//The value is cleared to zero.
TR0=0;//Turn off timer 0.
}
}