Vous êtes sur la page 1sur 17

A 4x3 matrix keypad and a 16x2 LCD have been used here.

Keypad and LCD are very commonly used input & output devices, respectively. A four digit predefined password needs to be specified the user. This password is stored in the system. While unlocking, if the entered password from keypad matches with the stored password, then the lock opens and a message is displayed on LCD. Also an output pin is made high to be used for further purpose. The connections in the circuit are as following: port P2 of microcontroller AT89C51 is used as data input port which is connected to data pins (7-14) of LCD. P1^0, P1^1 and P1^2 pins of microcontroller are connected to control pins RS, RW and EN of LCD. Port P0 is used to take input from keypad. P0^7 has been used as lock output pin of controller. As the program starts, string Enter Password is displayed on LCD. The keypad is scanned for pressed digits one by one. Every time, row and column of the key pressed is detected and a * is displayed on LCD corresponding to the entered number. After the four digits are entered, the user is prompted to Confirm Password and again the input is taken through LCD. If the passwords do not match, a message is displayed to indicate Wrong Password; otherwise the user is prompted to unlock the device. To unlock, user needs to Enter Password through keypad. Again the keypad is scanned for pressed keys and corresponding digits are identified. The passkey is displayed as **** on the LCD screen. After the four digits are entered, they are compared with the pre-set password. If all the four digits match with set password, LCD displays Lock Open and the lock output pin goes high. If the security code is wrong, Wrong Password is sent to be displayed on LCD. The system gets locked if more than three attempts are made with wrong password to open the electronic lock. The system needs to be reset in such a case.

#include<reg51.h> #define port P1 #define dataport P2 #define key P0 #define sec 100 sbit rs = port^0; sbit rw = port^1; sbit en = port^2;

sbit col1=key^4;

sbit col2=key^5; sbit col3=key^6; sbit row1=key^0; sbit row2=key^1; sbit row3=key^2; sbit row4=key^3; sbit lock_output=P0^7;

int check=0; int digit[4]={0,0,0,0}; int dig_input[4]={0,0,0,0}; int dig_input_recheck[4]={0,0,0,0};

int i,k;

void delay(unsigned int msec) // Time delay function { int i,j ; for(i=0;i<msec;i++) for(j=0;j<1275;j++); }

void lcd_cmd(unsigned char item) // Function to send command to LCD { dataport = item; rs= 0; rw=0; en=1; delay(1); en=0; return; }

void lcd_data(unsigned char item) // Function to send data to LCD { dataport = item; rs= 1; rw=0; en=1; delay(1); en=0; return; }

void lcd_data_string(unsigned char *str) // Function to send data to string { int i=0; while(str[i]!='\0') { lcd_data(str[i]); i++; //delay(10); } return; }

void lcd(unsigned char str[10]) { lcd_cmd(0x38); lcd_cmd(0x0e); lcd_data_string(str); }

void ans()

{ if(check>3) { lcd_cmd(0x01); lcd_cmd(0x82); lcd_data_string(" LOCK OPEN"); lock_output=1; delay(300); while(1); } else { lcd_cmd(0x01); lcd_cmd(0x82); lcd_data_string(" WRONG PASSWORD"); lock_output=0; delay(300); } }

void code_check() // Function to check password { if(i<=3 ) { switch((i+1)) { case 1: { if(dig_input[0]==digit[0]) { check=check+1; } break; }

case 2: { if(dig_input[1]==digit[1]) { check=check+1; } break; } case 3: { if(dig_input[2]==digit[2]) { check=check+1; } break; } case 4: { if(dig_input[3]==digit[3]) { check=check+1; } break; } } } delay(10); if(i==3) { ans(); } }

void display(int a) //Display function { switch(a)

{ case 1:{ lcd_data('*'); delay(100); digit[i]=1; code_check(); break; } case 2:{ lcd_data('*'); delay(100); digit[i]=2; code_check(); break; } case 3:{ lcd_data('*'); delay(100); digit[i]=3; code_check(); break; } case 4:{ lcd_data('*'); delay(100); digit[i]=4; code_check(); break; } case 5:{ lcd_data('*'); delay(100); digit[i]=5;

code_check(); break; } case 6:{ lcd_data('*'); delay(100); digit[i]=6; code_check(); break; } case 7:{ lcd_data('*'); delay(100); digit[i]=7; code_check(); break; } case 8:{ lcd_data('*'); delay(100); digit[i]=8; code_check(); break; } case 9:{ lcd_data('*'); delay(100); digit[i]=9; code_check(); break; } case 0:{ lcd_data('*');

delay(100); digit[i]=0; code_check(); break; } } }

void check_col1() { row1=row2=row3=row4=1; row1=0; if(col1==0) display(1); row1=1; row2=0; if(col1==0) display(4); row2=1; row3=0; if(col1==0) display(7); row3=1; row4=0; if(col1==0) { row4=1; } }

void check_col2() { row1=row2=row3=row4=1;

row1=0; if(col2==0) display(2); row1=1; row2=0; if(col2==0) display(5); row2=1; row3=0; if(col2==0) display(8); row3=1; row4=0; if(col2==0) display(0); row4=1; }

void check_col3() { row1=row2=row3=row4=1; row1=0; if(col3==0) display(3); row1=1; row2=0; if(col3==0) display(6); row2=1; row3=0; if(col3==0) display(9); row3=1;

row4=0; if(col3==0) { row4=1; } }

void check_password_col1() { row1=row2=row3=row4=1; row1=0; if(col1==0) dig_input[k]=1; row1=1; row2=0; if(col1==0) dig_input[k]=4; row2=1; row3=0; if(col1==0) dig_input[k]=7; row3=1; row4=0; if(col1==0) { row4=1; } }

void check_password_col2() { row1=row2=row3=row4=1;

row1=0; if(col2==0) dig_input[k]=2; row1=1; row2=0; if(col2==0) dig_input[k]=5; row2=1; row3=0; if(col2==0) dig_input[k]=8; row3=1; row4=0; if(col2==0) { dig_input[k]=0; row4=1; } }

void check_password_col3() { row1=row2=row3=row4=1; row1=0; if(col3==0) dig_input[k]=3; row1=1; row2=0; if(col3==0) dig_input[k]=6; row2=1; row3=0;

if(col3==0) dig_input[k]=9; row3=1; row4=0; if(col3==0) { row4=1; } }

void pass_set() { row1=row2=row3=row4=0; while(col1==1 && col2==1 && col3==1); for(i=0;i<4;i++) { k=i; delay(50); lcd_cmd(0xc4+i); delay(100); row1=row2=row3=row4=0; while(col1==1 && col2==1 && col3==1); row1=row2=row3=row4=0; if(col1==0) check_password_col1(); else if(col2==0) check_password_col2(); else if(col3==0) check_password_col3(); lcd_data('*'); delay(50);

} }

void main() { int e,j=0,count=1; col1=col2=col3=1; //FOR PASSWoRD INPUT do { lcd_cmd(0x01); lcd_cmd(0x81); lcd("ENTER PASSWORD:"); pass_set(); for(e=0;e<4;e++) dig_input_recheck[e]=dig_input[e]; lcd_cmd(0x01); lcd("CONFIRM PASSWORD:"); pass_set(); for(e=0;e<4;e++) { if(dig_input_recheck[e]==dig_input[e]) j++; } if(j<4) { lcd_cmd(0x01); lcd_cmd(0x85); lcd("PASSWORD"); lcd_cmd(0xC2); lcd("NOT MATCH"); delay(300); } //Clear LCD screen

} while(j<4);

while(count<4) { lcd_cmd(0x01); lock_output=0; lcd_cmd(0x82);

//Code input and check

lcd("ENTER PASSWORD"); check=0; row1=row2=row3=row4=0; while(col1==1 && col2==1 && col3==1); for(i=0;i<4;i++) { delay(100); lcd_cmd(0xc4+i); row1=row2=row3=row4=0; while(col1==1 && col2==1 && col3==1); row1=row2=row3=row4=0; if(col1==0) check_col1(); else if(col2==0) check_col2(); else if(col3==0) check_col3(); } count++; delay(1); }

if(count==4)

{ lcd_cmd(0x01); lcd_cmd(0x86); lcd("SORRY"); lcd_cmd(0xc1); lcd("NO MORE TRIALS"); while(1); } }

AT89C51 is an 8-bit microcontroller and belongs to Atmel's 8051 family. AT89C51 has 4KB of Flash programmable and erasable read only memory (PEROM) and 128 bytes of RAM. It can be erased and program to a maximum of 1000 times.

In 40 pin AT89C51, there are four ports designated as P1, P2, P3 and P0. All these ports are 8-bit bidirectional ports, i.e., they can be used as both input and output ports. Except P0 which needs external pull-ups, rest of the ports have internal pull-ups. When 1s are written to these port pins, they are pulled high by the internal pull-ups and can be used as inputs. These ports are also bit addressable and so their bits can also be accessed individually.

Port P0 and P2 are also used to provide low byte and high byte addresses, respectively, when connected to an external memory. Port 3 has multiplexed pins for special functions like serial communication, hardware interrupts, timer inputs and read/write operation from external

memory. AT89C51 has an inbuilt UART for serial communication. It can be programmed to operate at different baud rates. Including two timers & hardware interrupts, it has a total of six interrupts.

Pin Diagram:

Pin Description:

Pin No 1 2 3 4 5 6 7 8

Function

8 bit input/output port (P1) pins

Name P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

Reset pin; Active high Input (receiver) for serial RxD communication Output (transmitter) for serial TxD communication External interrupt 1 Int0 External interrupt 2 Int1 Timer1 external input T0 Timer2 external input T1 Write to external data memory Write Read from external data memory Read

Reset P3.0 P3.1 8 bit input/output port (P3) pins P3.2 P3.3 P3.4 P3.5 P3.6 P3.7 Crystal 2 Crystal 1 Ground P2.0/ A8 P2.1/ A9 P2.2/ A10 P2.3/ A11 P2.4/ A12 P2.5/ A13 P2.6/ A14 P2.7/ A15 PSEN ALE Prog EA Vpp P0.7/ AD7 P0.6/ AD6 P0.5/ AD5 P0.4/ AD4 P0.3/ AD3 P0.2/ AD2 P0.1/ AD1 P0.0/ AD0 Vcc

Quartz crystal oscillator (up to 24 MHz) Ground (0V)

8 bit input/output port (P2) pins / High-order address bits when interfacing with external memory

Program store enable; Read from external program memory Address Latch Enable Program pulse input during Flash programming External Access Enable; Vcc for internal program executions Programming enable voltage; 12V (during Flash programming)

8 bit input/output port (P0) pins Low-order address bits when interfacing with external memory

Supply voltage; 5V (up to 6.6V)

Vous aimerez peut-être aussi