In this article we will discuss about how to take input from user as username/password and check it for logging in. We will be using lcd for display the input and the status of logging in or any error.
Here we are using following components:
- P89V51RD2 microcontroller for programming in keil
- 80C51 microcontroller for simulation in proteus
- LM016L as 16x2 character LCD
- 4x4 matrix keypad.
Steps involved in program are as follows:
- First we will take input from user for username
- if username is wrong then we will clear entered username from array and we will set flags to default.
- if username is right then we will ask for password
- if password is wrong then we will clear entered username and password and we will set all flags to default.
- if password is right then we will display logged in screen.
- during above process if reset key is pressed then everything will be set to default
For how to use 16x2 character LCD and how to use 4x4 matrix keyboard, click on the links.
Code:
//header files
#include<reg51.h>
#include"lcd.h"
#include"kbd.h"
#include"delay.h"
//to store entered username and password
unsigned char username[8];
unsigned char password[8];
//opriginal username and password
unsigned char fixusername[8]={1,2,3,4,5,6,7,8},fixpassword[8]={8,7,6,5,4,3,2,1};
//character count and check flags
unsigned char useri=0, passi=0, userflag=0, passflag=0;
void process();
void checkuser();
void checkpass();
void print_on_LCD(unsigned char);
int main(void)
{
init_LCD();
wrcmd(0x80,0);
write_to_LCD("user:");
while(1)
{
process();
}
}
void process()
{
unsigned char key;
//check if any key is pressed
key=getkey();
switch(key)
{
case 1:
print_on_LCD(key);
break;
case 2:
print_on_LCD(key);
break;
case 3:
print_on_LCD(key);
break;
case 4:
print_on_LCD(key);
break;
case 5:
print_on_LCD(key);
break;
case 6:
print_on_LCD(key);
break;
case 7:
print_on_LCD(key);
break;
case 8:
print_on_LCD(key);
break;
case 9:
print_on_LCD(key);
break;
case 15:
//reset button
wrcmd(0x01,0);
wrcmd(0x80,0);
write_to_LCD("user:");
useri=0;
userflag=0;
passi=0;
passflag=0;
break;
case 16:
//check username was correct
if(userflag==1)
{
checkpass();
}
//checking username right now
else
{
checkuser();
}
delay(100);
delay(100);
break;
}
}
void checkuser()
{
unsigned char i;
//check username
for(i=0;i<8;i++)
{
if(username[i]!=fixusername[i])
break;
}
//if username is correct
if(i==8)
{
userflag=1;
wrcmd(0xc0,0);
write_to_LCD("pass:");
}
//if username was wrong
else
{
//reset entered username
for(i=0;i<8;i++)
{
username[i]=0;
}
wrcmd(0x01,0);
wrcmd(0x80,0);
write_to_LCD("wrong username");
delay(100);
delay(100);
delay(100);
delay(100);
delay(100);
wrcmd(0x01,0);
wrcmd(0x80,0);
write_to_LCD("user:");
useri=0;
userflag=0;
}
}
void checkpass()
{
unsigned char i;
//check password
for(i=0;i<8;i++)
{
if(password[i]!=fixpassword[i])
break;
}
//if password is correct
if(i==8)
{
passflag=1;
wrcmd(0x01,0);
wrcmd(0x80,0);
write_to_LCD("Logged In");
//clear entered username
for(i=0;i<8;i++)
{
username[i]=0;
}
userflag=0;
}
//if password is wrong
else
{
//reset entered username and password
for(i=0;i<8;i++)
{
password[i]=0;
username[i]=0;
}
wrcmd(0x01,0);
wrcmd(0x80,0);
write_to_LCD("wrong password");
delay(100);
delay(100);
delay(100);
delay(100);
delay(100);
wrcmd(0x01,0);
wrcmd(0x80,0);
write_to_LCD("user:");
useri=0;
passi=0;
userflag=0;
passflag=0;
}
}
void print_on_LCD(unsigned char key)
{
unsigned char temp;
//check if printing password
if(userflag==1)
{
//check if 8 characters are already entered
if(passi<8)
{
password[passi]=key;
passi++;
temp=key+48;
write_to_LCD(&temp);
delay(100);
delay(100);
}
}
//entering username
else
{
//check if 8 characters are already entered
if(useri<8)
{
username[useri]=key;
useri++;
temp=key+48;
write_to_LCD(&temp);
delay(100);
delay(100);
}
}
}
Output:
- successful LogIn
- Wrong username
- Wrong password
Note: I am working on optimization of this code. If you have any suggestions for reducing code and data size of this code then comment below.
Program files, hex file and simulation file are at this link. click there to download and test.
No comments:
Post a Comment