Proton BASIC Compiler - Matrix to 4-bit Keypad Encoder

  • PicŪ Basic

  • Matrix to 4-bit Keypad Encoder

    By Galaxy

    A keypad-matrix is very nice, but.. they have disadvantages if used with microcontrollers.

    • For a 3 X 4 keypad you need 7 I/O-pins.
    • Such a keypad wants pullup resistors.
    • So, you have to realise that in hardware, or you are forced to a port that has software pullups.
    • If you use portB, (with PICŪ-controllers) then you've lost your external interrupt lines.
    • The scanning of a matrix is, for a controller, a time wasting task.
    • Did i say "you need 7 I/O pins"? Well, make it 8 pins. The inkey command is doing "things" with portB.7 in a way that i can't use that pin as an input.
    • Inkey has taken the last from portB too !
    That has to be different, and that's why I made a matrix decoder that reads the matrix, and transforms it to a 4-bits binary code.
    This code matches with the printing on the key's of a 3 X 4 keypad. These 4 bits can be placed in the upper region of portB, so your interrupt lines (B0 and B1) are free again. Now you don't need pullups anymore, so you can use other ports as well for the keypad.
    That's not all. If even those 4 lines are too much for you, then you can remove the jumper J5. Without jumper J5, every keystroke will be sent in asynchronous serial mode.

    The Schematic

    Please remember that every individual keypad can have it's own connections, so pay attention to this !!
    Pin 1 from the keypad stands for row 3
    Pin 2 ---> row 2
    Pin 3 ---> column 1
    pin 4 ---> row 1
    pin 5 --->column 3
    pin 6 ---> row 4
    Pin 7 --->column 2

    The PICŪ runs on its internal oscillator, so we spare out a crystal and 2 caps.
    A 4-bit code can be taken from the middle 4 pins of J2. The outer pins must be connected to a 5 volt supply and GND.
    If no key is pressed, the output has the value 15 (bin 1111). If a key is pressed, the output gives the corresponding value of that key. The * gets the value 10, and the # gets the value 11. Every keypress gives a friendly beep. Additionally a negative 1 ms pulse is given on J4. You can use this pulse as an interrupt trigger, if you wish. This pulse is generated after the data has arrived at the port. The data stays on the port until the key is released.
    If you've chosen the serial configuration, then the data-signal can be taken from RA0 (pin 5 of the header).
    The signal has a reverse polarity, is 9600 baud, 8 bits wide, and has no parity (9600, 8, n, 1)
    There's also an interrupt pulse here, too. This is sent by RA1 (pin 4 of the header).
    This pulse is generated before the data is sent, giving the main-controller the chance to anticipate it.

    In both configurations (parallel & serial) you are free to use the interrupt pulse. There is no requirement for it.

    Component list

    Quantity reference description
    7 R1-R7 smd-resistors
    1 C1 smd-capacitor
    5 J1-J5 SIL-headers male
    1 J3 beeper
    1 U1 PIC 16F628

    The PCB

    It is a small PCB of approx. 5 X 2.5 cm (2 X 1 "). The PICŪ is a conventional dip.
    All resistors, and the cap are smt-devices. You are free to mount the SIL-header on top or on the bottom of the PCB. Whatever is best for you.

    The Software

    The program is written in Proton+
    Here is the asm-file, together with the include file.
    And we finish with the hex-file.

    I have chosen the RS-232 protocol for this project. If you would rather use another protocol like IēC, or SPI then it's easy to implement in the software.
    Project kindly submitted by: galaxy

    About the Proton Compiler

    Crownhill's Proton Plus Compiler is a part of the Proton Development Suite - A suite of British-developed applications enabling fast development of PICŪ micro's using the PICŪ BASIC Language.
    For more information on the Proton Development Suite, please visit