Keyscan:
'FlagS[0]="C"
Debounce=1 ' Setup the initial value for Debounce
Key=0 ' Clear the variable KEY, prior to scanning
' TRISA=%00100000
' TRISB = %00001111
' Option_Reg.7=0 ' Enable Internal PortB Pullup Resistors
PORTB.4=0
PORTB.5=1
PORTB.6=1
PORTB.7=1 ' Pull the fourth Row line LOW
GoSub Scancol ' Scan the columns
IF K_Flag=1 Then GoTo Map ' If a key is pressed then map it
PORTB.4=1
PORTB.5=0
PORTB.6=1
PORTB.7=1 ' Pull the fourth Row line LOW
GoSub Scancol ' Scan the columns
IF K_Flag=1 Then GoTo Map ' If a key is pressed then map it
PORTB.4=1
PORTB.5=1
PORTB.6=0
PORTB.7=1 ' Pull the fourth Row line LOW
GoSub Scancol ' Scan the columns
IF K_Flag=1 Then GoTo Map ' If a key is pressed then map it
PORTB.4=1
PORTB.5=1
PORTB.6=1
PORTB.7=0 ' Pull the fourth Row line LOW
GoSub Scancol ' Scan the columns
IF K_Flag=1 Then GoTo Map ' If a key is pressed then map it
D_Flag=0 ' No key pressed, so Reset debounce flag
Debounce=0 ' No key pressed, so Reset key Debounce flag
GoTo Exit ' Exit from the subroutine
' Do the following code if a key has been pressed
Map: IF D_Flag=1 Then Exit ' Already responded to this press, so exit
D_Flag=1 ' Set Debounce flag
Debounce=0 ' Reset key Debounce flag
LookUp Key,[68,67,66,65,35,9,6,3,0,8,5,2,43,7,4,1,128],Keyval ' Map of the keypad legends for numeric output
' **To convert the output to an ascii value comment the line above and uncomment the line below
LookUp Key,["D","C","B","A","#","9","6","3","0","8","5","2","*","7","4","1",32],Key ' Map of the keypad legends for ascii output
' This subroutine scans the columns
' The bit, K_Flag returns a 1 if a key is pressed, and 0 if no key pressed
' Also, if no key is pressed the variable KEY will return with the value of 16
Exit: Return
Scancol:
K_Flag=1 ' Set K_Flag initially to 1
IF PORTB.0=0 Then DEB_release 'S_Exit ' Return if a key on the first column is pressed
Key=Key+1 ' Else increment KEY, and try another row
IF PORTB.1=0 Then DEB_release 'S_Exit ' Return if a key on the second column is pressed
Key=Key+1 ' Else increment KEY, and try another row
IF PORTB.2=0 Then DEB_release 'S_Exit ' Return if a key on the third column is pressed
Key=Key+1 ' Else increment KEY, and try another row
IF PORTB.3=0 Then DEB_release 'S_Exit ' Return if a key on the fourth column is pressed
Key=Key+1 ' Else increment KEY, KEY now equals 16
K_Flag=0 ' Set the K_Flag to indicate no key pressed
S_Exit: Return ' And exit the subroutine
DEB_release:
'Check to see if key is released
counter=256
DEB_release_l1:
IF PORTB.0=0 Then DEB_release
IF PORTB.1=0 Then DEB_release
IF PORTB.2=0 Then DEB_release
IF PORTB.3=0 Then DEB_release
counter=counter-1
IF counter=0 Then S_Exit
GoTo DEB_release_l1