# PIC Microcontroler based Keyboards

## 4x4 Keypad with 1 A/D pin:

Dennis Crawley says:

The diagram below shows how this can be done, by connecting the row/column lines of the keypad to resistors so that each key (the X's in the diagram below) results in a different resistance value. This resistance is connected, along with a load resistor (the 22K shown below), in a voltage divider between Vcc and ground; each key gives a different voltage, thus a different A/D reading.

Observations:

1. It requires 1% resistors, but it could be tested with normal 20% tolerance.
2. It's very stable with voltage fluctuations.
3. Since keyboards come with some resistance 80-100 ohms, and they tend to grow, the soft need some calibration as well as keyboard change over the time,...

Basically, the program transmit each key pressed to a terminal, so the programmer can see the key real value. Once he/she *calibrate* the range the pic is ready to use.

```;**********************************************
LIST    P=PIC16F873,F=INHX8M,R=DEC
#INCLUDE P16F873.INC
__CONFIG _HS_OSC&_WDT_OFF&_CP_OFF&_PWRTE_ON&_BODEN_ON&_LVP_OFF
ERRORLEVEL 1,-302
;**********************************************
;Fosc 16Mhz
;First commented on PicList by Dave Dilatush
;Sat, 9 Feb 2002 20:43:39 GMT
;Re: [PIC]: CHALLENGE: 4x4 keyboard and LCD with 1 port = 8 bits
;
;  X-------X-------X-------X----------+--Vcc
;  |       |       |       |          |
;  |       |       |       |          |
;  X-------X-------X-------X---4.7K---+
;  |       |       |       |          |
;  |       |       |       |          |
;  X-------X-------X-------X---10K----+
;  |       |       |       |          |
;  |       |       |       |          |
;  X-------X-------X-------X---15K----+
;  |       |       |       |
;  |     1.2K    2.4K    3.6K
;  |       |       |       |
;  +-------+-------+-------+--> To PIC A/D Input
;                          |
;                         22K
;                          |
;                         GND
;
CBLOCK 0X070            ;
BUFFER                           ;
LOOPS                            ;
LOOPS2                           ;
NIBBLE1                          ;
NIBBLE2                          ;
NIBBLE3                          ;
FILTER1                          ;
FILTER2                          ;
FILTER3                          ;
VECES                            ;
BUFFERKEY                        ;
ENDC                     ;

ORG     00               ;
GOTO    INIT             ;
;
;Delay. @4Mhz-1ms.
RETARDO                          ;
MOVWF   LOOPS            ;
TOP2    MOVLW   D'110'           ;
MOVWF   LOOPS2           ;
TOP     NOP                      ;
NOP                      ;
NOP                      ;
NOP                      ;
NOP                      ;
NOP                      ;
DECFSZ  LOOPS2           ;
GOTO    TOP              ;
DECFSZ  LOOPS            ;
GOTO    TOP2             ;
RETURN                   ;

GOTO    CONSU            ;
RETURN                   ;

;Resistors and  Keys Resistances
;may vary over the time
;so we established ranges for
;each converted value with:
;addlw (Hi - Lo) + 1
;for a good explanation go to:
;http://www.piclist.com/techref/microchip/rangechk.htm
;********************************;
PARSER                           ;
CLRF   BUFFERKEY          ;Split de hexa into
MOVLW  .3                 ;two nibbles. Test for
XORWF  FILTER1,W          ;Hi nibble, If = 3
BTFSS STATUS,Z            ;Test the low nibble
GOTO   TWO                ;otherwise jump.
;3FC ---> real value got it from TOASCII routine
MOVFW  FILTER2            ;
MOVLW  "1"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;3C4
MOVFW  FILTER2            ;
MOVLW  "2"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;39E
MOVFW  FILTER2            ;
MOVLW  "3"                ;
BTFSC  STATUS,C           ;
GOTO   SAVEKEY            ;
;363
MOVFW  FILTER2            ;
MOVLW  "S"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;345
MOVFW  FILTER2            ;
MOVLW  "4"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;31F
MOVFW  FILTER2            ;
MOVLW  "5"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;305
MOVFW  FILTER2            ;
MOVLW  "6"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;

TWO                              ;Values with hi nibble
;equal to 2
;2DB
MOVFW  FILTER2            ;
MOVLW  "U"                ;U for UP
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;2BE
MOVFW  FILTER2            ;
MOVLW  "7"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;2A3
MOVFW  FILTER2            ;
MOVLW  "8"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;28E
MOVFW  FILTER2            ;
MOVLW  "9"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;271
MOVFW  FILTER2            ;
MOVLW  "D"                ;D for Down
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;25C
MOVFW  FILTER2            ;
MOVLW  "P"                ;P for Program
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;248
MOVFW  FILTER2            ;
MOVLW  "0"                ;
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;238
MOVFW  FILTER2            ;
MOVLW  "B"                ;B for Backspace
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;
;222
MOVFW  FILTER2            ;
MOVLW  "E"                ;E for Enter
BTFSC STATUS,C            ;
GOTO   SAVEKEY            ;

KEYBERROR2                       ;If some keys have
MOVLW  "#"                ;changed their values
SAVEKEY                          ;the soft could handled
MOVWF  BUFFERKEY          ;the error
RETURN                    ;

;********************************;
TOASCII                          ;When is active,
MOVF    FILTER1,W        ;this routine take the
MOVWF   NIBBLE1          ;hex value and convert it
MOVLW   30H              ;into three ascii values
;ABCDEF1234567890
MOVF    FILTER2,W        ;
MOVWF   BUFFER           ;
ANDLW   0FH              ;
MOVWF   NIBBLE3          ;
MOVLW   0AH              ;
SUBWF   NIBBLE3,0        ;
MOVLW   37H              ;
BTFSS   STATUS,C         ;
MOVLW   30H              ;
SWAPF   BUFFER,0         ;
ANDLW   0FH              ;
MOVWF   NIBBLE2          ;
MOVLW   0AH              ;
SUBWF   NIBBLE2,0        ;
MOVLW   37H              ;
BTFSS   STATUS,C         ;
MOVLW   30H              ;
RETURN                   ;

FILTRO  CLRF    FILTER1          ;the converted value
CLRF    FILTER2          ;into 3 registers.
CLRF    FILTER3          ;256 times.
CLRF    VECES            ;then divide by 256
REPEAT                           ;ignoring filter3.
CALL    CONVER           ;ex:for 3FB
BSF     STATUS,RP0       ;filter1=0x003
BCF     STATUS,RP0       ;
BTFSS   STATUS,C         ;
GOTO    FILT2            ;
MOVLW   D'1'             ;
BTFSS   STATUS,C         ;
GOTO    FILT2            ;
MOVLW   D'1'             ;
FILT2                            ;
BTFSC   STATUS,C         ;
INCF    FILTER1          ;
DECFSZ  VECES,1          ;
GOTO    REPEAT           ;
;
RETURN                   ;
;
;
;
TRASMI  BTFSS   PIR1,TXIF        ;
GOTO    TRASMI           ;
MOVWF   TXREG            ;
NOP                      ;
TRASMI2 BTFSS   PIR1,TXIF        ;last bit is sent
GOTO    TRASMI2          ;
RETURN                   ;

;Main
INIT    BSF     STATUS,RP0       ;
MOVLW   0FFH             ;
MOVWF   TRISA            ;
MOVWF   TRISB            ;
MOVLW   B'11111111'      ;
MOVWF   TRISC            ;
MOVLW   B'00100110'      ;
MOVWF   TXSTA            ;
MOVLW   D'103'           ;16mhz crystal
MOVWF   SPBRG            ;9600bps. If 4mhz is used,
MOVLW   B'10000010'      ;change spbrg to (dec)25
BCF     STATUS,RP0       ;
MOVLW   B'10000001'      ;Fosc/32
BSF     PIR1,TXIF        ;
BCF     PIR1,RCIF        ;
MOVLW   B'10010000'      ;
MOVWF   RCSTA            ;
;
MOVLW   "T"              ;welcom screen on pc
CALL    TRASMI           ;
MOVLW   "X"              ;
CALL    TRASMI           ;
MOVLW   " "              ;
CALL    TRASMI           ;
MOVLW   "O"              ;
CALL    TRASMI           ;
MOVLW   "K"              ;
CALL    TRASMI           ;
MOVLW   D'13'            ;
CALL    TRASMI           ;
MOVLW   D'10'            ;
CALL    TRASMI           ;
;
;
CICLO                            ;
CALL    FILTRO           ;check for pushed keys
MOVLW   2                ;> than 2 in filter1
SUBWF   FILTER1,W        ;
BTFSS   STATUS,C         ;
GOTO    CICLO            ;
;
MOVLW   .83              ;almost 20 ms.
CALL    RETARDO          ;if 4mhz is used,
CLRF    VECES            ;change to (dec)20
CALL    FILTRO           ;
MOVLW   2                ;
SUBWF   FILTER1,W        ;
BTFSS   STATUS,C         ;
GOTO    CICLO            ;
;        CALL    TOASCII         ;un-comment this to see the
CALL    PARSER           ;key real value, so range can
CALL    FILTRO           ;check if key was released
MOVLW   0X02             ;
SUBWF   FILTER1,W        ;
BC     RELEASE           ;
;
MOVLW   ":"              ;
CALL    TRASMI           ;
MOVFW   BUFFERKEY        ;
CALL    TRASMI           ;
;        MOVFW   NIBBLE1         ;un-comment to send keys
;        CALL    TRASMI          ;values to pc screen
;        MOVFW   NIBBLE2         ;
;        CALL    TRASMI          ;
;        MOVFW   NIBBLE3         ;
;        CALL    TRASMI          ;
MOVLW   D'13'            ;
CALL    TRASMI           ;
MOVLW   D'10'            ;
CALL    TRASMI           ;
;
GOTO    CICLO            ;
;
END

```

 file: /Techref/microchip/4x4key1ad7r.htm, 14KB, , updated: 2010/7/8 16:00, local time: 2020/9/30 17:38, TOP NEW HELP FIND:  3.235.101.50:LOG IN

 ©2020 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! PIC Microcontroler 4x4 Keypad with 1 A/D pin

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

 PICList 2020 contributors: o List host: MIT, Site host massmind.org, Top posters @20200930 Bob Blick, RussellMc, madscientistatlarge, Jason White, Neil, David C Brown, Sean Breheny, Harold Hallikainen, James Cameron, Allen Mulvey, * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.