Device = 16F628A
Xtal=4
All_Digital=true ; all port A switch to digital mode
PortB_Pullups true
Dim num As Byte ; num :variable to store the pressed keys
Dim r As Byte ; r : variable for repetitions
Dim buzzer As PORTA.0 ; buzzer : PORTA.1 (buzzer pin)
Dim led As PORTA.1 ; led : PORTA.2 (led pin)
Dim door As PORTA.2 ;door : portA.3 ( lock relay pin)
Dim A As PORTB.0 ; A: row1 pin
Dim B As PORTB.1 ; B: row2 pin
Dim C As PORTB.2 ; C: row3 pin
Dim D As PORTB.3 ; D : row4 pin
Dim col1 As PORTB.4 ;col1-col4 the pin names of the columns
Dim col2 As PORTB.5
Dim col3 As PORTB.6
Dim col4 As PORTB.7
Dim set_first As Byte ;Variable to store key 1st number
Dim set_second As Byte ;Variable To store key 2nd number
Dim set_third As Byte ;Variable To store key 3rd number
Dim set_fourth As Byte ;Variable To store key 4th number
set_first =1
set_second =2
set_third =3
set_fourth =4
Starting: ;led and buzzer to know it is working
High led : High buzzer
DelayMS 500
Low led : Low buzzer
GoTo first_key ; go to compare 1st key
storing: ;to change the key
GoSub press_effect : High led ;
GoSub scan : GoSub press_effect ;go to scan and return with a key "num" value
High led ;keep LED lit
set_first = num ;save the value of number to set_first
store2:
GoSub scan : GoSub press_effect ;go to scan and return with a key "num" value
High led ;keep led lit
set_second = num ;save the value of number to set_second
store3:
GoSub scan : GoSub press_effect ;go to scan and return with a key "num" value
High led ;keep led lit
set_third = num ;Save the value of number to set_third
store4:
GoSub scan : GoSub press_effect ;go to scan and return with a key "num" value
High led ;keep LED lit
set_fourth = num ;Save the value of number to set_fourth
GoTo Starting ; go to beginning
scan:
Low A ;rowA sensing
If col1 = 0 Then num =1 :Return ;if rowA and col1 key pressed return with num = 1
If col2 = 0 Then num =2 :Return ;If rowA And col2 key pressedreturn with num = 2
If col3 = 0 Then num =3 :Return ;If rowA And col3 key pressedreturn with num = 3
If col4 = 0 Then num =10:Return ;If rowA And col4 key pressedreturn with num = 10
High A
Low B ;rowB sensing
If col1 = 0 Then num =4 : Return
If col2 = 0 Then num =5 : Return
If col3 = 0 Then num =6 : Return
If col4 = 0 Then num =11: Return
High B
Low C ;rowC sensing
If col1 = 0 Then num =7 : Return
If col2 = 0 Then num =8 : Return
If col3 = 0 Then num =9 : Return
If col4 = 0 Then num =12: Return
High C
Low D ;srowD sensing
If col1 = 0 Then num =14: Return
If col2 = 0 Then num =0 :Return
If col3 = 0 Then num =15: Return
If col4 = 0 Then num =13: Return
High D
DelayMS 10
GoTo scan
; ***************** loops for key press effect and key debounce *************
press_effect:
High led : High buzzer ;generates led lit and sound each key is pressed
DelayMS 100 ;100 milliseconds duration
Low led : Low buzzer ;sound and led off
spacing: ;loop For key debounce
If col1 = 0 Then spacing ;If the key is pressed go spacing (wait for release)
If col2 = 0 Then spacing ;If the key is pressed go spacing (Wait For release)
If col3 = 0 Then spacing ;If the key is pressed go spacing (Wait For release)
If col4 = 0 Then spacing ;If the key is pressed go spacing (Wait For release)
DelayMS 25 ;debounce
Return ;returns if you release the keys
; ***************** key comparison************************
first_key:
GoSub scan ;go to scan and return with a value
GoSub press_effect ;Sound and light effect On key pressed
If num = set_first Then second_key ;if the number is equal to set_first
GoTo untrue ;Otherwise go to loop untrue
second_key:
GoSub scan :GoSub press_effect ;go to scan and return with a value
If num = set_second Then third_key ;If the number is equal To set_second
GoTo untrue1 ;Otherwise go To loop untrue1
third_key:
GoSub scan :GoSub press_effect ;go To scan And Return with A value
If num = set_third Then fourth_key ;if the number is equal To set_third
GoTo untrue2 ;Otherwise go To loop untrue2
fourth_key:
GoSub scan :GoSub press_effect ;go To scan And Return with A value
If num = set_fourth Then OPEN ;If the number is equal To set_fourth
GoTo untrue3 ;Otherwise go To loop untrue3
OPEN:
For r = 1 To 2 ;Two beeps indicates correct key
DelayMS 100
High led : High buzzer
DelayMS 100
Low led : Low buzzer
Next
High door ;connects the relay (open door)
DelayMS 1000 ;wait 1 second
Low door ;disconnect relay
High A: High B :High C :Low D ;sensing only the row D
If col4 = 0 Then storing ;corresponding to "D" key
GoTo first_key ;go back to compare keys
; ***************** loops for error keys *****************
untrue:
GoSub scan :GoSub press_effect ;these keys do not equate
untrue1: ;any expected pressed key
GoSub scan :GoSub press_effect ;to the End 4 keys And does nothing
untrue2:
GoSub scan :GoSub press_effect
untrue3:
For r = 1 To 3 ;3 beeps indicates incorrect password
DelayMS 100
High led : High buzzer
DelayMS 100
Low led : Low buzzer
Next
GoTo first_key ;go back to compare keys
End