| ??? 09/08/10 15:47 Read: times |
#178477 - Please assess the c code written by me |
Hello guys i am a student doing electronics and comm. of 5th semester B.E. i have written a code that opens or closes a door using a password/key as a practice i would like some feed back from you guys so as to improve as this is the first program i have written on my own
#include"reg51.h"
//#include"8052.h"
#define key_0 '0'
#define key_1 '1'
#define key_2 '2'
#define key_3 '3'
#define key_4 '4'
#define key_5 '5'
#define key_6 '6'
#define key_7 '7'
#define key_8 '8'
#define key_9 '9'
#define key_A 'A'
#define key_B 'B'
#define key_C 'C'
#define key_D 'D'
#define key_E 'E'
#define key_F 'F'
#define special_q '?'
sfr lcd = 0x90; //0x90 is port 1 address
sfr hex = 0x80; //0x80 is port 0 address
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
sbit door_state = P2^3; //let state of 1 mean door is closed and
state of 0 mean door is open
sbit motor1=P2^6;
sbit motor2=P2^7;
sbit alarm_1=P2^4;
void delay_ms(unsigned char);
void lcd_init();
void lcd_line(bit);
void lcd_com(unsigned char);
void lcd_disp(unsigned char);
void lcd_disp1(unsigned char dat[],bit delay1);
unsigned char keypad();
unsigned char invalid(unsigned char,unsigned char);
unsigned char compare(unsigned char one[], unsigned char two[], bit);
void change_key();
void close();
void open();
void alarm();
unsigned char present_key[]= "12345600",wrong_count=0,right_count=0;
code unsigned char zero2nine[]= {"0 to 9 only"},wrong[]= {"wrong key.."},
lock_error[]= {"Lock error"};
code unsigned char key_state[]= {0xEE , 0xED , 0xEB , 0xE7,
0xDE , 0xDD , 0xDB , 0xD7,
0xBE , 0xBD , 0xBB , 0xB7,
0x7E , 0x7D , 0x7B , 0x77
};
code unsigned char digit[]= { key_0 , key_4 , key_8 , key_C ,
key_1 , key_5 , key_9 , key_D ,
key_2 , key_6 , key_A , key_E ,
key_3 , key_7 , key_B , key_F
};
// main program starts here
void main()
{
unsigned char i,j,temp,password[8]=0;
code unsigned char right[]= {"right"},p_wd[]= {"Key:"};
code unsigned char lcd_open[]= {"open..."}, lcd_close[]=
{"close..."};
unsigned char flag_g=0;//,a;
code unsigned char lcd_door[]= {"A-> open/close"};
code unsigned char lcd_change[]= {"B-> Change key"}, lcd_exit[]= {"E->
Exit"};
//begin:
while(1)
{
lcd_init();
lcd_disp1(p_wd,0);
lcd_line(1);
//This is where password is taken as input
for(i=0; i<8; i++)
{
password[i]=keypad();
// reading from keypad
flag_g=invalid(password[i],i);
if(flag_g==0xaa || flag_g==0xff || flag_g==0xDD)
{
break;
}
lcd_disp( special_q ); // 0x3f is hex value of ascii
character "?" (question mark)
}
if(flag_g==0xff || flag_g==0xDD)
{
flag_g=0;
continue;
}
flag_g=0;
right_count=0;
//comparing is done here
flag_g = compare(password,present_key,1);
if(flag_g==0xff)
{
flag_g=0;
continue;
}
if(right_count==7)
{
wrong_count=0;
lcd_disp1(right,1);
}
// The menu is shown here
j=0;
while(j<=100)
{
j++;
for(i=0; i<1; i++)
{
lcd_disp1(lcd_door,0);
temp=keypad();
if(temp != 0xff) break;
lcd_disp1(lcd_change,0);
temp=keypad();
if(temp != 0xff) break;
lcd_disp1(lcd_exit,0);
temp=keypad();
if(temp != 0xff) break;
}
if(temp==key_C)
{
if(door_state==1)
{
lcd_disp1(lcd_open,0);
open();
}
else
{
lcd_disp1(lcd_close,0);
close();
}
}
else if(temp==key_D)
{
change_key();
}
else if(temp==key_B)
{
temp=0;
flag_g=0xff;
break;
}
}
continue;
/*if(flag_g==0xff)
{
flag_g=0;
continue;
}*/
//sleep();
}
}
/*
Purpose: This function is used to create delay between operations with
a minimum of 1 ms and maximum
of about 256 ms of delay
Input: input is 8 bit i.e. unsigned char. The input specifieds the
amount of delay called
*/
void delay_ms(unsigned char u)
{
unsigned char i;
unsigned int j;
//
for(i=0; i<u; i++)
{
for(j=0; j<1275; j++);
}
}
/*
Purpose: This function is used to pass on commands to the LCD
display connected to port P1.
Input: input is 8 bit i.e. unsigned char. The input specifieds
command to be given to the LCD.
*/
void lcd_com(unsigned char x)
{
RS=0; //command
mode
RW=0;
EN=1;
lcd=x;
delay_ms(5);
EN=0;
delay_ms(5);
}
/*
Purpose: This function initialises the LCD screen
*/
void lcd_init()
{
lcd_com(0x0E);
lcd_com(0x38);
lcd_com(0x06);
}
/*
Purpose: This function is used to change/define the line which is
to be used in the LCD.
Input: input is 1 bit.
The input of '0' makes the cursor to go to the line 0
and input of '1' makes the cursor to go to line 1.
*/
void lcd_line(bit line)
{
if(line==0)
lcd_com(0x80); //line one
else
lcd_com(0xC0); //return home
}
/*
Purpose: This function is used to pass on data to the LCD display
connected to port P1.
Input: Input is 8 bit i.e. unsigned char. The input specifieds
data to be given to the LCD.
*/
void lcd_disp(unsigned char x)
{
RS=1;
RW=0;
EN=1;
lcd=x;
delay_ms(5);
EN=0;
delay_ms(5);
//lcd_com(0x06);
}
void lcd_disp1(unsigned char dat[], bit a)
{
unsigned char i;
lcd_com(0x01);
lcd_line(0);
for(i=0; i <sizeof(dat) ; i++)
{
if(i==8)
{
lcd_line(1);
}
lcd_disp(dat[i]);
}
// Four second delay is to be expected
if(a==1)
{
delay_ms(255); delay_ms(255); delay_ms(255);
delay_ms(255);
delay_ms(255); delay_ms(255); delay_ms(255);
delay_ms(255);
delay_ms(255); delay_ms(255); delay_ms(255);
delay_ms(255);
delay_ms(255); delay_ms(255); delay_ms(255);
delay_ms(255);
delay_ms(255); delay_ms(255);
}
else
{
delay_ms(255); delay_ms(255);
}
}
/*
Purpose: This function is used to get data from the keypad
connected to port P0.
Return: Return is 8 bit i.e. unsigned char. The Return is the
value of the button pressed in the keypad.
*/
unsigned char keypad()
{
unsigned char i,temp=0,temp1=0,temp2=0xff,wait=0,wait1=0;
while(1)
{
//write program to prevent indefinite waiting here
wait++;
hex = 0x0f;
delay_ms(10);
// temp = hex;
// temp &= 0x0f;
for(i=0; i<4; i++)
{
if(i==0) hex = temp1= 0xEF;
else if(i==1) hex = temp1 = 0xDF;
else if(i==2) hex = temp1 = 0xBF;
else if(i==3) hex = temp1 = 0x7F;
temp = hex;
temp &= 0x0f;
temp1&= 0xf0;
if(temp != 0x0F)
{
delay_ms(50);
delay_ms(50);
delay_ms(50);
delay_ms(50);
temp=hex;
temp&=0x0F;
if(temp!=0x0F)
{
temp = temp | temp1;
delay_ms(50);
break;
}
}
}
for(i=0; i<16; i++)
{
if(key_state[i]==temp)
temp2=digit[i];
}
if(wait==255)
{
wait=0;
wait1++;
if(wait1==255)
{
temp2=0xff;
return temp2;
}
}
}
return temp2;
}
/*
Purpose: This function should be used to close the lock of the
door
*/
void close()
{
unsigned char a=0;
if(door_state==0)
{
motor1=0;
motor2=1;
while(a<=200)
{
a++;
if(door_state==1)
{
return;
}
delay_ms(5);
}
lcd_com(0x01);
lcd_line(0);
lcd_disp1(lock_error,1);
return;
}
}
/*
Purpose: This function should be used to open the lock of the door
*/
void open()
{
unsigned char a=0;
if(door_state==1)
{
motor1=0;
motor2=1;
while(a<=200)
{
a++;
if(door_state==0)
{
return;
}
delay_ms(5);
}
lcd_com(0x01);
lcd_line(0);
lcd_disp1(lock_error,1);
return;
}
}
/*
Purpose: This function checks for illegal buttons pressed and gives
appropriate return
Input: (x,i) : x is the input from the keypad and i is the index of the
key presed
Return: (flag) :return value of 0xaa is given if 14 or enter is pressed
0xDD is returned if no key is pressed
and 0xff is given if 15 or escape is pressed
*/
unsigned char invalid(unsigned char x,unsigned char i)
{
unsigned char flag,temp;//,j;
while(i==7)
{
temp=keypad();
if(temp==key_A) //14 is return/enter, key 15 is escape
{
flag=0xaa;
break;
}
else if(temp==key_B || temp== 0xff)
{
flag=0xff;
break;
}
}
if(x==key_A)
{
flag=0xaa;
}
/*
Do not accept the values of 10,11,12,13. Password must
contain only 0 to 9 only
*/
else if(x==key_C || x==key_D || x==key_E || x==key_F)
{
flag=0xDD;
lcd_disp1(zero2nine,1);
}
else if(x==key_B || x==0xff)
{
flag=0xff;
}
return flag;
}
/*
Purpose: this is used to compare the 2 variables and give
corresponfing output
*/
unsigned char compare(unsigned char one[],unsigned char two[], bit a)
{
unsigned char flag_c,i;
code unsigned char mismatch[]= {"Key mismatch"},try_again[]= {"Try
again"};;
flag_c=0x00;
right_count=0;
for(i=0; i<8; i++)
{
if(one==two)
{
right_count++;
}
else
{
wrong_count++;
right_count=0;
flag_c=0xff;
break;
}
}
if(flag_c==0xff)
{
if(a==1)
{
lcd_disp1(wrong,1);
if(wrong_count>=5)
{
alarm();
return 0xff;
}
}
else if(a==0)
{
lcd_disp1(mismatch,0);
delay_ms(200);
lcd_disp1(try_again,0);
return 0xff;
}
}
return flag_c;
}
/*
Purpose: 1. This function is used to get data from the keypad
connected to port P0 twice.
2. Then compare them and then change the password.
*/
void change_key()
{
unsigned char i,flag_g, temp, password[8]=0, pass_confirm[8]=0;// j,
count;
code unsigned char new_key[]= {"New key:"},confirm[]= {"Confirm:"};
code unsigned char key_changed[]= {"key changed..."};
while(1)
{
lcd_disp1(new_key,0);
lcd_line(1);
temp=0;
/* take new password */
for(i=0; i<8; i++)
{
// count++;
password[i]=keypad();
flag_g=invalid(password[i],i);
if(flag_g==0xaa || flag_g==0xDD)
{
break;
}
else if(flag_g==0xff)
{
return;
}
lcd_disp( special_q ); // 0x3f is hex value of
ascii character "?"
}
if(flag_g==0xDD)
{
flag_g=0;
continue;
}
flag_g=0;
//confirmation is done here
lcd_disp1(confirm,0);
lcd_line(1);
/* take new password */
for(i=0; i<8; i++)
{
pass_confirm[i]=keypad();
flag_g=invalid(pass_confirm[i],i);
if(flag_g==0xaa || flag_g==0xDD)
{
break;
}
else if(flag_g==0xff)
{
return;
}
// count++;
lcd_disp( special_q ); // 0x3f is hex value of ascii
character "?"
}
if(flag_g==0xDD)
{
flag_g=0;
continue;
}
flag_g=0;
flag_g=compare(password,pass_confirm,0);
if(flag_g==0xff)
{
flag_g=0;
continue;
}
flag_g=0;
if(right_count==7)
{
for(i=0; i<8; i++)
{
present_key[i] = password[i];
}
lcd_disp1(key_changed,1);
right_count=0;
}
}
return;
}
/*
Purpose:This function calls alarm and switches off the LCD display
until a 5 digit master password is pressed
*/
void alarm()
{
code unsigned char crack[]= "0F3C7";
unsigned char temp[5],a,b,count;
/* Program to switch off lcd display here */
lcd_com(0x08);
alarm_1=1;
while(1)
{
for (a=0; a<5; a++)
{
temp[4]=keypad();
}
count=0;
for (a=0 ; a<5 ; a++)
{
if(temp[a] != crack[a])
{
alarm_1=1;
for (b=0; b<5; b++)
{
temp[a]=0;
}
break;
}
else if(temp[a]==crack[a])
{
count++;
}
}
if(count!=4)
{
continue;
}
else
{
delay_ms(200);
alarm_1=0;
return;
}
}
}
|
| Topic | Author | Date |
| Please assess the c code written by me | 01/01/70 00:00 | |
| Lots of details to improve on | 01/01/70 00:00 | |
| REALLY confusing | 01/01/70 00:00 | |
| timer delay review | 01/01/70 00:00 | |
| what about this timer program | 01/01/70 00:00 | |
| Still a lot of work to do | 01/01/70 00:00 | |
| I have implemented many of the suggestions | 01/01/70 00:00 | |
| Still not thinking about your symbol names | 01/01/70 00:00 | |
variable name dilema | 01/01/70 00:00 |



