Shop OBEX P1 Docs P2 Docs Learn Events
Scanning a matrix and incrementing some values — Parallax Forums

Scanning a matrix and incrementing some values

Brian CarpenterBrian Carpenter Posts: 728
edited 2006-10-22 09:55 in General Discussion
i have been working this evening on the 4x4 matrix program that Parallax has written.
I can make the program work and understand, after much playing, how it works.· I want to be able to take this program farther though.· What i want to be able to do is to have a byte value for each key 0-16.· I want to adjust each value each time that that key is pressed.· For instance, i want an led to light once the 3 key has been pressed 15 times and a different LED to light once key 16 has been pressed 25 times.·The led will stay lit until until the system is reset.· I did not know what approach to take to keep track of these·16 variables·'Key0 - Key15' and then also let the system know what the different key press amounts are for each key.· For instance, depending on the situation, Key0 will be allowed 25 key pressed before the LED is lit but Key12 will only accept 15 before its led is lit.· I am thinking that if i use a variable called LEDS it can be a 16 bit word and after each of the Key counts reach there maximum number they will set a value in this variable.· For instance,· if key2 and key10 have both reached thier maximum value the LED Variable may look like this, 00000010 00000010· Eventual i would like to use the Pink to set teh maximum key values.· When i set up variables ofr each key i exceed the availiale ram. Any ideas will be greatly appreciated
' =========================================================================''   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

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Post Edited (altitudeap) : 10/22/2006 10:21:18 AM GMT
Sign In or Register to comment.