by K.S.Sankar - Mostek Electronics ( www.mostek.biz )
Originaly published in ELECTRONICS FOR YOU magazine ( INDIA - W W W . E F Y M A G . C O M )
Code locks can
be constructed using digital Ics and timers based on valid inputs and invalid Key
strokes. These circuits require too many Ics.
Here is a
simple design based on ATMEL 89c2051
a 20-pin micro controller that does the whole job of detecting a 4-digit
sequential code with time limit facility with the help of only one more
additional Ic ( a buffer 4050) and a transistor to latch a relay.
With the cost
of micro controllers now dropping as low as 4 digital IC gates ,
It makes sense
to design simple logic circuits using them with free ware software languages.
The project
uses an LCD display for design and development but it is not really necessary
and can be removed from the circuit without any change in source code.
LCD models are available in 16 pin or 14 pin configuration.
The 16 pin variety has a back light option. Popular brands are LAMPEX,
HANTRONIX and Hitachi. Most other models also have the same pin configuration.
Note the pin
numbers before soldering to the circuit.
The model used
n this project is LAMPEX –model LM16200 Alpha numeric with 16 character and 2
lines with back light option.
During our
experiments , we found that any unbranded LCD display works as well and the
project is not specific to any brand
The 10k Potentiometer controls the
contrast of the LCD panel works best when it is nearer to ground potential.
The 2 line x 16 character LCD
modules are available from a wide range of
manufacturers and should all be compatible. The source program in BASCOM-51 is listed below:
'--------------------------------------------------------------
' file:
efy20LOC.BAS
' micro controller based 4 digit code lock
' written in bascom-51 language from www.mcselec.com
' by K.S.Sankar www.mostek.biz june 2006
'--------------------------------------------------------------
' pass = 1 4 2 8 binary ( 1324) decimal
$crystal = 6000000
$regfile = "89c2051.dat"
Dim I As Byte
Dim K(4) As Byte
Dim Pass(4) As Byte
Dim Key As Byte
Dim Invalid_pass As Bit
Dim Sec_count As Byte
Dim Clock_word As Word
Dim Passtime As Byte
Dim Attempts As Byte
Dim Maxattempts As Byte
Ready_led Alias P1.0
Relay_out Alias P1.1
For I = 1 To 4
K(i) = 0
Next I
Pass(1) = 1
Pass(2) = 3
Pass(3) = 2
Pass(4) = 4
Sec_count = 0
Passtime = 5
Attempts = 0
Maxattempts = 3
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.3 , Rs = P1.2
'port 1
P1 = 0
P3 = 255
Config Timer0 = Timer , Gate = Internal , Mode = 2
'Timer0 use timer 0
'Gate = Internal no external interrupt
'Mode = 2 8 bit auto reload
' set t0 internal interrupt 2000 times a sec
On Timer0 Timer_0_overflow_int
Load Timer0 , 250
Priority Set Timer0
Enable Interrupts
Enable Timer0
Begin:
If Attempts >= Maxattempts Then
Locate 0 , 0 : Lcd
Maxattempts ; " attempts over"
Locate 2 , 0 : Lcd "try after 10 seconds"
Attempts = 0
Gosub Trylater
End If
Sec_count = 0
For I = 1 To 4
K(i) = 0
Next I
Cls
Cursor On Blink
'clear the LCD display
Lcd "Enter Pass:"
'display this at the top line
Ready_led = 1
For I = 1 To 4
While 1 = 1
If Sec_count > Passtime Then
Exit For
End If
If P3 <> 255 Then
' some key
pressed - check it
If I = 1 Then
' start
timer0 on first keystroke
Sec_count = 0
Start Timer0
End If
Key = P3
' wait for key
release
While Key = P3
Wend
K(i) = 255 - Key
If K(i) = 1 Then
Goto Lcd_out
End If
If K(i) = 2 Then
Goto Lcd_out
End If
If K(i) = 4 Then
K(i) = 3
Goto Lcd_out
End If
If K(i) = 8 Then
K(i) = 4
Goto Lcd_out
End If
If K(i) = 16 Then
K(i) = 5
Goto Lcd_out
End If
If K(i) = 32 Then
K(i) = 6
Goto Lcd_out
End If
If K(i) = 128 Then
K(i) = 8
Goto Lcd_out
End If
' invalid key
combination
Key(i) = 0
Lcd_out:
Lcd K(i)
Waitms 30
Exit While
End If
Wend
Next I
Ready_led = 0
Stop Timer0
' check if time over
If Sec_count > Passtime Then
Locate 2 , 0 : Lcd "time over"
Incr Attempts
Gosub Error_flash
Wait 1
Goto Begin
End If
' check valdity
Invalid_pass = 0
For I = 1 To 4
If K(i) <> Pass(i) Then
Invalid_pass = 1
End If
Next I
If Invalid_pass = 1 Then
Goto Invalid
End If
Valid:
Locate 2 , 0 : Lcd "valid password"
Relay_out = 1
Wait 1
Relay_out = 0
Goto Begin
Invalid:
Locate 2 , 0 : Lcd "invalid"
Gosub Error_flash
Incr Attempts
Wait 1
Goto Begin
Trylater:
' wait for 10 seconds
For I = 1 To 10
Wait 1
Key = P3
Key = 255 - Key
If Key = 3 Then
Exit For
End If
Next I
Wait 2
Return
Error_flash:
For I = 1 To 10
Ready_led = Ready_led Xor 1
Waitms 100
Next I
Ready_led = 0
Return
' interrupt subroutine -----------------
Timer_0_overflow_int:
' program comes here 2000 times a sec with a 6mhz xtal
Incr Clock_word
If Clock_word > 2000 Then
Clock_word = 0
Incr Sec_count
End If
Return
End
' ==-=-=-=-=-=-=-=-=-=-=-=-==-==
The program in
hex is only 1.6k and the 2051 micro can take upto 2k of code. This program
can be modified to suits users requirement.
The hex file
should be ‘burnt’ into the chip using any universal programmer.
Circuit diagram below:

Circuit Explanation
The micro
controller used is ATMEL AT89c2051 a 20pin device with 2k of program memory.
Port-1 is used
to drive the LCD display in 4-bit mode with 10k pullup resistors. A crystal of 6Mhz
is used.
Timer0 is used
as an internal COUNTER and increments a
variable every second. This is used in the project to time the delay taken to
enter the code.
The software
switches on a ready led and waits for a 4 digit code. The valid code in this
project is 1324.
The 8 input
switches are connected to port-3 and the LCD to port-1.
Port-3 does
not have the bit p3.6 and it is ignored.
Two leds are
at port 1 bit0 and bit 1 to indicate a READY and RELAY ON indication.
The relay ON
pin is also connected to a transistor through a buffer to swtich ON a 12v Relay
which can activate the electric lock.
A timer is
started on the first key stroke and the remaining 3 digits have to be entered
within 5 seconds. If not the software gets back to the beginning.
After 3
attempts the circuit will wait for about 10 seconds to avoid unwanted tampering
attempts.
All these
timings can be changed to suit the readers requirement in the source program.
The software
is written using BASCOM-51 ( more about it in the authors previous article in JAN 2005 issue – REAL TIME CLOCK using micro controller)
The source code is self explanatory
|