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

ExperimenterUK
Sat Oct 12 2019, 10:23 PM
Log in.. then post your code in the 8051 forum
Sat Oct 12 2019, 10:18 AM
Bhadra Sajikumar
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 07:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 08:48 PM
Caryn Kover
Thank you for your time,
ExperimenterUK
Mon Aug 05 2019, 07:09 PM
@Ismail ..post in forum, give details
Mon Aug 05 2019, 04:33 PM
Ismail balghmi
Hello. Please I won't download
ExperimenterUK
Wed Jul 24 2019, 12:47 AM
@BUSTER123 Please post in the forum
Tue Jul 23 2019, 06:13 PM
BUSTER123
hello! newbie here. i am kind of a guy who hates using ready - made existing libraries, so this helped a lot as i was trying to control and write to a character lcd display. however, i can't figure out how to set the cursor position. for example, i want to set it to the 5th column in the first row, so position 5. so according to the table, i am sending 10000101 on the 8 data lines of the lcd (register select pulled low). the cursor does not move there however. and i cant move the cursor left or right too. is it something wrong that i am doing? thank you.
ExperimenterUK
Thu Jul 11 2019, 07:55 PM
@ramos https://www.theengineeringprojects.com/2017/09/l298-motor-driver-library-proteus.html
ExperimenterUK
Thu Jul 11 2019, 07:44 PM
@praveen123. Can you give a link to the project ?

Downloads

Comments

auwal
Tue Nov 19 2019, 03:13 PM
ANKITPATEL
Mon Nov 18 2019, 11:00 AM
Casmir
Wed Nov 13 2019, 06:25 AM
DeepakBGattani
Mon Nov 11 2019, 01:31 PM
Jeanbusatta
Sun Nov 10 2019, 04:42 AM
mdayaz
Sat Nov 09 2019, 03:41 PM
baoyen123
Fri Nov 08 2019, 10:06 AM
xiwang
Fri Nov 08 2019, 07:28 AM

Online

Guests: 20, Members: 0 ...

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

Members: 38105
Newest member: auwal
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Nov 19 2019, 10:44 PM
The 8051 has too little memory to do anything us...
ilylily07 Avatar
Posted By ilylily07
Mon Nov 18 2019, 09:54 AM
0v7670 camera interfacing with stc 8051.using asse...
Jeanbusatta Avatar
Posted By Jeanbusatta
Sun Nov 10 2019, 04:43 AM
ty, really needed it
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Oct 22 2019, 05:18 AM
wimac Avatar
Posted By wimac
Mon Oct 21 2019, 09:18 AM
I’ve been following this thread and looking at the...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:09 PM
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:02 PM
sir, when i on display ,sometime 3rd display is no...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 02:53 PM
sir, when i on display ,sometime 3rd display is no...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:13 PM
please find video attachment. 3rd & 4th displa...