News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Using low cost 4x4 keypad

Started by See_Mos, Jul 06, 2021, 03:48 PM

Previous topic - Next topic

See_Mos

The low cost keypad I bought was described as a membrane type however I suspect it uses carbon pads on the PCB and carbon on the button to make the closure resulting in high resistance contacts.  The INKEY command was intermittent or failed to work on some buttons.

This is the code I used to replace the INKEY command

' #############################################################################
'  using pullup resistors. COLUMNS ON LOW NIBBLE, ROWS ON HIGH NIBBLE
' #############################################################################
' default for my keypad
' #############################################################################
' 100K pullup resistors hold all port pins high.  Each column in turn is pulled low
' if a button is down then the associated row will also be pulled low
' the row inputs are then inverted to give a TRUE BIT.  Getbit converts the BIT to
' its decimal value
    TRISC = $F0
    LATC = $FF
    While 1 = 1
        Key = 16
        XX = %00000001                          ' Port bit to pull low
        LATC = $FF ^ XX                         ' XOR to invert
        For Column = 0 To 3
            If PORTC & $F0 <> $F0 Then          ' a button is down
                DelayMS 50                      ' debounce
                If PORTC & $F0 <> $F0 Then      ' check again
                    Temp = ~ PORTC              ' get PORTC and invert bits
                    Temp = Temp >> 4            ' because we want only the high nibble
                    For Index = 0 To 3
                        If GetBit Temp,Index = True Then Key = Index
                    Next
                    Key = Key + Column * 4
                EndIf
            EndIf
            XX = XX << 1
            LATC = $FF ^ XX
        Next                   
        MyChar = LookUp Key, ["1", "4", "7", "*",_        ' Convert the keypad's keys to ASCII characters
                                "2", "5", "8", "0",_
                                "3", "6", "9", "#",_
                                "A", "B", "C", "D", 255]
        Print At 1,1,Dec Key," "
        Print At 2,1,MyChar                                         
        DelayMS 200
    Wend

I couldn't think of a good alternative to the variable 'XX'

Yasin

I've been using this for years too. PORTB is defined as Keypad Port. I activate Pullups resistors while reading Keypad. When the reading is finished, I use the same port as the GLCD dataport.
I think I adapted it from an example of Les.


KEYPAD_READ_SUB:
INTCON2.7 = 0
Getkey:
    Nop : Nop : PORTB = 0 : TRISB = $F0 : Nop : Nop
Getkeyp:
    For Col = 0 To 3
        PORTB = 0 : TRISB = (Dcd Col) ^ $FF : Nop : Nop : Row = PORTB >> 4
        If Row <> $0F Then GoTo Gotkey
    Next
Gotkey:
    KEY = (Col * 4) + (Ncd (Row ^ $0F))
    If Row = 15 Then
        KEY = 16
    Else
        KEY = KEY - 1
    EndIf

    Return


See_Mos

Just shows that there are many solutions to most coding problems.

With the existing wiring the key sequence is reversed but that is not a problem.