SX/B Example: 4x4 Matrix Keypad | Examples Index |
' ========================================================================= ' ' 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 [ 0 ] [ 1 ] [ 2 ] [ 3 ] ' ' R2 [ 4 ] [ 5 ] [ 6 ] [ 7 ] ' ' R3 [ 8 ] [ 9 ] [ A ] [ B ] ' ' R4 [ C ] [ D ] [ E ] [ F ] ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 ID "KEYPAD" ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- Keys VAR RC ' keyboard scan port TRIS_Keys VAR TRIS_C PLP_Keys VAR PLP_C Col1 VAR Keys.7 ' column inputs Col2 VAR Keys.6 Col3 VAR Keys.5 Col4 VAR Keys.4 LEDs VAR RB TRIS_LEDs VAR TRIS_B ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- Yes CON 0 ' active low input No CON 1 Dash CON %01000000 ' segment G only ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- theKey VAR Byte ' from keypad, 0 - 16 row VAR Byte ' keyboard scan row tmpB1 VAR Byte ' subroutine work vars tmpB2 VAR Byte tmpW1 VAR Word ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Subroutine Declarations ' ------------------------------------------------------------------------- GET_KEY SUB 0 ' get key from pad DELAY SUB 1, 2 ' delay in milliseconds ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: LEDs = Dash ' dash in display TRIS_LEDs = %00000000 ' LED pins are outputs Main: 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 - 65535 DELAY: 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 values ReMap: DATA 1, 2, 3, $A DATA 4, 5, 6, $B DATA 7, 8, 9, $C DATA $E, 0, $F, $D ' Seven-segment digit maps Digits: ' .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