PDA

View Full Version : 8x8 keypad matrix...



mbox
- 22nd February 2010, 02:53
Hi, I got my 4x4 keypad working using keypad.bas file from Setve. Now I'm trying to modify it to create 8x8 PortB for rows and PortD for Columns. Kindly check my codes and schematic, I use 1k for the resistors.

TRISB = %00000000
PortB = 0
TRISD = %11111111
PortD = 1
ADCON1 = 7 ;Make PORTA and PORTE digital
OPTION_REG.7 = 0


INCLUDE "KeyPad.bas"
'
' Hardware connection
' ===================
DEFINE KEYPAD_ROW 8 ' 8 row
define KEYPAD_ROW_PORT PORTB ' on PORTB
DEFINE KEYPAD_ROW_BIT 0 '
DEFINE KEYPAD_COL 8 ' 8 col
DEFINE KEYPAD_COL_PORT PORTD ' on PORTD
DEFINE KEYPAD_COL_BIT 0 '


'
' Serial Communication definition
' ===============================
DEFINE HSER_TXSTA 24h ' enable transmit, BRGH=1
DEFINE HSER_SPBRG 129 ' 9600 Bauds

'
' Variables definition
' ===================
myvar var byte

' ---------------------------------[Program Start]----------------------------------------------
start:
@ READKEYPAD _myvar
hserout ["Key=",dec myvar,13,10]
goto start
thanks in advance,
mbox

Archangel
- 22nd February 2010, 03:00
Hi mbox,
Please, always, always post the PIC part number as some really are very different. I have gotten to the point of making a comment in all my code with that information.
Did this work for you with the serial output on the "other" chip you were using?
What chip "was" that?

Did you edit keypad.bas to alert it as to the changes? (Best I remember it defines everything on PortB . .) .
I have had no success porting the program Steve wrote for LLoyd Edwards to use serial LCDs, (did I remember to say thank you LLoyd?), Thank You LLoyd. I will have to revisit it as i probably did something bone headad like try to use Tx pin as output . . .

DaveC3
- 22nd February 2010, 03:27
here is some code I use for a 8X10 switch matrix I think you will get the idea how to modify it for a 8X8 matrix. I do not remember where I got the matrix part of this code so I can not give credit to the author. By the way I do not use any resistors. Works fine for me without.





DEFINE OSC 48
ADCON1 = $F 'port A digital
CMCON = 7 'PortA Digital
PortE.7 = 1 'set weak pullups port D
USBBufferSizeMax con 8 ' maximum buffer size
USBBufferSizeTX con 8 ' input
USBBufferSizeRX con 8 ' output

' the USB buffer...
USBBuffer Var Byte[USBBufferSizeMax]
USBBufferCount Var Byte
TrisB = %00000000
TrisD = %11111111
TrisE.0 = 1
TrisE.1 = 1

shift var byte
keyout var byte
keyold var byte
K_Flag Var Bit
Key Var Byte
Debounce Var Bit ' Flag to indicate a keypress.
D_Flag Var Bit ' Debounce flag used by Inkeys.
keysend var byte
D_Flag=0

'************************************************* ******************************
' Interrupt definition
' ====================
' USB interrupt used to keep USB connection alive
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USB_INT, _DoUSBService, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

pause 500
usbinit ' initialise USB...
USBService ' keep connection alive
UIE = $7F
UEIE = $9F
@ INT_ENABLE USB_INT


goto ProgramStart
' This subroutine Scans the 16 button keypad and returns the key pressed in "KEY"
' If no key is pressed the value returned is 128.
' It also returns a Bit-Flag called DEBOUNCE which is 1 if a key is still in use
' And 0 if not. This acts as a debounce for the Keypad.
Keyscan:
Debounce=1 ' Setup the initial value for Debounce
Key=0 ' Clear the variable KEY, prior to scanning
Portb=%11111110 ' 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
key=10
Portb=%11111101 ' Pull the third row line LOW
Gosub Scancol ' Scan the columns
If K_Flag=1 then goto Map ' If a key is pressed then map it
key=20
Portb=%11111011 ' Pull the second row line LOW
Gosub Scancol ' Scan the columns
If K_Flag=1 then goto Map ' If a key is pressed then map it
key=30
Portb=%11110111 ' Pull the first row line LOW
Gosub Scancol ' Scan the columns
If K_Flag=1 then goto Map ' If a key is pressed then map it
key=40
Portb=%11101111 ' 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
key=50
Portb=%11011111 ' Pull the third row line LOW
Gosub Scancol ' Scan the columns
If K_Flag=1 then goto Map ' If a key is pressed then map it
key=60
Portb=%10111111 ' Pull the second row line LOW
Gosub Scancol ' Scan the columns
If K_Flag=1 then goto Map ' If a key is pressed then map it
key=70
Portb=%01111111 ' Pull the first row line LOW
Gosub Scancol ' Scan the columns
If K_Flag=1 then goto Map ' If a key is pressed then map it
Portb=%11111111

' key=80
' Porta=%11111011 ' 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
' key=90
' Porta=%11110111 ' Pull the third row line LOW
' Gosub Scancol ' Scan the columns

If K_Flag=1 then goto Map ' If a key is pressed then m


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: portb.0 = 0
' if porta.1 = 0 then
' shift = 1
' else
' shift = 0
' endif

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

'Key Code
Exit: Lookup Key,[81,86,88,47,4,84,83,87,82,85,_ '0-9
20,0,21,0,0,0,19,24,17,22,_ '10-19
65,72,69,48,3,68,67,71,66,70,_ '20-29
28,0,29,0,0,0,27,0,25,30,_ '30-39
60,64,61,43,8,0,59,63,36,62,_ '40-49
44,40,37,42,7,0,35,39,41,38,_ '50-59
49,56,53,46,5,52,51,55,50,54,_ '60-69
73,80,77,45,6,76,75,79,74,78,_ '70-79
0,0,0,0,0,0,0,0,0,0,_ '80-89
0,0,0,0,0,0,0,0,0,0],Keyout '90-99 Map of the keypad legends


Return ' Exit from the subroutine

' 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
Scancol:
K_Flag=1 ' Set K_Flag initially to 1
If Portd.0=0 then S_Exit ' Return if a key on the first column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Portd.1=0 then S_Exit ' Return if a key on the second column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Portd.2=0 then S_Exit ' Return if a key on the third column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Portd.3=0 then S_Exit ' Return if a key on the fourth column is pressed
Key=Key+1 ' Else increment KEY, KEY now equals 16

If Portd.4=0 then S_Exit ' Return if a key on the first column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Portd.5=0 then S_Exit ' Return if a key on the second column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Portd.6=0 then S_Exit ' Return if a key on the third column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Portd.7=0 then S_Exit ' Return if a key on the fourth column is pressed
Key=Key+1 ' Else increment KEY, KEY now equals 16
If Porte.0=0 then S_Exit ' Return if a key on the third column is pressed
Key=Key+1 ' Else increment KEY, and try another row
If Porte.1=0 then 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

'key = 0

S_Exit: Return ' And exit the subroutine

' ************************************************** **********
' * receive data from the USB bus *
' ************************************************** **********
DoUSBIn:
USBBufferCount = USBBufferSizeRX ' RX buffer size
USBIn 1, USBBuffer, USBBufferCount, DoUSBIn ' read data, if available
return

' ************************************************** **********
' * wait for USB interface to attach *
' ************************************************** **********
DoUSBOut:
USBBufferCount = USBBufferSizeTX ' TX buffer size
USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
return
'################################################# ##############################

DoUSBService: 'Keeps USB alive
usbservice
@ INT_RETURN

'################################################# ##############################
ProgramStart:
Again: gosub Keyscan ' Go and scan the keypad
If K_Flag = 0 then ' If no key is pressed then do the following: -

Goto Again ' And look again when woken up.
Endif
'High Strobe ' Enable the Strobe pin

Pause 200 ' Delay for 50ms after strobe pin is set HIGH



if keyold != key then
usbbuffer[0] = Keyout ' Transmit the KEY byte
gosub dousbout
endif
keyold = keyout

Goto again ' Do it all again!

mbox
- 22nd February 2010, 22:22
Thanks for the quick responce, I'm using pic16f877, the same PIC I use for my 4x4 keypad. If possible I want to make use the keypad.bas include file work for the 8x8. I did not make any changes to the keypad.bas but instead I include this in my code...
DEFINE KEYPAD_ROW 8 ' 8 row
define KEYPAD_ROW_PORT PORTB ' on PORTB
DEFINE KEYPAD_ROW_BIT 0 '
DEFINE KEYPAD_COL 8 ' 8 col
DEFINE KEYPAD_COL_PORT PORTD ' on PORTD
DEFINE KEYPAD_COL_BIT 0
I also want to make clear, is the DEFINE KEYPAD_ROW_BIT and DEFINE KEYPAD_COL_BIT is where you suppose to connect your first row and column?

regards,
mbox

Archangel
- 23rd February 2010, 02:26
Thanks for the quick responce, I'm using pic16f877, the same PIC I use for my 4x4 keypad. If possible I want to make use the keypad.bas include file work for the 8x8. I did not make any changes to the keypad.bas but instead I include this in my code...

DEFINE KEYPAD_ROW 8 ' 8 row
define KEYPAD_ROW_PORT PORTB ' on PORTB
DEFINE KEYPAD_ROW_BIT 0 '
DEFINE KEYPAD_COL 8 ' 8 col
DEFINE KEYPAD_COL_PORT PORTD ' on PORTD
DEFINE KEYPAD_COL_BIT 0
I also want to make clear, is the DEFINE KEYPAD_ROW_BIT and DEFINE KEYPAD_COL_BIT is where you suppose to connect your first row and column?

regards,
mbox
Hi,
I see mister_e has commented the DEFINES in keypad.bas you should scroll down and see this in amongst the assembly code:

<font color=green>
;
; Default settings
; ================
KB_ROW = 4 ; 4 ROW keypad
KB_ROW_PORT = PORTB ; Keypad ROW on PORTB
KB_ROW_BIT = 0 ; ROW0 = PORTB.0
KB_COL = 4 ; 4 COL keypad
KB_COL_PORT = PORTB ; Keypad Col on PORTB
KB_COL_BIT = 4 ; COL0 = PORTB.4
DebounceDelay = d'200' ; debounce delay 200mSec
</font color>

I do not know if the defines will override this or not, somehow I doubt it, but it very well may.
Also here are some threads on this subject:
http://www.picbasic.co.uk/forum/showthread.php?t=3962&highlight=keypad.bas
http://www.picbasic.co.uk/forum/showthread.php?t=3250

VinuPP
- 9th October 2014, 18:43
Hello In your code you have enabled pullups resistor to vcc in portb so please change your ports
because "Column bits must have Pull-up resistor attach to VCC"
Ex:
DEFINE KEYPAD_ROW 6 ' 6 row
define KEYPAD_ROW_PORT PORTC '
DEFINE KEYPAD_ROW_BIT 0 '
DEFINE KEYPAD_COL 4 ' 4 col
DEFINE KEYPAD_COL_PORT PORTB '
DEFINE KEYPAD_COL_BIT 0 '