;Name:      main
;
;Author:    Dan Kohn
;
;Date:      April 2, 2009
;
;function:  to test and demonstrate the get_key subroutine.


outa    equ        $FFB8


        org        $D000
main:

        jsr get_key
        jsr outa

        jmp main


;Name:      get_key
;
;Author:    Dan Kohn
;
;Date:      April 2, 2009
;
;Note:      This code was originally KPAD4x4.asm writen by Wytec as a demo
;           program but was modified to act as a keypad entry subroutine.
;
;Function:  To read the 4x4 keypad and return the ASCII representation of
;           the key pressed in REG A.
;
;           The function waits for a key to be pressed AND released.
;
;On Return: Reg A holds the ascii for the key pressed.
;
;I/O pins used
;
; The following signal definitions only apply to Wytec 4X4 membrane keypad:

; PD2 connects COL0 of the keypad via pin 1 of the 8-pin keypad header J6
; PD3 connects COL1 of the keypad via pin 2 of the 8-pin keypad header J6
; PD4 connects COL2 of the keypad via pin 3 of the 8-pin keypad header J6
; PD5 connects COL3 of the keypad via pin 4 of the 8-pin keypad header J6

; PE0 connects ROW0 of the keypad via pin 5 of the 8-pin keypad header J6
; PE1 connects ROW1 of the keypad via pin 6 of the 8-pin keypad header J6
; PE2 connects ROW2 of the keypad via pin 7 of the 8-pin keypad header J6
; PE3 connects ROW3 of the keypad via pin 8 of the 8-pin keypad header J6

; The PE0-PE3 has a 100K pull-up resistor in each line.



porta:                equ        0
portd:                equ        8
ddrd:                equ        9
porte:                equ        $0a
REGBLK:                equ        $1000


temp:           rmb     1

TEN_MS:                equ        2500        ; 2500 x 8 cycles = 20,000 cycles = 10ms

* keypad scan for Wytec 4X4 membrane keypad
* at exit:  if a key is down, the carry bit =1 and the accumulator b
*            holds the key number
*            if no key is dwon the carry bit =0
*
keybrd:        ldx        #REGBLK                                                 n
        ldab        #3
*        ldaa        #11011111b
        ldaa        #$df            ; starting with pd5 is low
        staa        temp
next_row:
        staa        portd,x
        nop                        ; add delay before checking key down
        nop
        nop
        nop
        ldaa        porte,x
        anda        #$0f            ; needs PE0-PE3 only
        cmpa        #$0f
        bne        keyin                ; key input detected
        decb
        cmpb        #$ff                ; after 4 tests, accu B will be $ff
        beq        no_keyin
         ror        temp             ; check next column
        ldaa        temp
        jmp        next_row
*
no_keyin:
        clc
        rts                        ; no key input

keyin:         rora
        bcc        key_ok
        addb    #4
        jmp     keyin
key_ok: sec
        rts
*
delay_10ms:
        pshy
        ldy     #TEN_MS                ; 2500 x 8 = 20,000 cycles = 10ms 
del1:        dey                        ; 4 cycles
        nop                        ; 1 cycle
        bne        del1                ; 3 cycles
        puly
               rts

*
get_key:
        pshx
        pshy
        pshb

*        lds        #STACK
        ldx        #REGBLK
*        ldaa        #11111111b
        ldaa        #$ff
        staa        portd,x
*        ldaa         #11111110b                ; 0 =input, 1 = output
        ldaa         #$fe                        ; PD0=input
        staa         ddrd,x
*
begin:
        jsr        keybrd
        bcc        nokey
           jsr        delay_10ms
        bcc        nokey
* key down
not_zero:
        tba           ;store key pressed in REG A
        adda #$30     ;convert to ascii
        cmpa #$39
        
        ble number    ;if digit is not 0-9
        adda #$07     ;convert to letter A-F
        
number: psha
        
upkey:  ldaa #%11000011   ;wait for key to be released

        staa portd,x
        ldaa porte,x
        anda #$0f
        cmpa #$0f
        bne upkey
        
        pula
        pulb
        puly
        pulx
        rts
*
nokey:  jsr        delay_10ms
        jmp        begin
*
         end
