3x4 keypad scanning problem and others


Results 1 to 5 of 5

Threaded View

  1. #2
    Tim B's Avatar
    Tim B Guest


    Did you find this post helpful? Yes | No

    Default

    Here is a sample of code that I have written to scan a 4 x 4 keyboard just remove unwanted scans to reduce the no columns / rows

    Pbp does not have clear so replace that with ??? = 0 also it does not have break so replace that with goto label (label would be just after the endif) Dec replace with let x = x - 1

    It is quite a neat routine and debounces with out using pause, but if you want you can delete that section and use pause.

    Have you looked at my password program here ...

    http://users.picbasic.org/index.php?...cts&category=1

    Tim


    DEVICE 16F628A

    DIM KBRD_PORT AS PORTB
    DIM KBRD_PORT_TRIS AS TRISB
    DIM KBRD_PORT_COLUMNS AS %11110000 ' DEFINE THE COLUMN INPUTS
    DIM KBRD_PORT_ROWS AS %00001111 ' DEFINE THE ROW INPUTS
    DIM KBRD_ROWS AS 4 ' NUMBER OF ROWS IN PORT
    DIM DEBOUNCE_CNT_VAL AS 4

    DIM KBRD_TEMP AS BYTE ' TEMP VARS
    DIM KBRD_TEMP2 AS BYTE ' /
    DIM KEY AS BYTE ' KEY RESULT
    DIM LAST_KEY ' KEY READ IN LAST SCAN
    DIM KEY_FLAGS AS BYTE ' KEY SCAN GEN CONTROL FALGS
    DIM DEBOUNCE_CNTR AS BYTE '

    DIM KEY_PRESSED AS KEY_FLAGS.0 ' GENERAL CONTROL FLAGS
    DIM NEW_KEY AS KEY_FLAGS.1

    PORTB_PULLUPS = 1
    KBRD_PORT_TRIS = KBRD_PORT_COLUMNS ' MAKE THE COLUMNS BITS INPUTS

    GOTO START

    KEYSCAN:
    KBRD_PORT = KBRD_PORT_ROWS ^ KBRD_PORT_ROWS ' SEND ROW LINES LOW
    KBRD_TEMP = KBRD_PORT & %11110000 ' READ ALL THE COLUMNS INPUTS
    IF KBRD_TEMP <> $F0 THEN ' ONLY DO THE REST IF A KEY WAS PRESSED
    KBRD_TEMP = KBRD_ROWS ' NO OF ROWS TO SCAN
    KBRD_TEMP2 = %11111110 ' SET UP MASK
    KEY_PRESSED = 0 ' CLEAR THE FLAG TO START WITH
    CLEAR KEY
    REPEAT
    KBRD_PORT = KBRD_TEMP2 ' WRITE TO THE PORT TO SET THE ROW LOW
    INC KEY
    IF KBRD_PORT.4 = 0 THEN KEY_PRESSED = 1: BREAK ' CHECK COLUMN 1
    INC KEY
    IF KBRD_PORT.5 = 0 THEN KEY_PRESSED = 1: BREAK ' CHECK COLUMN 2
    INC KEY
    IF KBRD_PORT.6 = 0 THEN KEY_PRESSED = 1: BREAK ' CHECK COLUMN 3
    INC KEY
    IF KBRD_PORT.7 = 0 THEN KEY_PRESSED = 1: BREAK ' CHECK COLUMN 4
    DEC KBRD_TEMP
    KBRD_TEMP2 = KBRD_TEMP2 << 1 ' ROTATE THE MASK
    SET KBRD_TEMP2.0 ' SET THE BOTTOM BIT
    UNTIL KBRD_TEMP = 0 ' SCAN ALL THE ROWS
    IF KEY_PRESSED = 1 THEN KEY_SCAN_DONE ' HAS A KEY BEEN PRESSED
    ENDIF
    CLEAR KEY ' IF WE GET TO HERE NO KEY HAS BEEN PRESSED
    ' SO MAKE IT READ 0
    KEY_SCAN_DONE:
    LAST_KEY = KEY
    IF KEY = 0 THEN DEC DEBOUNCE_CNTR
    IF DEBOUNCE_CNTR = 0 THEN
    DEBOUNCE_CNTR = DEBOUNCE_CNT_VAL
    NEW_KEY = TRUE
    ELSE
    IF KEY <> LAST_KEY THEN
    DEBOUNCE_CNTR = DEBOUNCE_CNT_VAL
    NEW_KEY = TRUE
    ENDIF

    RETURN

    START:
    CLS ' CLEAR THE LCD
    WHILE 1 = 1
    GOSUB KEYSCAN
    PRINT AT 1,1 DEC KEY ' PRINT TO THE LCD
    DELAYMS 10
    WEND
    Last edited by Tim B; - 28th November 2003 at 23:48.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts