EmbeddedRelated.com
Forums

I'm looking for a simple way to handle key presses as well as store an option to allow a repeating key or just a one time press and release. Through experimentation, I was able to capture the value while the key is held down and when the key is only pressed once. But that involves me adding an instruction and burning my code onto the chip again, but I don't want to do that.

Since each memory location in KEYS is 8 bits, I feel somehow I can change that 8-bit cyclic buffer that cycles the current button status in to a 7-bit cyclic buffer while using one bit exclusively for storing the option to have the key value returned only when the key is pressed once or while the key is held.

So I feel something in this fragment needs modification, but is there a way to do it with using as few clock cycles as possible?

        mov A,@R0    ;put value in our mini 8-bit cyclic buffer
        rlc A
        mov @R0,A

Here's my complete key scanning code. I cannot modify the first 7 lines inside the rescan loop since they load specific values from a table that makes the correct key returned.

    KEYIO equ P0
    KEYS equ 0B0h ;key storage
    ISPRESS equ 0Fh ;To avoid bouncing
    scankey:
      mov DPTR,#scans  ;to key scan table
      mov R0,#KEYS+0Dh ;14 possible keys
      rescan:
        clr A
        movc A,@A+DPTR ;load value to put on key lines to prepare matrix for that number
        mov KEYIO,A    ;store in A
        inc DPTR
        clr A             
        movc A,@A+DPTR ;xor next scan value 
        xrl A,KEYIO    ;with captured key
        inc DPTR
        ;If correct key is captured, then A=0
        dec A        ;Here we set carry in 2 clocks because A=0
        add A,#1h
        mov A,@R0    ;put value in our mini 8-bit cyclic buffer
        rlc A
        mov @R0,A
        cjne A,#ISPRESS,npress ;See if key is literally pressed 
          ;could reset @R0 here to allow multi-press
          mov A,R0             ;it is so get pointer
          subb A,#KEYS         ;and substract start of pointer
          ret                  ;A=# representing key pressed
        npress:
        dec R0                 ;Go to next key
      cjne R0,#KEYS-1,rescan ;Keep going until all keys are processed
      mov A,#0FFh            ;All keys processed, nothing captured so return FFh
    ret