18F Keypad (4*4) Example

    Swordfish has its own keypad library for 4 * 4 keypads, but I don't understand why it doesn't take a different approach to remove the need for external resistors as well to interface with the keypads...

    Solution: Make my own Library! By only making a single pin and output, setting it high, and checking for a corresponding high input on the matrix, I can determine what button was pressed! You will still need a resistor network to tie the input pins to earth, but this a single component compared to 4 separate resistors.

    I don't have a 16 Button keypad like the one above in my simulation software, but I do have the one shown below. The box looking thing at the top is a virtual terminal for USART data to be displayed with.

Circuit Diagram:

16 Button Keypad Entry

Note the PIC's power supply/oscillator are not shown

    If your using PORTB for the keypad, then be sure to disable PORTB internal pull-ups if they are not already configured by default.

    As each key does not represent its physical location (i.e. you press 1, it will return 5), the Key results must be formatted, I have done this by using a simple Select Case routine as shown below; 

Device = 18F2520
Clock = 8
Config OSC = INTIO67

Dim Key As Byte

#option KEYPAD_PORT = PORTB
Include "INTOSC8.bas"           // sets up the internal oscillator for 8Mhz straight away
Include "usart.bas"             // to allow for USART data sending/receiving
Include "Keypad16.bas"          // for interfacing with 12 button keypads
Include "Convert.bas"           // to convert numbers into ascii strings


USART.SetBaudrate(br9600)

While True
    // wait for a key to be pressed, then convert it and send via uart
    Key = 0
    While Key = 0
        Key = Keypad16.Value
    Wend
    Select Key
        Case 1
            Key = "C"
        Case 2
            Key = "0"    
        Case 3
            Key = "="    
        Case 4
            Key = "+"    
        Case 5
            Key = "1"    
        Case 6
            Key = "2"    
        Case 7
            Key = "3"    
        Case 8
            Key = "-"    
        Case 9
            Key = "4"    
        Case 10
            Key = "5"    
        Case 11
            Key = "6"    
        Case 12
            Key = "*"    
        Case 13
            Key = "7"    
        Case 14
            Key = "8"    
        Case 15
            Key = "9"    
        Case 16
            Key = "/"    
    End Select
    USART.Write("Key Pressed: ", Key, 13)
    // wait for the user to release the key
    Keypad16.Debounce    
Wend

     Video Tutorials:

  • None as yet
  • None as yet

    Download Spency's Keypad16.Bas Library

    Link: What is a Swordfish Library?

Where you can get the components;

 Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Expand 16F PIC Examples16F PIC Examples
Collapse 18F PIC Examples18F PIC Examples
LED's
Switches
Collapse 7 Segment Displays7 Segment Displays
12 Button Keypad
16 Button Keypad
LCD's
Expand 7 Segment Displays7 Segment Displays
ADC
ADC (Another Example)
EEPROM's
DS1307
Expand RS232 and UARTRS232 and UART
DS18B20
External ADC
Hall Effect Sensor
Pulse Width Modulation (PWM)
Infrared UART
Swordfish Modules
Expand Code SnippetsCode Snippets
Expand Handy TipsHandy Tips