8051 code for Password Locking System
This program is to take input from the keypad as the password which is set by the user and displays whether the password is correct or incorrect on LCD.
ALGORITHM
1)Initializing the password in an array called pass.
2)Taking the inputs from the keypad.
3)The flag variable keeps the count of number pressed.
4) As shown below program contains 4 password letters if enters into the if loop if four letters are pressed and compares the pass array with the user pressed array.
5) If each letter is correct it increments the status variable to ensure all the letters
are matched.
6) When status is 4 it display Correct password on LCD else displays Incorrect password on LCD.
CODE GOES HERE
#include<at89c51xd2.h>
void lcd_int(void); // function to initialize the lcd commands
void delay(int); // function for delay
void lcd_comm(void); // function to set lcd commands
void lcd_data(void);// function to set lcd data
void display(int x);// function to display the content required to display on lcd
void keypad(void);// function to take inputs from the keypad
int x,j,i,k=0,flag=0,status=0;
unsigned int temp1,temp2,var;
unsigned char chk[4];
sbit rs=P1^7;
sbit en=P1^6;
void main()
{
lcd_int();
while(1)
{
unsigned char data2[]={"Incorrect_passward"};//initialzing the array to display on the lcd
temp1=0x80; // sets cursor to line 1 of display
lcd_comm();
delay(50000);
for(j=0;j<18;j++)
{
temp2=data2[j];// passing each value of array
lcd_data();
delay(50000);
}
keypad();
temp1=0x01;// command to clear the display
lcd_comm();
delay(20000);
}
}
void lcd_int(void)
{
unsigned int cmd[]={0x20,0x28,0x0e,0x06,0x80,0x01};
for(j=0;j<7;j++)
{
temp1=cmd[j];
lcd_comm();
delay(500);
}
}
void lcd_comm(void)
{
var=temp1;
temp1=temp1 & 0xf0;
P1=temp1>>4;
rs=0;
en=1;
en=0;
P1=var & 0x0f;
rs=0;
en=1;
en=0;
delay(60);
}
void lcd_data(void)
{
var=temp2;
temp2=temp2 & 0xf0;
P1=temp2>>4;
rs=1;
en=1;
en=0;
P1=var & 0x0f;
rs=1;
en=1;
en=0;
delay(6000);
}
void delay(int x)
{
int i=0;
for(i=0;i<x;i++);
}
void keypad(void)
{
unsigned char R[4]={0xfe,0xfd,0xfb,0xf7};
unsigned char r[9]={0x7e,0xbe,0xde,0x7d,0xbd,0xdd,0x7b,0xbb,0xdb};
while(1)
{
for(i=0;i<4;i++)
{
P0=R[i];
for(i=0;i<9;i++)
{
if(P0==r[i])
{
display(i);
}
}
delay(10000);
}
}
}
void display(int x)
{
unsigned char R[9]={1,2,3,4,5,6,7,8,9};
unsigned char data1[]={"correct_password"};
unsigned char data2[]={"Incorrect_password"};
unsigned char pass[4]={1,5,3,7};
temp2=R[x];
chk[k]=R[x];
k++;
flag++;// keeping the count of keys
lcd_data();
delay(50000);
if(flag==4) //minimum requirement of password is four if four keys are pressed it checks for 4
{
for(i=0;i<4;i++)// loop to check the correct password
{
if(chk[i]==pass[i])
{
status++;
}
}
if(status==4) // if the correct keys are pressed status turns to be 4 and it comes inside this loop
{
for(j=0;j<16;j++) // it displays the data assigned when password is correct
{
temp2=data1[j];
lcd_data();
delay(50000);
}
}
else
{
for(j=0;j<18;j++) // it displays the data assigned for incorrect password
{
temp2=data2[j];
lcd_data();
delay(50000);
}
}
}
}