📄 ps2-scan.asm
字号:
*****************************************************************************
* Copyright (c) Motorola 1998 *
* File Name: PS2-SCAN.ASM *
* *
* Description: Keyboard matrix scanner and handler in PS/2 mode *
* *
* *
* Assembler: CASM08Z (P&E Microcomputer Systems Inc) *
* Version: 3.16 *
* *
* Current Revision: 1.0 *
* Current Revision Release Date: 2002.08.01 by Derek Lau *
* *
* Updated History *
* Rev YYYY.MM.DD Author Description of Change *
* --- ----------- ------ --------------------- *
* 0.0 1998.06.15 Tony Luk orginal release *
* 1.0 1998.08.03 Tony Luk extended key matrix to 8 x 18 *
*****************************************************************************
* This Program is a freeware to demonstrate the operation of Motorola *
* Microcontroller. Motorola reserves the right to make changes without *
* further notice to any product herein to improve reliability, function, or *
* design. Motorola does not assume any liability arising out of the *
* application or use of any product, circuit, or software described herein; *
* neither does it convey any license under its patent rights nor the rights *
* of others. Motorola products are not designed, intended, or authorized *
* for use as components in systems intended for surgical implant into the *
* body, or other applications intended to support life, or for any other *
* application in which the failure of the Motorola product could create a *
* situation where personal injury or death may occur. Should Buyer purchase *
* or use Motorola products for any such intended or unauthorized *
* application, Buyer shall indemnify and hold Motorola and its officers, *
* employees, subsidiaries, affiliates, and distributors harmless against *
* all claims, costs, damages, and expenses, and reasonable attorney fees *
* arising out of, directly or indirectly, any claim of personal injury or *
* death associated with such unintended or unauthorized use, even if such *
* claim alleges that Motorola was negligent regarding the design or *
* manufacture of the part. Motorola and the Motorola logo* are registered *
* trademarks of Motorola Inc *
*****************************************************************************
* ====== Compile Options =================================================
.CYCLE_ADDER_OFF
$BASE !10
* ====== MACRO Definition ================================================
;;$INCLUDE "macro8.asm"
.PAGE
* ====== Subroutines =====================================================
* SCAN_INIT initialize key matrix scan
* SCAN_MTX scan matrix keyboard(non-diode)
* GHOST_KEY detect ghost keys, and then mask off rows with ghost keys
* COUNT_ONE count the no. of '1' in <reg_A>
* SCAN_HDLR process scan results
* ADD_KEY add reg_A to USB keyboard report
* REMOVE_KEY remove reg_A from USB keyboard report
* BIT_TABLE return bit position in a byte
* --------------------------------------------------------------------- *
* SCAN_INIT - Initialize key matrix scan *
* In : <nil> *
* Out : b_CKPress =0 no key pressed(for curr. scan) *
* b_PKPress =0 no key pressed(for prev. scan) *
* b_Kghost =0 no ghost keys detected *
* b_Kovf =0 no kbd. report overflow *
* Q_ColMsk[0..17] column mask table(0- blank key) *
* Call : LOCATE_KEY *
* --------------------------------------------------------------------- *
PSCAN_INIT:
clr VS_Scan ; clr prev./curr. key press flags,
; clr ghost key flag,
; clr kbd report overflow flag
*
*
*
* --- Initialize Scan Results --------------------------------------------
ldx #K_ColMax ; start with last column
PSINI_PVAGN:
;; (Q_PScan[0..15] will be overwritten by Q_CScan[0..15] for next key
;; matrix scan)
;; clr {Q_PScan-1},x ; clear prev. scan results
clr {Q_CScan-1},x ; clear curr. scan results
decx
bne SINI_PVAGN ; ~last column ?
*
;; (done in TIMER_INIT routine)
;; bset b_40ms,VS_TIMER ; force first key matrix scan
*
*
*
* --- Initialize Column Mask Table ---------------------------------------
* (for blank key pos., corresp. column mask bit = 0)
clr VP_Col ; start with column 0
SINI_CAGN:
clr V_ColVal ; temp. byte for column mask
*
clr VP_Row ; start with row 0
SINI_RAGN:
*
* --- Get Key Code at Location (VP_Row,VP_Col) -----------------
lda VP_Row
ldx VP_Col
jsr LOCATE_KEY ; return key no. in V_KeyNum
*
* --- Update Temp. Column Mask ---------------------------------
clc ; clear carry bit
lda V_KeyNum
KCMPEQ KEY_NA,SINI_BlankKey ; blank key ?
sec ; set carry bit
SINI_BlankKey:
ror V_ColVal ; shift in mask bit
*
* --- Check Next Row -------------------------------------------
SINI_NROW:
inc VP_Row
ldx VP_Row
KCPXNE K_RowMax,SINI_RAGN ; ~last row ?
*
* --- Check Next Column ----------------------------------------
SINI_NCOL:
lda V_ColVal
ldx VP_Col
sta Q_ColMsk,x ; store column mask
*
inc VP_Col
ldx VP_Col
KCPXNE K_ColMax,SINI_CAGN ; ~last column ?
*
*
*
* ------------------------------------------------------------------------
PSINI_EXIT:
rts
* --------------------------------------------------------------------- *
* SCAN_MTX - scan matrix keyboard(non-diode) *
* In : b_CKPress =1 key press detected for prev. scan *
* Q_CScan[0..17] table of prev. scan result *
* Q_ColMsk[0..17] table of column mask(for blank keys) *
* Out : Q_PScan[0..17] table of prev. scan result *
* Q_CScan[0..17] table of curr. scan result *
* (if a bit is set, key at that pos. is *
* pressed.) *
* b_CKPress =1 key press detected for curr. scan *
* b_PKPress =1 key press detected for prev. scan *
* Call : Dly_20uS *
* --------------------------------------------------------------------- *
PSCAN_MTX:
*
* --- Check for Key(s) Pressed -------------------------------------------
bclr b_PKPress,VS_Scan ; push prev. key press flag
brclr b_CKPress,VS_Scan,SMTX_CKPRESS
bset b_PKPress,VS_Scan
*
* (pull low all column lines to check if any key pressed)
PSMTX_CKPRESS:
;; (ensure column data zero)
clr PTB ; put zero to column data bits
clr PTC
*
lda V_PTE
and #%11111010 ; put zero PTE0,2 col 16, 17
sta PTE
*
lda #$FF ; pull low all column lines
sta DDRB ; (related data reg. default to $00)
sta DDRC
*
bset b_COL16,DDRE ; pull low col 16
bset b_COL17,DDRE ; pull low col 17
*
jsr Dly_20uS ; wait column lines stable
*
lda Port_Row ; read back row lines
coma ; key pressed(active low)
bne PSMTX_SCAN ; key press detected ?
*
* --- Clear Current Scan Result (for no key press detected) --------------
PSMTX_RST:
bclr b_CKPress,VS_Scan ; signal no key pressed
*
ldx #K_ColMax ; start with last column
PSMTX_RNXT:
lda {Q_CScan-1},x ; copy prev. Q_CScan[x] to
sta {Q_PScan-1},x ; curr. Q_PScan[x]
*
clr {Q_CScan-1},x ; clear Q_CScan[x]
*
decx
bne PSMTX_RNXT ; ~last column ?
*
* --- Reset Keyboard Report --------------------------
brclr b_Kovf,VS_Scan,PSMTX_OVFEND ; ~kbd. report overflow ?
bclr b_Kovf,VS_Scan ; reset overflow flag
*
clr VP_KCode ; reset ptr to Q_NKey
*
ldx #K_KCsize ; clear kbd. report
PSMTX_OVFAGN:
clr {Q_KBD_RPT-1},x
decx
bne PSMTX_OVFAGN
PSMTX_OVFEND:
*
bra PSMTX_EXIT
*
*
*
* --- Scan Keys Matrix ---------------------------------------------------
PSMTX_SCAN:
bset b_CKPress,VS_Scan ; signal key pressed
*
;; (done in PSMTX_CKPRESS above)
clr PTB ; put zero to column data bits
clr PTC
*
*
KMOV %00000001,DDRB ; init. $00001 to Col[17..0]
KMOV %00000000,DDRC
bclr b_COL16,DDRE ; release high col 16
bclr b_COL17,DDRE ; release high col 17
*
clr VP_Col ; start with col #0
PSMTX_SNCOL:
jsr Dly_20uS ; wait column lines stable
*
ldx VP_Col ; get curr. col ptr
*
lda Q_CScan,x ; copy prev. Q_CScan[x] to
sta Q_PScan,x ; curr. Q_PScan[x]
*
lda Port_Row ; check row status
coma ; key pressed(active low)
and Q_ColMsk,x ; mask out blank key pos.
sta Q_CScan,x ; store result to scan buffer
*
lsl DDRB ; force low next column
rol DDRC
*
bcc PSMTX_NC16 ; reach col 16 ?
bset b_COL16,DDRE ; force low col 16
PSMTX_NC16:
KCPXNE 16,PSMTX_NC17 ; reach col 17 ?
bclr b_COL16,DDRE ; force high col 16
bset b_COL17,DDRE ; force low col 17
PSMTX_NC17:
*
inc VP_Col
ldx VP_Col
KCPXNE K_ColMax,PSMTX_SNCOL ; ~last column ?
*
*
*
* -------------------------------------------------------------------
PSMTX_EXIT:
clr DDRB ; release high all column lines
clr DDRC
*
bclr b_COL16,DDRE ; release high PTE0,2 col 16, 17
bclr b_COL17,DDRE
*
rts
* --------------------------------------------------------------------- *
* ADD_KEY - add reg_A to USB keyboard report *
* In : reg_A key# to be added *
* Out : Q_KBD_RPT update new make key *
* Call : <nil> *
* --------------------------------------------------------------------- *
PADD_KEY:
*
* --- Handle Keys Reported in Modifier Byte ------------------------------
AKEY_MODIFIER:
KCMPLO Key_LCTL,AKEY_KEYCODE ; < Left Ctrl usage index ?
KCMPHI Key_RGUI,AKEY_KEYCODE ; > Right GUI usage index ?
*
sub #Key_LCTL ; calculate index to BIT_TABLE
tax
*
lda BIT_TABLE,x ; set corresp. bit in V_MKey
ora V_MKey
sta V_MKey
*
bra PAKEY_EXIT
*
*
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -