Discussion in "PIC Microcontroller Discussion" started by    DAT    Oct 9, 2007.
Tue Oct 09 2007, 10:56 am
#1
Any One Here Can You Please Read Through My C Code and Correct It...My programming is really worse....I really appreciate any inputs from you guys.


/*******************************************
Keypad and LCD lock on Pic16f628A with 3*4 keypad
Project name: Combination Lock (Demonstration of the Keypad Library routines)
LCD Display in 4-bit mode on PORTA
Keypad on PORTB

Door1 = 05
Door2 = 28
Alarm off = 99

Three Attempts before Alarm

*****************************************/
#define door1_low 6
#define door1_upper 14
#define door2_low 10
#define door2_upper 2

char var1, var2, kp, attempts;
char state = 1;//start from state 1
int cnt=0;
void main(void) {
char text[] = "Combination Lock" ;//message text
CMCON = 0x07 ;//PORTA to digital mode
LCD_Config(&PORTA,7,6,5,3,2,1,0) ;//Pin assignment
;//RS,E,RW,D7,D6,D5,D4
LCD_Cmd(LCD_CLEAR) ;//Clear display
LCD_Cmd(LCD_CURSOR_OFF) ;//Turn cursor off
LCD_Cmd(LCD_SECOND_ROW) ;//Move to ROW2 for rest of program
//Seup Keypad on PORTB
Keypad_Init(&PORTB) ;// Initialize Keypad on PORTB
LCD_OUT(1,1,text) ;//Print welcome message
while(1) { //while ever loop
switch(state){ //check the current state
case 1:
while(!keypad_Read()){} //wait for key1 press
kp = Keypad_Released() ;//wait for release
var1=kp; //store var1 into kp
Delay_ms(10) ;//debounce time
state = 2;
break;
case 2:
if(kp==15) state=1; //go back state 1 when kp = # (enter key)
if(kp==13) state=1;//go back state 1 when kp = * (clear key)
else
state = 3;//go to state 3 get key2
break;
case 3:
while(!keypad_Read()){} //wait for key2 press
kp = Keypad_Released() ;//wait for release
var2=kp;//store var2 into kp
Delay_ms(10) ;//debounce time
break;
if(kp==15) state=1;//enter key goes back state 1
if(kp==13) state=1;//clear key goes back state 1
else
state = 4;//continue goes to state 4
break;
case 4:
while(!keypad_Read()){} //wait for enter key press
kp = Keypad_Released() ;//wait for release
Delay_ms(10) ;//debounce time
break;
if(kp==13)
state =1; //clear goes back to s1
if(kp==15)
state = 5;//enter key goes to state 5
else state = 4;
break;
case 5: //compare enter values with password values stored
if(var1==door1_upper && var2 == door1_low)
state = 8;//goes to state 8 door1 open
LCD_OUT(2,1,"Door1 Opened");
Delay_ms(5000) ;//delay door1 open 5secs
if(var1==door2_upper && var2 == door2_low)
state = 9;//goes to state 9 for door 2
else
LCD_OUT(2,1,"Bad Code");
Delay_ms(2000);
cnt++;
state=6;
case 6:
if(var1==3 && var2 == 3) //both doors open
state = 10;
else
LCD_OUT(2,1,"Bad Code");
Delay_ms(2000) ;
cnt++;
case 7://enter key 99 for alarm off
if(var1==9 && var2 == 9)
LCD_OUT(2,1,"Alarm Off"); //turns alarm off
attempts=0;
state=1;
break;
case 8://open door1 for 5secs
PORTA.F4=1;
Delay_ms(5000);
state=1;
cnt=0;
break;
case 9: //open door2 for 5secs
PORTA.F5=1;
Delay_ms(5000);
state=1;
cnt=0;
break;
case 10: //open both doors
PORTA.F5=1;
PORTA.F4=1;
Delay_ms(5000);
state=1;
cnt=0;
break;
default:// must be a bad attempts
attempts++;
if(attempts ==3)
state = 11;
LCD_OUT(2,1,"Alarm ON");
break;

}


} }




[ Edited Wed Oct 10 2007, 08:07 am ]
Tags Pic16f628A Combination Lock C Code LCD 3x4 KeypadPIC digital lock project
Tue Oct 09 2007, 11:02 am
#2
Here is the attachment of combination lock flowchart and hardware circuit simulation using Proteus and combination_lock.c
Tue Oct 09 2007, 04:41 pm
#3
did you check your code in compiler? are you able to compile it with no error? if yes then try it with hardware of as you seems to have proteus. So try to run it on proteus.
Also its better to zip the files and then upload it again.
Wed Oct 10 2007, 02:34 am
#4
Here is the attachment of combination lock flowchart and hardware circuit simulation using Proteus and combination_lock.c
Wed Oct 10 2007, 11:27 am
#5
ok one thing you need to change is...
in all the cases wherever you are checking for
if(kp==13) and if(kp==15)
make it else if for the second check...
i.e.
if(kp==13)
....
else if(kp==15)
...
else
....
reason is...
lets say if key is 13.. then it will go to first if statement.. checks it.. and finds its true as Kp is 13.. so execute the if condition loop... then it comes to second check.. as kp != 15 so it goes to else statement and will execute it.. so there could be your problem.. make the changes and tell me..
Thu Oct 11 2007, 03:52 am
#6
Ajay, i did corrected the error" if to else if " you have mentioned b4. To get the programming working smoothly i also have to expand the case statement as i have modified the previous version below:

Now I have another problem to face that i could not display message "Alarm" on LCD after 3 attempted incorrect password....

Can you please Help Me In this One Sir, Ajay..

/*******************************************
Keypad and LCD lock on Pic16f628A with 3*4 keypad
Project name: Combination Lock (Demonstration of the Keypad Library routines)
LCD Display in 4-bit mode on PORTA
Keypad on PORTB

Door1 = 05
Door2 = 28
Door1 & 2 = 33
Alarm off = 99

Three Attempts before Alarm

*****************************************/



char var1, var2, kp;
char attempts=1;
//int cnt=0;

char state = 1;//start from state 1
//int cnt=0;
char text[] = "Combination Lock" ;//message text
void main(void) {

CMCON = 0x07 ;//PORTA to digital mode
LCD_Config(&PORTA,7,6,5,3,2,1,0) ;//Pin assignment
;//RS,E,RW,D7,D6,D5,D4
LCD_Cmd(LCD_CLEAR) ;//Clear display
LCD_Cmd(LCD_CURSOR_OFF) ;//Turn cursor off
LCD_Cmd(LCD_SECOND_ROW) ;//Move to ROW2 for rest of program

//Seup Keypad on PORTB
Keypad_Init(&PORTB) ;// Initialize Keypad on PORTB
LCD_OUT(1,1,text) ;//Print welcome message
while(1) { //while ever loop
switch(state){ //check the current state
case 1:
LCD_out(1,1,"Combination Lock");
//LCD_Cmd(LCD_CLEAR) ;
while(!keypad_Read()){} //wait for key1 press
Delay_ms(10) ;//debounce time
kp = Keypad_Released() ;//wait for release
var1=kp; //store var1 into kp
state = 2;
break;

case 2:
if(kp==15) state=1; //go back state 1 when kp = # (enter key)
else if(kp==13) state=1;//go back state 1 when kp = * (clear ke
else
state = 3;//go to state 3 get key2
break;
case 3:
while(!keypad_Read()){} //wait for key2 press
Delay_ms(10) ;//debounce time
kp = Keypad_Released() ;//wait for release
var2=kp;//store var2 into kp
state=4;
break;
case 4:
if(kp==15) state=1;//enter key goes back state 1
else if(kp==13) state=1;//clear key goes back state 1
else
state = 5;//continue goes to state 4
break;

case 5:
while(!keypad_Read()){} //wait for enter key press
Delay_ms(10) ;//debounce time
kp = Keypad_Released() ;//wait for release
state = 6;
break;
case 6:
if(kp==13)
state =1; //clear goes back to s1
else if(kp==15)
state = 7;//enter key goes to state 7
else state = 5;
break;
case 7:
if (var1==14 && var2==6)//comparing for door1
state = 12; //goes state 11 open door1
else state =8;
break;
case 8:
if (var1==2 && var2 == 10)//comparing for door2
state = 13;//goes to state 9 for door 2
else state=9;
break;
case 9:
if (var1==3 && var2 == 3)
state = 14;//goes to state 13 for door1 & 2
else
state =10;
break;
case 10:
if(var1==11 && var2==11)
state = 15;//goes to state 14 Alarm Off
else
state =11;
break;
case 11:
LCD_OUT(2,1,"Invalid") ;

while(!keypad_Read()){} //wait for enter key press
Delay_ms(10) ;//debounce time
keypad_Read()++;
keypad_Read()+=attempts;
if(attempts==3)
state =16;
kp = Keypad_Released() ;//wait for release
LCD_Cmd(LCD_CLEAR) ;
if(kp==13)
state =1;
break;
case 12://open door1 for 5secs
PORTA.F5=1;
//Delay_ms(5000);
state=1;
Lcd_out(2,1,"D1 Opened") ;
while(!keypad_Read()){} //wait for enter key press
Delay_ms(10) ;//debounce time
kp = Keypad_Released() ;//wait for release
if(kp==13)
LCD_Cmd(LCD_CLEAR) ;
state=1;
break;
case 13: //open door2 for 5secs
LCD_OUT(2,1,"D2 Opened");
//PORTB.F4=1;
//Delay_ms(5000);
while(!keypad_Read()){} //wait for enter key press
Delay_ms(10) ;//debounce time
kp = Keypad_Released() ;//wait for release
if(kp==13)
LCD_Cmd(LCD_CLEAR) ;
state=1;
break;
case 14: //open both doors
LCD_OUT(2,1,"D1&2 Opened");
//PORTA.F5=1;
//PORTB.F4=1;
//Delay_ms(5000);
while(!keypad_Read()){} //wait for enter key press
Delay_ms(10) ;//debounce time
kp = Keypad_Released() ;//wait for release
if(kp==13)
LCD_Cmd(LCD_CLEAR) ;
state=1;
break;
case 15:
LCD_OUT(2,1,"*Alarm OFF*");
Delay_ms(5000);
LCD_Cmd(LCD_CLEAR) ;
state=1;
break;
default:// must be a bad attempts
//attempts++;
//if(attempts==3)
LCD_OUT(2,1,"Alarm ON");
Delay_ms(5000);
//attempts=0;
state = 1;
break;

}


} }


[ Edited Thu Oct 11 2007, 09:05 am ]
Thu Oct 11 2007, 04:01 am
#7
Ajay, This is the file in .c so you can download and easy to check...
Thu Oct 11 2007, 04:03 am
#8


Ajay, This is the file in .c so you can download and easy to check...

DAT



Thu Oct 11 2007, 10:34 am
#9
Hi DAT, you can call me Ajay only.. no need of sir!
Ii checked your code again... I am really not sure what the problem is.. as i need to see the working of project to know more about the problem. Also i do not have any compiler for PIC so cannot compile program and simulate it.. but i can give you some suggestions..

In the case of invalid code..

case 11:
LCD_OUT(2,1,"Invalid") ;

while(!keypad_Read()){} //wait for enter key press
Delay_ms(10) ;//debounce time

//this doesn't seems to be ok to me..
keypad_Read()++;
//instead you could have directly incremented
//attempts
//use attempt++;

keypad_Read()+=attempts;

if(attempts==3)
state =16;
kp = Keypad_Released() ;//wait for release
LCD_Cmd(LCD_CLEAR) ;
if(kp==13)
state =1;
break;

Although default has to work, but i want you to try with "case 16" also.. as i see there is no problem in the program.. could be some kind of logical error.. try if you can simulate it on the debugger... step by step execution... and see what could be the problem..
Thu Oct 11 2007, 11:09 am
#10
Hello Ajay, the did the simulations and it is running without errors. The programming is working fine except the case 11 "3 attempts" before turn alarm on ... i could not get the display ALARM ON at the default case....

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am