Code:
'****************************************************************
'* Name : 60 Keys_Kpad.BAS *
'* Author : [a.ratti] *
'* Notice : Copyright (c) 2008 *
'* : All Rights Reserved *
'* Date : 20/11/2008 *
'* Version : 2.2 *
'* Notes : Micro used: Pic 16F84a *
'* : The software will decode any matrix keypad 4 x n *
'* : where n is any columns number from 1 to 15 *
'****************************************************************
TrisA = %00000000
TRISB = %11111111
Define OSC 4
'--- EEprom ASCII code assignment to define keys response ---------
'------------------- First Row --------------------
Eeprom 1,[49,50,51,52,53,54,55,56,57,48,43,40,94,91,27]' (1,1)-(1,15)
'--------------------Second Row --------------------
Eeprom 16,[65,66,67,68,69,70,71,72,73,74,45,41,37,93,26]' (2,1)-(2,15)
'--------------------- Third Row ------------------
Eeprom 31,[75,76,77,78,79,80,81,82,83,84,47,64,36,60,62]' (3,1)-(3,15)
'--------------------- Fourth Row ------------------
Eeprom 46,[85,86,87,88,89,90,46,58,59,61,42,35,38,32,13]' (4,1)-(4,15)
'------------------------------------------------------------------
'--------------------------- Set variables and pins --------------------
LED VAR PortA.0 ' Heart beating led
Buz var PortA.1 ' got character acknowledgement
Tx var PortA.2 ' Serial out
RTS var portA.3 ' Ready to sent when low
T9600 con 2 ' baud rate = 9600
UCase var byte
KeyFlag var byte
KbOut var Byte [2] ' Buffer Array
high Tx ' purge serial line (True Mode)
High RTS ' disable RTS
low Buz ' Bepper off
KeyFlag = 0 ' reset variable
Ucase = 0 ' set uppercase
'-------------------------- Main program -------------------------------
Loop:
if KeyFlag>0 then 'If <> 0 then Tx ASCII character and Beep
Low RTS 'RTS flag enabled
pause 20
if KbOut[1]=26 then Loop 'Skip ascii code 26
If KbOut[1]=>65 then 'Skip ascii code <> character
KbOut[0] = KbOut[1] + UCase 'If UCase = 0 then UpperCase
else
KbOut[0] = KbOut[1]
endif
'---------------------------------------------------------------------
Serout Tx,T9600,[KbOut[0]] ' Tx ascii character
'----- Choose one of the two serout comand DON'T SELECT BOTH !!! -----
'Serout Tx,T9600,[#KbOut[0]] ' Tx ascii code
'---------------------------------------------------------------------
High RTS ' RTS flag disabled
high buz ' Beep on
pause 30 ' time duration of the beep
low Buz ' Bepper off
pause 150
endif
pause 50 ' Heart beating frequency
toggle Led ' Heart beating
while KeyFlag>0 ' stop auto repeat
KeyFlag = PortB
KeyFlag=KeyFlag & %00001111
wend
'----------------------------- KeyScan routine ------------------------
KeyFlag=0 ' reset flag variable
'------------------------ First Row - keys from 1 to 15 --------------
TRISB = %11101111
PortB =16
If PortB>16 then ' one key of the first row has been pressed
KeyFlag=PortB
KeyFlag=KeyFlag & %00001111 ' decode to which column the key belongs
Gosub GetChar
goto Loop
endif
'------------------------ Second Row - keys from 16 to 30 -------------
TRISB = %11011111
PortB =32
If PortB>32 then ' one key of the second row has been pressed
KeyFlag=PortB
KeyFlag=KeyFlag & %00001111
KeyFlag = KeyFlag +15 ' decode to which column the key belongs
gosub GetChar
goto Loop
endif
'----------------------- Third Row - keys from 31 to 45 ---------------
TRISB = %10111111
PortB =64
If PortB>64 then ' one key of the third row has been pressed
KeyFlag=PortB
KeyFlag=KeyFlag & %00001111
KeyFlag = KeyFlag +30 ' decode to which column the key belongs
gosub GetChar
goto Loop
endif
'------------------------ Fourth Row - keys from 46 to 60 -------------
TRISB = %01111111
PortB =128
If PortB>128 then ' one key of the fourth row has been pressed
KeyFlag = PortB
KeyFlag=KeyFlag & %00001111
KeyFlag = KeyFlag +45 ' decode to which column the key belongs
gosub GetChar
endif
goto Loop
'------------------------ End of keyscan routine ----------------------
'----------------------------------------------------------------------
'---------------- Select Uppercase or LowCase letters -----------------
'------ (Ucase = 32 then lowerCase) (UCase = 0 then UpperCase) -------
'----------------------------------------------------------------------
GetChar: ' Read ASCII code from eeprom & set UCase or LCase
Read KeyFlag, KbOut[1]
If KbOut[1]=26 then
If UCase=0 then
Ucase=32
else
UCase=0
endif
endif
return
end
Bookmarks