Shop OBEX P1 Docs P2 Docs Learn Events
Keypad 3x4 for SX chip — Parallax Forums

Keypad 3x4 for SX chip

dbjdbj Posts: 75
edited 2006-10-30 11:19 in General Discussion
·Could the code from this post see·File link·be modified to work on a
3x4 keypad? If so would you just delete the "A" "B" "c" and "d" in the lookup table, and change the scan lines p0-p2 outputs and p4-p6 as imputs, how would the DIRL= %00000111 is this correct, dosent seem right this code is writen for BS2 when I convert over to SX it gets an error
on X VAR Byte and·i Var Nib is that because the memory is not set up the same on the SX chip.· Thanks Dave

File:
http://forums.parallax.com/showthread.php?p=570329

Post Edited (dbj) : 10/30/2006 4:45:23 AM GMT

Comments

  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2006-10-30 11:19
    Try this code (attachment) I wrote based off of the SX/B example help code (below), for an old 3x5 keypad from Radio Shack. It also sends the character out serially that you can connect to a BS2.
    [img]http://forums.parallax.com/attachment.php?attachmentid=73897[/img]
    
    [img]http://forums.parallax.com/attachment.php?attachmentid=73898[/img]
    
    ' =========================================================================''   File...... KEYPAD.SXB'   Purpose... Scanning a 4x4 Matrix Keypad'   Author.... (c) Parallax, Inc. -- All Rights Reserved'   E-mail.... support@parallax.com'   Started...'   Updated... 05 JUL 2006'' =========================================================================' -------------------------------------------------------------------------' Program Description' -------------------------------------------------------------------------'' This program demonstrates the scanning of a 4x4 matrix keypad. If no' key is pressed the GET_KEY routine will return a value of 16.'' Key values (hex):''      C1    C2    C3    C4'' R1  [noparse][[/noparse] 0 ] [noparse][[/noparse] 1 ] [noparse][[/noparse] 2 ] [noparse][[/noparse] 3 ]'' R2  [noparse][[/noparse] 4 ] [noparse][[/noparse] 5 ] [noparse][[/noparse] 6 ] [noparse][[/noparse] 7 ]'' R3  [noparse][[/noparse] 8 ] [noparse][[/noparse] 9 ] [noparse][[/noparse] A ] [noparse][[/noparse] B ]'' R4  [noparse][[/noparse] C ] [noparse][[/noparse] D ] [noparse][[/noparse] E ] [noparse][[/noparse] F ]' -------------------------------------------------------------------------' Device Settings' -------------------------------------------------------------------------DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONXFREQ            4_000_000ID              "KEYPAD"' -------------------------------------------------------------------------' IO Pins' -------------------------------------------------------------------------Keys            VAR     RC                      ' keyboard scan portTRIS_Keys       VAR     TRIS_CPLP_Keys        VAR     PLP_CCol1            VAR     Keys.7                  ' column inputsCol2            VAR     Keys.6Col3            VAR     Keys.5Col4            VAR     Keys.4LEDs            VAR     RBTRIS_LEDs       VAR     TRIS_B' -------------------------------------------------------------------------' Constants' -------------------------------------------------------------------------Yes             CON     0                       ' active low inputNo              CON     1Dash            CON     %01000000               ' segment G only' -------------------------------------------------------------------------' Variables' -------------------------------------------------------------------------theKey          VAR     Byte                    ' from keypad, 0 - 16row             VAR     Byte                    ' keyboard scan rowtmpB1           VAR     Byte                    ' subroutine work varstmpB2           VAR     BytetmpW1           VAR     Word' =========================================================================  PROGRAM Start' =========================================================================' -------------------------------------------------------------------------' Subroutine Declarations' -------------------------------------------------------------------------GET_KEY         SUB     0                       ' get key from padDELAY           SUB     1, 2                    ' delay in milliseconds' -------------------------------------------------------------------------' Program Code' -------------------------------------------------------------------------Start:  LEDs = Dash                                   ' dash in display  TRIS_LEDs = %00000000                         ' LED pins are outputsMain:  theKey = GET_KEY                              ' get a key  IF theKey < 16 THEN                           ' was a key pressed?    READ ReMap + theKey, theKey                 ' yes, remap keypad    READ Digits + theKey, LEDs                  ' output to display    DELAY 100  ELSE    LEDs = Dash  ENDIF  GOTO Main' -------------------------------------------------------------------------' Subroutine Code' -------------------------------------------------------------------------' This routine works by activating each row, then scanning each column.' If a particular row/column junction is not active (pressed), the key' value is incremented and the scan continues.  As soon as a key is found,' the routine exits.  If no key is pressed the routine will exit with a key' value of 16.'' Use: aByte = GET_KEY' -- scans keyboard and places key value into 'aByte'GET_KEY:  tmpB1 = 0                                     ' reset keyboard value  Keys = %0000_0111                             ' activate first row  TRIS_Keys = %1111_0000                        ' refresh IO state  PLP_Keys = %0000_1111                         ' pull-up input pins  FOR tmpB2 = 1 TO 4                            ' scan four rows    IF Col1 = Yes THEN EXIT                     ' check buttons on column    INC tmpB1                                   ' update key value    IF Col2 = Yes THEN EXIT    INC tmpB1    IF Col3 = Yes THEN EXIT    INC tmpB1    IF Col4 = Yes THEN EXIT    INC tmpB1    Keys = Keys >> 1                            ' select next row    Keys = Keys | %0000_1000                    ' clear previous row  NEXT  RETURN tmpB1' -------------------------------------------------------------------------' Use: DELAY ms' -- 'ms' is delay in milliseconds, 1 - 65535DELAY:  IF __PARAMCNT = 1 THEN    tmpW1 = __PARAM1                            ' save byte value  ELSE    tmpW1 = __WPARAM12                          ' save word value  ENDIF  PAUSE tmpW1  RETURN' =========================================================================' User Data' =========================================================================' Matrix to remap keypad valuesReMap:  DATA   1,  2,  3, $A  DATA   4,  5,  6, $B  DATA   7,  8,  9, $C  DATA  $E,  0, $F, $D' Seven-segment digit mapsDigits:'        .gfedcba  DATA  %00111111                               ' 0  DATA  %00000110                               ' 1  DATA  %01011011                               ' 2  DATA  %01001111                               ' 3  DATA  %01100110                               ' 4  DATA  %01101101                               ' 5  DATA  %01111101                               ' 6  DATA  %00000111                               ' 7  DATA  %01111111                               ' 8  DATA  %01100111                               ' 9  DATA  %01110111                               ' A  DATA  %01111100                               ' B  DATA  %00111001                               ' C  DATA  %01011110                               ' D  DATA  %01111001                               ' E  DATA  %01110001                               ' F
    
    
    
    
    365 x 49 - 2K
    106 x 36 - 360B
Sign In or Register to comment.