PDA

View Full Version : need help in coding..



daphne8888
- 19th March 2008, 08:30
hi,
i use pic16f877,a 3x4 keypad and a 16x2 LCD display to do my project.my progress is like this:first,keyin password for privacy,then choose status i want and show it on LCD screen.it will set then.after that,i need to keyin password again to check for correctness(but i tink i didnt do that far).now i stuck in interfacing keypad with pic.i know there must be some mistakes in coding since i just modify the coding without having better understanding in it.so,i really hope somebody can help me edit the coding..a million thankful for helping me..
here is my coding:
' ===================hardware assignment==================

Define LCD_BITS 4 ' 4-bit interface
Define LCD_LINES 2 ' 2 line LCD
Define LCD_DREG PORTB 'bit start from bit 0
Define LCD_DBIT 0
Define LCD_RSREG PORTA 'rs at bit 2
Define LCD_RSBIT 2
Define LCD_EREG PORTA 'e at bit 1
Define LCD_EBIT 4
DEFINE LCD_RWREG PORTA
DEFINE LCD_RWBIT 3

KeyCol Var Byte
KeyRow var byte
KeyPress var byte
KeyButton var byte
Flag Var Bit ' Flag to indicate a keypress.
b0 var byte
b1 var byte
i var byte
j var byte
counter var byte
TempPass var byte[4]
RealPass var byte[4]
ROM var byte
submenu var byte
menu VAR BYTE

Define OSC 4
TRISD = %00001111
'===================program starts==========================
start:
counter = 0
submenu = 0
read 5, ROM
if rom = 255 then
menu = 0
LCDOUT $FE, 1 , "Enter Password: "
lcdout $FE, $C0
gosub ValidateUser
ENDIF
lcdout $FE, 1 , "Password Stored"
pause 2000
LCDOUT $FE, 1 , "Select Status: "
goto mainstatus
'/////////////////////////////////////////////////////////
MainStatus:
counter = 0
if menu = 1 then
lcdout $FE, $C0
lcdout "Come In!"
endif
if menu = 2 then
lcdout $FE, $C0
lcdout "Do Not Disturb!"
endif
if menu = 3 then
lcdout $FE, $C0
lcdout "Out To Meeting!"
endif
if menu = 4 then
lcdout $FE, $C0
lcdout "Away!"
endif
if menu = 5 then
lcdout $FE, $C0
lcdout "Lunch!"
endif
if menu = 6 then
lcdout $FE, $C0
lcdout "Not In!"
endif
goto mainstatus
'///////////////////////////////////////////////////////////
ValidateUser:
for i = 1 to 4
gosub keyscan
temppass[i-1] = keybutton
next i
ValidateUser1:
gosub keyscan
if keypress = 7 then validateuser
if keypress <> 15 then validateuser1

ValidatePassword:
j = 0
for i = 0 to 3
if realpass[i] = temppass[i] then
j = j + 1
else
j = j - 1
endif
next i
return

CorrectCode:
lcdout $FE, 1
lcdout "Correct Password!"
pause 1800
return

WrongCode:
lcdout $FE, 1
lcdout "Wrong Password!"
pause 2000
return
'////////////////////////////////////////////////////////////
Submenu_1:
submenu = 1
lcdout $FE, 1
lcdout "Enter Password:"
lcdout $FE, $C0
gosub validateuser
gosub validatepassword
if j = 4 then
gosub Correctcode
else
gosub wrongcode
endif
goto start

Submenu_2:
submenu = 2
lcdout $FE, 1
lcdout "Enter Password:"
lcdout $FE, $C0
gosub validateuser
gosub validatepassword
if j = 4 then
gosub Correctcode
else
gosub wrongcode
endif
goto start

Submenu_3:
submenu = 3
lcdout $FE, 1
lcdout "Enter Password:"
lcdout $FE, $C0
gosub validateuser
gosub validatepassword
if j = 4 then
gosub Correctcode
else
gosub wrongcode
endif
goto start

Submenu_4:
submenu = 4
lcdout $FE, 1
lcdout "Enter Password:"
lcdout $FE, $C0
gosub validateuser
gosub validatepassword
if j = 4 then
gosub Correctcode
else
gosub wrongcode
endif
goto start

Submenu_5:
submenu = 5
lcdout $FE, 1
lcdout "Enter Password:"
lcdout $FE, $C0
gosub validateuser
gosub validatepassword
if j = 4 then
gosub Correctcode
else
gosub wrongcode
endif
goto start

Submenu_6:
submenu = 6
lcdout $FE, 1
lcdout "Enter Password:"
lcdout $FE, $C0
gosub validateuser
gosub validatepassword
if j = 4 then
gosub Correctcode
else
gosub wrongcode
endif
goto start

daphne8888
- 19th March 2008, 08:31
'// Declare Variables...
Col_A VAR PORTD.4
Col_B VAR PORTD.5
Col_C VAR PORTD.6

Row_A VAR PORTD.0
Row_B VAR PORTD.1
Row_C VAR PORTD.2
Row_D VAR PORTD.3

Scan_Col VAR BYTE ' Counter - current col
Key_Press VAR BYTE ' Value of key (0-9) & * + #
Key_Down VAR BYTE ' Flag set true when key is depressed
Allow_Key VAR BYTE ' Flag - disallow multiple keys being pressed

KeyScan:

@ incf _Scan_Col, 1 ' Inc col pos...

SELECT CASE Scan_Col ' Col (1-3)

CASE 1
Col_A = 0 ' Switch on col (active low)
Col_B = 1 ' Col off
Col_C = 1 ' Col off

'// 3 Key
IF Row_A = 0 THEN ' Key down? ...
IF Allow_Key = 0 THEN ' Any other key down?
Key_Press = 3 ' Load var w/value of key
Allow_Key = 1 ' Disallow other keys
LCDOUT "3"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 6 Key
IF Row_B = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 6
Allow_Key = 1
LCDOUT "6"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 9 Key
IF Row_C = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 9
Allow_Key = 1
LCDOUT "9"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// # Key
IF Row_D = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 35
Allow_Key = 1
LCDOUT "#"
pause 100
lcdout $FE, $10
ENDIF
ENDIF

CASE 2
Col_A = 1
Col_B = 0
Col_C = 1

'// 2 Key
IF Row_A = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 2
Allow_Key = 1
LCDOUT "2"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 5 Key
IF Row_B = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 5
Allow_Key = 1
LCDOUT "5"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 8 Key
IF Row_C = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 8
Allow_Key = 1
LCDOUT "8"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 0 Key
IF Row_D = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 0
Allow_Key = 1
LCDOUT "0"
pause 100
lcdout $FE, $10
ENDIF
ENDIF

CASE 3
Col_A = 1
Col_B = 1
Col_C = 0

'// 1 Key
IF Row_A = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 1
Allow_Key = 1
LCDOUT "1"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 4 Key
IF Row_B = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 4
Allow_Key = 1
LCDOUT "4"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// 7 Key
IF Row_C = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 7
Allow_Key = 1
LCDOUT "7"
pause 100
lcdout $FE, $10
ENDIF
ENDIF
'// * Key
IF Row_D = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 42
Allow_Key = 1
LCDOUT "*"
pause 100
lcdout $FE, $10
ENDIF
ENDIF

Scan_Col = 0
END SELECT
'// Check for key release, all cols on (active low) ...
Col_A = 0
Col_B = 0
Col_C = 0

IF Row_A = 1 THEN
IF Row_B = 1 THEN
IF Row_C = 1 THEN
IF Row_D = 1 THEN
Allow_Key = 0
Key_Down = 0
Key_Press = 255
ENDIF
ENDIF
ENDIF
ENDIF

RETURN