Progetti - PIC


Low power 4x3 keypad encoder

It may be convenient to have a keyboard with output already coded; this allows to save time in writing the program for a microcontroller customer who, perhaps, has no pin sufficient for scanning of the matrix. 

Certainly, there are encoders for keypad, as 74C922 (up to 16 keys) and 74C923 (up to 20 keys), but have always had a very high cost and this is not recommended their use, especially since we can perform the same task using a small microcontroller , much cheaper. 

In particular, we address to the PIC16F505, a 14-pin Baseline. The choice of this component is not some recent and very little performance is desired for two simple reasons: the first is that it is probably the PIC 14-pin with the lowest cost; this (less than 0.8 on microchipDIRECT) makes its introduction in the circuit weights very little, also because it takes only 5 other components, in addition to the keyboard. You could also use 16F54, even less expensive, but it is a 18-pin and needs an external oscillator, which, after all, invalidate the small difference in the cost of the chip. 

The second is that, for this application, you do not need any particular performance. In fact, we use only the function of awakening from sleep to level change on pins (IOC).

The circuit.

The circuit is provided to a 4x3 matrix keyboard, use fairly common. You can still encrypt any number of buttons below.

We rely on the function IOC, which is programmable to be activated RB0,1,3,4 and where the weak pull-up integrated, which saves the external resistors.

The resistors in series to the keyboard are optional and have the purpose of limiting ESD problems to the contact between keys and operator. Typical value is 100 ohm.
The resistances in series with the data lines are optional and are used to protect both from ESD that short circuits. Even here we are around 100 ohm, not critical.
JP1 is the connector to access the data and also includes the power supply, which can go from 2 to 5.5V. Resistances RN1 are pull down (100k) (see explanation below).
SP1 is a miniature speaker from 50-120 ohms or a piezo element or a buzzer (see explanation below). The diode D1 is only necessary if it is an electromagnetic element; a piezo element will be replaced with a resistor of approximately 1k.

The capacitors C1 are the usual 100nF close to the pins of the chip and a 47uF electrolytic.
It 'taking on board ICSP to modify the firmware of the micro. 

The circuit can be experienced on LPCuB:

4 jumper flying connect the output pin data with LEDs 0,1,2,3 show their status.
A 7-pin cable connects the keyboard.
The jumper flying "pink" connects the buzzer on the board.

The firmware.

The chip is normally able to sleep at very low power consumption, which can not load power even in low power circuits.
The output data is in three-state and is maintained at a low level with a pull-down. The host processor, reading the data lines, will find 0000 if no key is pressed.

When a key is pressed, it generates a wake-up and the microcontroller scans the columns.
If the button is pressed for less than the debounce time or is it a noise, the microcontroller waits for the release of all keys and returns to sleep. The host processor read line on 0000 data.
If a valid key is pressed, its value is reset to the data lines, which are configured as an output; the host processor will read a value other than 0. The buzzer emits a short beep.
Data lines return to three-state when the button is released.
If multiple keys are pressed simultaneously on different columns, for example 1 and (column 1) and 2 (column 2) the program detects only the key of the lower column, in this case the button 1. If desired, you can change the program to receive more keys.

To simplify the interface data has avoided the use of a line Data Available that notifies the host of the presence of a key pressed and has chosen the following solution:

  • data lines are 0 if no valid key is pressed
  • if a key is pressed, data lines indicate the value of the butto +1

Then :

D3 D2 D1 D0 Tasto
0 0 0 0 none
0 0 0 1 0
0 0 1 0 1
0 0 1 1 2
0 1 0 0 3
0 1 0 1 4
0 1 1 0 5
0 1 1 1 6
1 0 0 0 7
1 0 0 1 8
1 0 1 0 9
1 0 1 1 *
1 1 0 0 #

When the host verifies a given other than 0, she picks it up and processes it by simply subtracting 1 to obtain the value of the key pressed.

These values are part of a lookup table that contains all the 64 possible combinations of the levels of the 5 pin of PORTB.

    movf  keyold,w    ; verifying if 0
; if 0 not a valid key pressed 
; if !=0 key pressed or not

    skpnz             ; n - next test
     goto kbnook      ; =0 - not a valid key
; if 0xF = no key pressed

    xorlw 0x0F        ; invert
    skpz              ; if 0 go to next column
     goto kbok        ; if !=0, is a valid key, then debounce it
    goto  scl1        ; next column

The use of a retlw table It allows to minimize the decoding operations of the key and gives the possibility to easily vary the values according to need. 
Do not change the locations to 0 and Fh since these values are used by the program for the selections:

; keypad layout EOZ ECO.12150.06
; <pin #>
;     col0 col1 col2
;     <x1> <x2> <x3>
; <y1> 1    2     3 row0
; <y2> 4    5     6 row1
; <y3> 7    8     9 row2
; <y4> *    0     # row3
;  0 = not a valid key
; Fh = no key pressed
; Key from 0 to 9 are incremented of 1
; * -> B , # -> C

           addwf PCL,F
;   0  1  2  3  4  5  6   7  8  9  A   B  C  D  E  F
 dt 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,0xC ; 00-0F
 dt 0, 0, 0, 0, 0, 0, 0,0xA, 0, 0, 0,  0, 0, 7, 4,0xF ; 10-1F
 dt 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0,0xB, 0, 0, 0, 1  ; 20-2F
 dt 0, 0, 0, 8, 0, 0, 0,  9, 0, 5, 2,0xF, 0, 6, 3,0xF ; 30-3F

You can implement other alternatives as regards the exit; for example, if there are problems of piloting of a length of cable between the keyboard and the host, the data lines can be left in sleep configured as outputs after they have been brought to 0. In this case the pull-down is not needed and should be omitted. In this case it is necessary to edit the end of the program:

; key released - sleep
   clrf   PORTC    ; out pin = 0
   clrf   PORTB    ; col0-2 = 0
   bcf    col1     ; col1 = 0
   goto   $+1      ; stabilizzation
   goto   $+1
   movf   PORTB,w  ; clear flag ioc


The buzzer for the beep can be a micro speaker, from 50 to 120 ohms. If the resistance value is low, it will be necessary to put a small resistor in series or to use a buffer. The diode is used to prevent overvoltage on de-energization of the coil.
You can use a piezo element; in this case, in place of the diode will use a resistor (about 1k).
The program generates a square wave from 2-3kHz.
If you need a robust sound, you can use a buzzer. In this case it is not necessary the driving signal, but simply control the pin for the desired time.

; comando buzzer con driver interno
     bsf    buzz
     call   Delay100ms
     bcf    buzz
     retlw  0

Without buffer, the maximum current in buzzer shall not exceed the scope of the pin (25mA).

The source does not provide controls for the adjustment of the pages or indirect reference subroutines, since the entire program is contained in page 0 and tables and subroutines do not exceed the first 256 locations.
The initial selection allows building for 16F505 / 506/526, but everything is easily portable to any other PIC with a sufficient number of pins.


Nothing in particular, given the simplicity of the circuit. For the prototype was made a print of the same size keyboard (EOZ ECO.12150.06), but you can easily use any other model, bearing in mind that it is still a keypad matrix.

The MPLAB project.



Copyright afg. Tutti i diritti riservati.
Aggiornato il 13/05/15.