You are on page 1of 12

DIGITAL CODE LOCK

KAAVIYA.K 2010115014 VEDAVALLI.V 2010115053 YOGALAKSHMI.R 2010115059

DIGITAL CODE LOCK Introduction: This project is designed to produce security for a place by applying a digital code lock. If the password entered is wrong for three times in continuous the device will get blocked and can only be opened using master password. The project consists of 4x3 keypad matrix, for entering a password to the micro controller, a LCD display, to display data, buzzer to indicate lock or unlocked status and a 8051 micro controller to control all the devices. Operation: In order to open the lock the user has to enter the correct password and to press # in the keypad which is used as enter in the project. The default password is 54321. If the password matches the lock will be opened which will be indicated by turning ON a relay. If password entered is wrong for continuously three times, then the device will get blocked which can be unlocked using master password only. The master password is set as 1234567890, which can be used to change the user password. In order to change the user password the user has to enter 54321 and the system will ask for master password. After entering the master password, the user set is new password. The * button in the keypad is used as cancel button. The micro controller will take care in displaying the data typed in the keypad to the LCD. The use of LCD and keypad

reduces the number of I/O pins for the micro controller. Except relay all other devices operates on 5v and relay operates on 12v. BLOCK DIAGRAM

LCD

8051 4x3 Keypad Matrix

Relay

Delay.c
#include "delay.h" void delayus(unsigned char delay){ while(delay--); } void delayms(unsigned char delay){ while(delay--) delayus(149); }

Delay.h:
#ifndef __DELAY__ #define __DELAY__ void delayus(unsigned char); void delayms(unsigned char); #endif

Keypad.c
#include "keypad.h" #include "delay.h" bit keystatus = FALSE; void keypad_init(){ keyport &=0x0F; } unsigned char getkey(){ unsigned char i,j,k,key=0,temp; k=1; for(i=0;i<4;i++){ keyport &=~(0x80>>i); temp = keyport; temp &= 0x07; if(7-temp){ if(!col1){ key = k+0; while(!col1); return key; } if(!col2){ key = k+1; while(!col2); return key; } if(!col3){ key = k+2; while(!col3); return key; } j++; } k+=3;

keyport |= 0x80>>i; delayms(10); } return FALSE; } unsigned char translate(unsigned char keyval){ if(keyval<10) return keyval+'0'; else if(keyval==10) return 'x'; else if(keyval==11) return '0'; else if(keyval==12) return 'e'; }

Keypad.h:
#ifndef __KEYPAD_ #define __KEYPAD_ #include <REG2051.H> #define keyport P1 #define col1 P1_0 #define col2 P1_1 #define col3 P1_2 #define TRUE 1 #define FALSE 0 void keypad_init(); unsigned char getkey(); unsigned char translate(unsigned char); #endif

Lock.c:
#include "keypad.h" #include "lcd.h" #include "delay.h" #include "lock.h" unsigned char code masterlock[10]="1234567890", defaultulock[5]="54321"; unsigned char userlock[5], input[10]; extern bit newlock; bit check(unsigned char *first, unsigned char *second, unsigned char len){

unsigned char i=0; for(i=0;i<len;i++){ if(first[i]!=second[i]) return FALSE; } return TRUE; } void setulock(){ char status; retry: lcd_cmd(LCD_CLS); lcd_cmd(LCD_SETDDADDR); lcd_str("Enter Mastercode"); lcd_cmd(0xC0); lcd_data(LOCK); lcd_data(':'); status = getinput(10); if(status == TRUE){ if(check(input,masterlock,10)){ retry1: lcd_cmd(LCD_CLS); lcd_str("Enter new code"); lcd_cmd(0xC0); lcd_data(LOCK); lcd_data(':'); status = getinput(5); if(status == TRUE){ lcd_cmd(LCD_CLS); lcd_data(OK); lcd_str("lock code saved!"); newlock = TRUE; store_code(); delayms(250); delayms(250); delayms(250); delayms(250); goto exit; } else if(status == RETRY) goto retry1; else if(status == EXIT) goto exit; } else{ lcd_cmd(LCD_CLS); lcd_str("WRONG CODE!");

delayms(250); delayms(250); delayms(250); delayms(250); goto exit; } } else if(status == RETRY) goto retry; else if(status == EXIT) goto exit; exit:; } char getinput(unsigned char max){ unsigned char i,key; i=0; while(1){ while(!(key=getkey())); key = translate(key); input[i]=key; if(key=='x'){ if(i==0) return EXIT; i--; lcd_cmd(0xC2+i); lcd_data(' '); lcd_cmd(0xC2+i); } else if(key=='e'){ return TRUE; } else{ i++; if(i>max){ lcd_cmd(LCD_CLS); lcd_data(EX); lcd_str(" Code too Long..."); delayms(250); delayms(250); delayms(250); delayms(250); return RETRY; } lcd_data(key); } }

} void store_code(){ unsigned char i; for(i=0;i<5;i++) userlock[i]=input[i]; }

Lock.h:
#ifndef __LOCK #define __LOCK #include <REG2051.H> #define lockpin P1_3 #define FALSE 0 #define TRUE 1 #define RETRY 2 #define EXIT 3 #define LOCK 0 #define UNLOCK 1 #define EX 2 #define OK 3 bit check(unsigned char *,unsigned char *,unsigned char); void setulock(); char getinput(unsigned char); void store_code(); #endif

LCD.c:
#include "lcd.h" #include "delay.h" #include <REG2051.H> unsigned char code lockicon[]={0xe, 0xa, 0x1f, 0x1f, 0x1b, 0x1b, 0xe, 0x0}; unsigned char code unlockicon[]={0xe, 0x2, 0x1f, 0x1f, 0x1b, 0x1b, 0xe, 0x0}; unsigned char code ex[]={0x1f, 0x1b, 0x1b, 0x1b, 0x1b, 0x1f, 0x1b, 0x1f}; unsigned char code ok[]={0x0, 0x1, 0x3, 0x16, 0x1c, 0x8, 0x0, 0x0}; void lcd_reset() { lcd_port = 0xFF; delayms(20); lcd_port = 0x03+LCD_EN; lcd_port = 0x03; delayms(10);

lcd_port = 0x03+LCD_EN; lcd_port = 0x03; delayms(1); lcd_port = 0x03+LCD_EN; lcd_port = 0x03; delayms(1); lcd_port = 0x02+LCD_EN; lcd_port = 0x02; delayms(1); } void lcd_init () { unsigned char i; lcd_reset(); lcd_cmd(LCD_SETFUNCTION); // 4-bit mode - 1 line - 5x7 font. lcd_cmd(LCD_SETVISIBLE+0x04); // Display no cursor - no blink. lcd_cmd(LCD_SETMODE+0x02); // Automatic Increment - No Display shift. lcd_cmd(LCD_SETCGADDR); for(i=0;i<8;i++) lcd_data(lockicon[i]); for(i=0;i<8;i++) lcd_data(unlockicon[i]); for(i=0;i<8;i++) lcd_data(ex[i]); for(i=0;i<8;i++) lcd_data(ok[i]); lcd_cmd(LCD_SETDDADDR); // Address DDRAM with 0 offset 80h. } void lcd_cmd (char cmd) { lcd_port = ((cmd >> 4) & 0x0F)|LCD_EN; lcd_port = ((cmd >> 4) & 0x0F); lcd_port = (cmd & 0x0F)|LCD_EN; lcd_port = (cmd & 0x0F); delayus(200); delayus(200); } void lcd_data (unsigned char dat) { lcd_port = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS); lcd_port = (((dat >> 4) & 0x0F)|LCD_RS); lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS); lcd_port = ((dat & 0x0F)|LCD_RS); delayus(200); delayus(200); }

void lcd_str (unsigned char *str) { while(*str){ lcd_data(*str++); } }

LCD.h:
#include <REG2051.H> #define lcd_port P3 #define LCD_EN 0x80 #define LCD_RS 0x20 #define LCD_CLS 0x01 #define LCD_HOME 0x02 #define LCD_SETMODE 0x04 #define LCD_SETVISIBLE 0x08 #define LCD_SHIFT 0x10 #define LCD_SETFUNCTION 0x28 #define LCD_SETCGADDR 0x40 #define LCD_SETDDADDR 0x80 #define FALSE 0 #define TRUE 1 void lcd_init(); void lcd_reset(); void lcd_cmd (char); void lcd_data(unsigned char); void lcd_str (unsigned char*);

main.c:
#include "lcd.h" #include "keypad.h" #include "lock.h" #include "delay.h" extern unsigned char input[10], userlock[5]; extern unsigned char code defaultulock[5],masterlock[10]; bit newlock=FALSE; unsigned char retrycount=3; void main(){

unsigned char status,i=0; bit lockstatus; lcd_init(); keypad_init(); while(1){ lcd_cmd(LCD_CLS); lcd_str("Enter lock code"); lcd_cmd(0xC0); lcd_data(LOCK); lcd_data(':'); status = getinput(5); if(check(input,"12345",5)){ setulock(); goto done; } if(status == TRUE){ if(newlock) lockstatus = check(input,userlock,5); else lockstatus = check(input,defaultulock,5); if(lockstatus){ retrycount = 3; lockpin = 0; lcd_cmd(LCD_CLS); lcd_data(OK); lcd_str(" Lock is"); lcd_cmd(0xC0); lcd_str("deactivated!"); delayms(250); delayms(250); delayms(250); delayms(250); lcd_cmd(LCD_CLS); lcd_str("Press '#' key to"); lcd_cmd(0xC0); lcd_str("lock again!"); while(getkey()!=12); lockpin = 1; lcd_cmd(LCD_CLS); lcd_data(OK); lcd_str("Lock is active!"); } else{ retrycount--; lcd_cmd(LCD_CLS); lcd_data(EX);

lcd_str(" Wrong Code!"); lcd_cmd(0xC0); lcd_str("Tries left = "); lcd_data('0'+retrycount); delayms(250); delayms(250); delayms(250); delayms(250); if(retrycount==0){ blocked: lcd_cmd(LCD_CLS); lcd_data(EX); lcd_str("BLOCKED"); lcd_data(EX); lcd_cmd(0xC0); lcd_data(LOCK); lcd_data(':'); status = getinput(10); if(check(input,masterlock,10)){ retrycount=3; lcd_cmd(LCD_CLS); lcd_data(EX); lcd_str("UNBLOCKED"); lcd_data(EX); delayms(250); delayms(250); delayms(250); delayms(250); } else{ lcd_cmd(LCD_CLS); lcd_data(EX); lcd_str("WRONG CODE"); lcd_data(EX); delayms(250); delayms(250); delayms(250); delayms(250); goto blocked; } } } } done:; } }

You might also like