Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

door Lock using pic16f877a problem , Plz help

Status
Not open for further replies.

last_samurai

New Member
hi.

I'm using pic16f877a ,lcd , keypad 4x4,
i wrote a code for door lock
but I'm facing such a problem , i don't know exactly what is that mistake ,

Kindly help me if you can ,
thanks
-----------------------------------------------------------
C:
#define press keypad_key_press()

#define click keypad_key_click()

sbit LCD_RS at RB0_bit;

sbit LCD_EN at RB1_bit;

sbit LCD_D4 at RB4_bit;

sbit LCD_D5 at RB5_bit;

sbit LCD_D6 at RB6_bit;

sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;

sbit LCD_EN_Direction at TRISB1_bit;

sbit LCD_D4_Direction at TRISB4_bit;

sbit LCD_D5_Direction at TRISB5_bit;

sbit LCD_D6_Direction at TRISB6_bit;

sbit LCD_D7_Direction at TRISB7_bit;

char keypadPort at PORTd;

char kb;

char x;

char key_value;

char fromuser[4];

char password[4]={1,2,3,4};

char check;

char *ptr;

char i=0;


void show(char,char);

void init()

{

adcon1=7;

portc=0;

trisc=0;

lcd_init();

lcd_cmd(12);

keypad_init();

}


void startup()

{

portc=0b11111111;

delay_ms(100);

portc=0b00000000;

delay_ms(100);

lcd_out(1,1,"welcome");

delay_ms(200);

lcd_cmd(1);

ptr=&fromuser[0];

}

void main() {

init();

startup();

x=0;


while(kb!=4)

  { // 4 it mean the ON/c button

  kb=keypad_key_press();

  while(!kb);

  fromuser[x]=kb;

  show(kb,x);

  x++;

  }//end while





  for(x=0;x<4;x++){lcd_chr(1,2,fromuser[x]+48);} //show saved pass

  check=memcmp(fromuser,password,4);

  if(check==0)lcd_out(2,2,"matching");

  else

  lcd_out(2,2,"not matching");


} //end main



void show(char key,char pos)

{

switch(key)

  {

  case 1:key_value=7;break;

  case 2:key_value=4;break;

  case 3:key_value=1;break;

  case 4:key_value=30;break;

  case 5:key_value=8;break;

  case 6:key_value=5;break;

  case 7:key_value=2;break;

  case 8:key_value=0;break;

  case 9:key_value=9;break;

  case 10:key_value=6;break;

  case 11:key_value=3;break;


  }

lcd_chr(1,1+pos,key_value+48);



  }
 
Last edited by a moderator:
This bit is wrong!!

C:
   kb=keypad_key_press();
   while(!kb);
   fromuser[x]=kb;

if kb = keypad_key_pressed returns a key, then the program crashes... Do it this way..

C:
   kb=keypad_key_press();    // read the key
   while(keypad_key_press()); // wait until not reading key
   fromuser[x]=kb;   // process key
 
why not:

Code:
kb = 0;  //Reset Key

while(!kb)    //Loop until kb is > 0 . Assuming no key = 0 return from below
    kb=keypad_key_press();  //update kb...

fromuser[x]=kb;

Code:
kb=keypad_key_press();
   while(!kb); //locks here because kb never changes
   fromuser[x]=kb;

the below from above comment will* or can give you old keypress
Code:
kb=keypad_key_press();   // read the key
   while(keypad_key_press());// wait until not reading key
   fromuser[x]=kb;// process key
 
Last edited:
Jason said:
the below from above comment will* or can give you old keypress
Code:
kb=keypad_key_press();   // read the key
   while(keypad_key_press());// wait until not reading key
   fromuser[x]=kb;// process key
Explain!

I can't see how as I only use the kb variable the once....

The version that you posted will halt the program waiting for a keypress, whereas my version waits until the user lets go if there has been a keypress...
 
Oops sorry thought u saw a ! Mark lol The version you posted will try to grab a press if nothing then will place a 0 in kb and a 0 in fromuser... will skip a keypress completely while filling the fromuser with 0 data wasting the buffer I assume.

The user looked like he wanted to wait until a key is pressed hence the while ! Kb... so mines will wait for input which is what it looks like the OP wanted.
 
Basically your way will never allow a combonation to be entered in time for a lock. ... example you press 9075 and the buffer will probably capture only one of those digits ... if lucky
 
Jason said:
The user looked like he wanted to wait until a key is pressed hence the while ! Kb... so mines will wait for input which is what it looks like the OP wanted.

True... The user hasn't responded so we'll have to wait...

Look here!!!
while(keypad_key_press());// wait until not reading key
As the keypad_key_press() isn't being processed in this line... All that happens is, If the key (Kb) is captured, then the program will wait until the key is released.... Kb will not be updated until the next cycle...
 
Just read his code and its weird... seems like a few things are missing... whats press? defined as that keypressed function?

Why not do something simple like:
Code:
char x = 0;    //Temp , hold how many key pressed

for(x=0;x<4;x++)    //if 4 keys entered then exit loop
{ 
    kb = 0;        //preset key to 0
    while(!kb)    //while key is 0 stay in loop
        kb=keypad_key_press();    //when keypressed return > 0 loop is exited
   
    fromuser[x]=kb;                //KEYS buffer is updated
    show(kb,x);                    //Display key pressed to user
}
 
Ian your code commented...
Code:
kb=keypad_key_press();   // read the key (even if 0 place in kb)
while(keypad_key_press());// wait until not reading key ...
fromuser[x]=kb;// process key (will be 0 since we didnt wait for a non zero)

so if no key is pressed then it will flood with 0000 and when a key like 8 is pressed it will loop to 0800 or something... depending on speed of cpu because nothing is stopping for the input.
 
Ian your code commented...
Code:
kb=keypad_key_press();   // read the key (even if 0 place in kb)
while(keypad_key_press());// wait until not reading key ...
fromuser[x]=kb;// process key (will be 0 since we didnt wait for a non zero)

so if no key is pressed then it will flood with 0000 and when a key like 8 is pressed it will loop to 0800 or something... depending on speed of cpu because nothing is stopping for the input.

I seem to have missed the "!" Sorry all....
Code:
kb=keypad_key_press();   // read the key (even if 0 place in kb)
while(!keypad_key_press());// wait until not reading key ...
fromuser[x]=kb;// process key (will be 0 since we didnt wait for a non zero)
Reading back through I see Jason mentioned this in post #5
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top