PicBasic Pro, PicBasic, BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

--- Quick & Simple ---
16-Key Serial Keypads

          BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List"Micro-News"BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List
                       Micro-Mailing-List

The schematics are from the MeLabs PIC-X1 board, and the code is a hacked version of the demo code for the PIC-X1 keypad interface also supplied by MeLabs for experimentation with the X1 board.

The original code used with the LCDOUT command to send each key-press to an LCD display. I simply deleted the commands for sending each key-press to an LCD, and substituted them with the SEROUT command, made a few tweaks here-and-there, and presto, instant 16-key serial keypad. If you have a spare 16-key matrix keypad lying around, a couple resistors, and a few minutes, you can easily build your own custom serial keypads for use with your next project.

Note: The value for the 4 resistors are not critical. Any value from 270-ohm to 10K should work equally well.

This is a very simple project, yet very effective at what it does. PortA.1 is the serial output-pin, and PortA.0 is assigned as the baud rate selection bit.

A logic 1 on PortA.0 will cause serial key data to be sent at N9600. The opposite, will cause serial key data to be sent at N2400.

Components Used:

bullet

1 PIC16F628-04/P microcontroller

bullet

1 16-key matrix keypad

bullet

1 4MHz ceramic resonator

bullet

4 x 270-ohm, 1/4W resistors

The original prototype was designed using the PIC16F628, but the code shown below can easily be modified to work on any PIC with sufficient I/O capacity for the keypad interface, and the two lines required for the serial output-pin, and baud-rate selection pin.

CMCON is the comparator mode selection register. Loading a value of 7 into CMCON sets-up the PortA I/O-pins as digital, while turning OFF the comparators.

If you prefer to use another PIC that has A/D on PortA, simply replace CMCON = 7 with ADCON1 = 7 to turn OFF the A/D peripheral, and set PortA to digital.

The Schematic:

The Code:

'************************************************************
'*  Name    : serkey16.bas                                  *
'*  Author  : Reynolds Electronics                          *
'*  Date    : 07/12/2001                                    *
'*  Version : 1.0                                           *
'*  Notes   : PicBasic Pro program for 16-key serial keypad *
'************************************************************
INCLUDE "modedefs.bas"

col         VAR   BYTE     ' Keypad column
row         VAR   BYTE     ' Keypad row
key         VAR   BYTE     ' Key value
baud        VAR   PortA.0  ' Baud select pin
serpin      VAR   PortA.1  ' Serial output pin
CMCON       =     7        ' PortA = digital I/O
VRCON       =     0        ' Voltage reference disabled
TRISA       =     %00000001' PortA.0 = baud select pin
OPTION_REG.7 =    0        ' Enable PORTB pull-ups

loop:
        GOSUB getkey          'Get key from keypad
send:
        IF baud = 1 THEN fast'If baud = 1 then N9600,else N2400
	    SEROUT serpin,N2400,[key]'Send key value out PortA.1
        GOTO loop
fast:
	SEROUT serpin,N9600,[key]
        GOTO loop               'Do it forever
getkey: 
        PAUSE 50                'Debounce key-input
getkeyu:' Wait for all keys up
        PORTB = 0            ' All output-pins low
        TRISB = $f0          ' Bottom 4-pins out, top 4-pins in
        IF ((PORTB >> 4) != $f) THEN getkeyu'If keys down, loop
        PAUSE 50             ' Debounce key-input

getkeyp:' Wait for keypress
        FOR row = 0 TO 3        ' 4 rows in keypad
            PORTB = 0           ' All output-pins low
            TRISB = (DCD row) ^ $ff ' Set one row pin to output
            col = PORTB >> 4    ' Read columns
            IF col != $f THEN gotkey' If any keydown, exit
        NEXT row
        GOTO getkeyp            ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
        key = (row * 4) + (NCD (col ^ $f))
        'NOTE: for 12-key keypad, change to key = (row * 3)
        RETURN                  ' Subroutine over
        
        END

Your mileage may vary .....!

Until the next project -  have fun, don't blow anything up, and thanks for stopin by...;o]

-Bruce

Click HERE to return to the PicBasic projects page.

Copyright © 1999-2008
Reynolds Electronics