File "keyS12A.asm"
Full Path: /home/analogde/www/private/Projet/Example/HC12/D256/keyS12A.asm
File size: 2.96 KB
MIME-type: text/plain
Charset: utf-8
;
; KEY12.ASM
; ---------
; KEYPAD routines for Axiom HC12 development boards.
; 912x or CML12S KEYPAD uses Port H.
;
; Variables - calling program must reserve these:
; -----------
; Global variables
; OLDKEY 1 byte, stores the previous key value for debouncing
;
; Local variables
; CNT1 1 byte, temporary debounce counter
;
; Subroutines:
; -----------
; INITKEY: Set up keypad port
; KEYSCAN: Do a quick scan of the KEYPAD. Return A=key if pressed, 0 if not
; KEYDLY: Delay, wait for lines to settle
; Define Key Character Table
_KEY_TBL: FCC 'A321B654C987D#0*'
; Initialize KEYPAD
; Port H 0-3 must be output
; 4-7 must be input with pull-down enabled
; Initialize KEYPAD
; Port H 0-3 must be output
; 4-7 must be input
INITKEY:
ldaa #$0F ; set Port H direction (0=input, 1=output)
staa DDRH ;
ldaa #$f0 ;
staa PPSH ; set for pull downs
ldaa #$f0
staa PERH ; enable pull downs
rts
;-------
GETKEY:
; jsr KEYWAIT ; wait for any keys to be released
jsr KEYSCAN ; scan keypad for new key
tsta ; key pressed?
beq GETKEY ; loop if no
rts
; Scan KEYPAD for input. return A=0 no input else A=key
; columns are active low, rows are then read active low
KEYSCAN:
movb #$0F,PTH ; set all KEYPAD columns
ldaa PTH ; get KEYPAD port
anda #$F0 ; key mask
beq KEYRESET ; branch if NO key pressed
;-------
; here if a key is pressed, determine which one
KEYSCAN1:
clr PTH ; clear all KEYPAD columns
clrb ; clear row counter
movb #$01,PTH ; set column 1
KEYLP:
jsr KEYDLY ; wait for lines to settle
ldaa PTH ; get KEYPAD port
anda #$F0 ; mask row for data
bne KEYSCAN2 ; branch if a key pressed, find row
lsl PTH ; set next column
addb #$04 ; next row count
cmpb #$10 ; check if no key found
bne KEYLP ; loop for key
;-------
KEYRESET:
clr OLDKEY ; clear any old key
KEYRST1:
movb #$10,CNT1 ; set debounce time counter
KEYRST2:
movb #$0F,PTH ; set all KEYPAD columns HIGH
clra ; clear key status
rts
; here if a key is pressed, B=column count, A=row value
KEYSCAN2:
incb ; increment key number
lsla ; shift row value to low nibble
bcc KEYSCAN2 ; loop until key found
; key found, check old or new...
cmpb OLDKEY ; test if same key
bne KEYNEW ; if not, start new key
dec CNT1 ; same key, debounce
beq KEYSAVE ; if yes, submit key
bpl KEYRST2 ; if still in debounce, wait
; key serviced, wait for release...
inc CNT1 ; prep for next cycle
bra KEYRST2 ;
KEYNEW:
stab OLDKEY ; save current key
bra KEYRST1 ; start debounce next time
KEYSAVE:
ldx #_KEY_TBL ; get start of key table
decb ; 0 offset key number
ldaa B,X ; load offset key value in a
rts ; same key pressed, already said so, return it
;-------
KEYDLY:
ldx #$20 ; clear offset count
KEYDLY1:
nop
dex ; decrement counter
bne KEYDLY1 ; loop if time not yet up
rts