Discussion in "PIC Microcontroller Discussion" started by    DAT    Oct 9, 2007.
Tue Oct 09 2007, 05:26 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, 02:37 AM ]
Tags Pic16f628A Combination Lock C Code LCD 3x4 KeypadPIC digital lock project
Tue Oct 09 2007, 05:32 AM
#2
Here is the attachment of combination lock flowchart and hardware circuit simulation using Proteus and combination_lock.c
Tue Oct 09 2007, 11:11 AM
#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.
Tue Oct 09 2007, 09:04 PM
#4
Here is the attachment of combination lock flowchart and hardware circuit simulation using Proteus and combination_lock.c
Wed Oct 10 2007, 05:57 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..
Wed Oct 10 2007, 10:22 PM
#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, 03:35 AM ]
Wed Oct 10 2007, 10:31 PM
#7
Ajay, This is the file in .c so you can download and easy to check...
Wed Oct 10 2007, 10:33 PM
#8


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

DAT



Thu Oct 11 2007, 05:04 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, 05:39 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

Conversation

Thu Jun 11 2020, 06:27 AM
anudee
sir could please send the code for I2C and wifi interfacing with 8051
scena
Tue May 19 2020, 10:29 PM
Does anyone have microcontroller programming software for STC15F104W (core 8051, 4 k, 128 byte, 6 I/O pin, UART ... DIL8), that works?
Sat May 02 2020, 07:38 PM
Tamu Gurung
Hi I am looking for a assembly language program for a digital decimal counter. Time counts max limit of 2 mins starting from 2.00 mins and ending at 0.00. time is to be displayed on 4 7-segment LED displays. Edsim51 simulator to be used. Thanks
Wed Mar 25 2020, 01:39 PM
Tajammul
Sir kindly send the code file i2c for 8051
ExperimenterUK
Fri Mar 13 2020, 10:36 PM
@ Rohitnani,ABEBE..which project ?
Fri Mar 13 2020, 12:55 PM
ABEBE
how can get hadwere of this project
Rohitnani
Sat Mar 07 2020, 05:11 PM
Sir, Could you please send me the zip file input password....
Wed Feb 26 2020, 04:58 PM
Peter Parker
Is it possible to send multiple messages using only 1 GSM module?
Sat Feb 01 2020, 10:12 AM
Pearl Olin
Hello,
Wed Jan 22 2020, 06:16 PM
MINU CHAUDHARY
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.

Downloads

Comments

Mirza123
Thu Jun 25 2020, 03:46 PM
eunicelove124
Mon Jun 22 2020, 03:03 PM
cerouno
Tue Jun 16 2020, 05:10 PM
Marce
Sat Jun 13 2020, 09:43 PM
Davidthils
Thu May 21 2020, 12:44 PM
Jakeror
Thu May 21 2020, 01:28 AM
motorCar
Wed May 20 2020, 07:05 PM
Vordrync
Wed May 20 2020, 11:52 AM

Online

Guests: 50, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Jul 01 2020, 11:07 PM
PS: I am a new learner thats why my question may s...
Mirza123 Avatar
Posted By Mirza123
Wed Jul 01 2020, 02:05 PM
Thanks for the assistance. I have read the data sh...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Jul 01 2020, 10:12 AM
See section 15 (Ports) of the data sheet. ...
wimac Avatar
Posted By wimac
Tue Jun 30 2020, 10:05 AM
How did you know that it is not 8051. Mirza123It i...
Mirza123 Avatar
Posted By Mirza123
Tue Jun 30 2020, 08:39 AM
It seems ports on the AT89LP2052 is not like on th...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Jun 30 2020, 08:34 AM
It seems ports on the AT89LP2052 are not like on t...
Mirza123 Avatar
Posted By Mirza123
Thu Jun 25 2020, 04:07 PM
Hello!i am trying to use AT89LP2052 ( AT89LP2052 h...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Jun 17 2020, 09:42 PM
It is about capacitor? i should put it between Vcc...
cerouno Avatar
Posted By cerouno
Wed Jun 17 2020, 10:37 AM
Take another look at the power supply to the LCD.A...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Jun 17 2020, 02:01 AM
Take another look at the power supply to the LCD.A...