March 15, 2012

Digital combination key using keypad 4x4


This project is digital combination key using 4x4 keypad with 4 digits of password. The password stored in eeprom memory.
To check the configuration of keypad please check at How to Input Data Mikrocontroller Using Keypad
  • For first time run the program will check the eeprom memory if value = 255 it gives the information to system that system start up for first time so the system give the default password. The password is 1111.
  • If key = password then portc.0 = 1 (key opened)
  • To close the key we also enter the key = password (key closed)
  • If for 3 times wrong input. so system will lock. The lock cannot be opened Even though the microcontroller reset. It caused by key variable stored in eeprom which when the system start up it will check that variable. The One way to open the lock is with give high logic to portc.1 and after that the password back to default the password = 1111
  • To change the password press 1234. Then enter the old password. If password correct then enter the your new password.

Simulation Using Bascom AVR


$regfile = "m8535.dat"
$crystal = 11059200
$baud = 9600
'-------------------------------------------------------------------------------
Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5 , Db6 = Porta.6 , Db7 = Porta.7 , E = Porta.1 , Rs = Porta.0
Config Lcd = 16 * 2

Dim Keyread As Byte , Key As Byte
Dim Dig1 As Byte , Dig2 As Byte , Dig3 As Byte , Dig4 As Byte , Sum As Byte , A As Bit
Dim Eeprom_pass1 As Byte , Eeprom_pass2 As Byte , Eeprom_pass3 As Byte , Eeprom_pass4 As Byte

Config Kbd = Portb , Debounce = 40
Config Portc.0 = Output
Config Portc.1 = Input
Portc.0 = 0
'-------------------------------------------------------------------------------
Gosub Read_eeprom
If Eeprom_pass1 = 255 Or Eeprom_pass2 = 255 Or Eeprom_pass3 = 255 Or Eeprom_pass4 = 255 Then
   Dig1 = 1
   Dig2 = 1
   Dig3 = 1
   Dig4 = 1
   Gosub Write_eeprom
End If

Readeeprom Sum , 10

If Sum = 3 Then
   Gosub Lock_program
End If

Gosub Read_eeprom
Gosub Reset_var
Cls

Sum = 0
A = 0
'-------------------------------------------------------------------------------
Do
      Locate 1 , 1
      Lcd "press run key"
      Keyread = Getkbd()
      If Keyread < 16 Then Gosub Gotkey
Loop
'-------------------------------------------------------------------------------
Gotkey:
Gosub Reset_var
   Do
       Key = Lookup(keyread , Dta)
       Dig1 = Key
       Loop Until Dig1 < 16
       Locate 2 , 1
       Lcd "*" ;
       Waitms 300
   Do
      Keyread = Getkbd()
      If Keyread < 16 Then
         Key = Lookup(keyread , Dta)
         Dig2 = Key
      End If
    Loop Until Dig2 < 16
    Lcd "*" ;
   Waitms 300
   Do
      Keyread = Getkbd()
      If Keyread < 16 Then
         Key = Lookup(keyread , Dta)
         Dig3 = Key
      End If
    Loop Until Dig3 < 16
    Lcd "*" ;
   Waitms 300
   Do
      Keyread = Getkbd()
      If Keyread < 16 Then
         Key = Lookup(keyread , Dta)
         Dig4 = Key
      End If
    Loop Until Dig4 < 16
    Lcd "*"
    Waitms 300


    Gosub Read_eeprom
    If Dig1 = 1 And Dig2 = 2 And Dig3 = 3 And Dig4 = 4 Then Gosub Change_pass
    If A = 0 And Dig1 = Eeprom_pass1 And Dig2 = Eeprom_pass2 And Dig3 = Eeprom_pass3 And Dig4 = Eeprom_pass4 Then
         Gosub Open_key
         Sum = 0
    Else
       Incr Sum
          If Sum = 3 Then
             Writeeeprom Sum , 10
             Gosub Lock_program
          End If
       Cls
       Locate 1 , 1
      Lcd "press run key"
    End If

Return
'-------------------------------------------------------------------------------
Reset_var:
   Dig1 = 16
   Dig2 = 16
   Dig3 = 16
   Dig4 = 16
Return
'-------------------------------------------------------------------------------
Open_key:
   Cls
   A = 1
   Portc.0 = 1
   Locate 1 , 1
   Lcd "press stop key"
   Do
      Gosub Gotkey
      Gosub Read_eeprom
      If Dig1 = Eeprom_pass1 And Dig2 = Eeprom_pass2 And Dig3 = Eeprom_pass3 And Dig4 = Eeprom_pass4 Then Portc.0 = 0
   Loop Until Portc.0 = 0
   Gosub Reset_var
   A = 0
   Cls
Return
'-------------------------------------------------------------------------------
Change_pass:
   Cls
   Locate 1 , 1
   Lcd "enter old password"
   Gosub Gotkey
   Gosub Read_eeprom
      If Eeprom_pass1 = Dig1 And Eeprom_pass2 = Dig2 And Eeprom_pass3 = Dig3 And Eeprom_pass4 = Dig4 Then
         Cls
         Locate 1 , 1
         Lcd "enter new password"
         Gosub Gotkey
         Gosub Write_eeprom
         Locate 2 , 1
         Lcd "password stored"
         Wait 1
      Else
         Cls
         Locate 1 , 1
         Lcd "Wrong password"
         Wait 1
      End If
      Gosub Reset_var
      Gosub Read_eeprom
      Cls
   Return
'-------------------------------------------------------------------------------
Read_eeprom:
   Readeeprom Eeprom_pass1 , 2
   Readeeprom Eeprom_pass2 , 4
   Readeeprom Eeprom_pass3 , 6
   Readeeprom Eeprom_pass4 , 8
Return
'-------------------------------------------------------------------------------
Write_eeprom:
   Writeeeprom Dig1 , 2
   Writeeeprom Dig1 , 4
   Writeeeprom Dig3 , 6
   Writeeeprom Dig4 , 8
Return
'-------------------------------------------------------------------------------
Lock_program:
   Do
   Locate 1 , 1
   Lcd "program locked"
   Locate 2 , 1
   Lcd "wrong 3 times"
   Loop Until Portc.1 = 1
   Gosub Back_to_default
Return
'-------------------------------------------------------------------------------
Back_to_default:
   Dig1 = 1
   Dig2 = 1
   Dig3 = 1
   Dig4 = 1
   Gosub Write_eeprom
   Sum = 0
   Writeeeprom Sum , 10
Return
'-------------------------------------------------------------------------------
Dta:
    Data 1 , 2 , 3 , 10 , 4 , 5 , 6 , 11 , 7 , 8 , 9 , 12 , 13 , 0 , 14 , 15

3 comments:

  1. it seems I have a lot to learn digital

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. salam kenal mas, dan Trim's mas boleh bljar ni mas, sudah saya coba lednya mau hidup tapi passwordnya gk mau diganti katanya WRONG PASSWORD, lw bleh nanya salahnya dmna ya mas?.. apa mungkin sub GOTKEY nya y?...
    mksh mas...

    ReplyDelete

Let's us sharing

Recent Comment

Grab This Widget