⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key2.asm

📁 基于HT82K96E的usb鼠标实现源程序
💻 ASM
📖 第 1 页 / 共 2 页
字号:
include         ht82k96e.inc
;include			usbrf.inc
;include			usbfifo.inc
include				keyequ.inc



;**************************************************************************************
Scaninc				equ				pac
Scanin				equ				pa
scanout1C_adr		equ				15h
ScanOut1C			equ				pbc
ScanOut1			equ				pb
scanout2C_adr		equ				17h
ScanOut2C			equ				pcc
ScanOut2			equ				pc
    Scan2_Mask  	EQU     		0F0H		;pc4~7
scanout3C_adr		equ				19h
ScanOut3C			equ				pdc
ScanOut3			equ				pd

LED_PORT_c			equ				pcc
led_port			equ				pc			;pc1~3, Scrl, Num, Cap
	LED_Mask    	EQU     0FH	
    @LED_Num    	EQU     2               ; Num Lock LED
    @LED_Caps   	EQU     4               ; Caps Lock LEd
    @LED_Scrl   	EQU     1               ; Scroll Lock LED
	led_num			equ		pc.1
	led_cap			equ		pc.2
	led_scrl		equ		pc.3		
	led_num_c		equ		pcc.1
	led_cap_c		equ		pcc.2
	led_scrl_C		equ		pcc.3		


;KB_LED        
@Num_bit      		EQU     0               ; Num Lock LED
@Cap_bit     		EQU     1               ; Caps Lock LEd
@Scrl_bit     		EQU     2               ; Scroll Lock LED

DEBOUNCE				equ				20H
REPORT_KB_SIZE			equ				8
end_of_ScanLines		equ				20
USB_REG_BANK			equ				1
;*************************************************************************

key_var			.section		'data'
key_mdfb		db			?
key_rsvb		db			?
key_code1b		db			?
key_code2b		db			?
key_code3b		db			?
key_code4b		db			?
key_code5b		db			?
key_code6b		db			?
key_buf_end		db			?
indexbuf		db			?
buff_index		db			?
scanlineno		db			?
currentstate	db			?
currentstate1	db			?
STATE_BUF_BEG   		db     	20 dup(?)			;the buffer for keyboard scan line
STATE_BUF_END			db		?
DebounceCounter 		db		?
scantemp				db		?
GhostScanLine   		db     	?
BitMap          		db     	?	
ChangeKeyMap    		db		?	
KeyNo           		db		?	
KeyBit          		db		?		
temp1					db		?	
KB_LED					db		?

bPhantom		dbit
bKB_KeyUpt		dbit

public			bKB_KeyUpt
public			kb_led


;public			key_mdfb		
;public			key_rsvb		 
;public			key_code1b		 
;public			key_code2b		 
;public			key_code3b		 
;public			key_code4b		 
;public			key_code5b		 
;public			key_code6b		 
;public			key_buf_end
;public			buff_index		 
;public			indexbuf		 
;public			scanlineno		 
;public			currentstate	 
;public			currentstate1	 
;public			STATE_BUF_BEG   		;the buffer for keyboard scan line
;public			STATE_BUF_END			
;public			DebounceCounter 		
;public			scantemp				
;public			GhostScanLine   		
;public			BitMap          		
;public			ChangeKeyMap    			
;public			KeyNo           			
;public			KeyBit          				
;public			temp1						
;public			KB_LED
;public			key_rpt_cnt


;*************************************************************************
extern			exit_of_examine_kb_state	:near

;extern			KEY_MdfB      	:byte		
;extern			KEY_RsvB      	:byte	
;extern			KEY_Code1B     	:byte		
;extern			KEY_Code2B     	:byte		
;extern			KEY_Code3B     	:byte		
;extern			KEY_Code4B     	:byte		
;extern			KEY_Code5B     	:byte		
;extern			KEY_Code6B     	:byte		
;extern			KEY_BUF_END		:byte

extern			FIFO_OUT1		:byte
extern			fifo_out2		:byte
extern			fifo_out3		:byte
extern			fifo_out4		:byte
extern			fifo_out5		:byte
extern			fifo_out6		:byte
extern			fifo_out7		:byte
extern			fifo_out8		:byte
extern			FIFO_SendLen	:byte


;extern			BUFF_Index		:byte
;extern			IndexBuf		:byte
;extern			scanlineno		:byte
;extern			CurrentState	:byte
;extern			CurrentState1	:byte
;extern			STATE_BUF_BEG	:byte
;extern			DebounceCounter	:byte
;extern			ScanTemp		:byte
;extern			GhostScanLine	:byte
;extern			BitMap			:byte
;extern			ChangeKeyMap	:byte
;extern			KeyNo			:byte
;extern			Keybit			:byte
;extern			temp1			:byte
;


kb_code			.section		'code'
;----------------------------------------------------------------------------
;scan key, get data, parsing & send data
;maybe need check suspend
examine_kb_state:
;				clr		key_modify

;				clr		break_key
                CLR     WDT
    ;set scan line to low
                ;Get the port and bit no. of scan line
                MOV     A,ScanLineNo      	;
                CALL    GetScanPortAndBit   ;Output: PortNo & BitNo
    ;Delay
                MOV     A,10                ;Delay 30 intruction time
                CALL    DelayNInst          ;   about 20 us in 6 M sysclk
    ;get key state of scan line
                MOV     A,ScanIn          	;Get key state
                MOV     CurrentState,A    	;Save key state

                MOV     A,ScanLineNo      	;get current line state pointer
                ADD     A,OFFSET STATE_BUF_BEG    
                MOV     MP0,A               ;
                MOV     A,R0                ;get current line state data
                XOR     A,CurrentState    	;Whether current line no change ?
                SZ      Z                   ;No
                JMP     ScanOneRowOK        ;Yes
    ;first or not
                SZ      DebounceCounter   	;first ?
                JMP     NotFirstKeyChanged  ;No
FirstKeyChanged:                            ;Yes
                CLR     DebounceCounter   	;Reset DebounceCounter to 0
                INC     DebounceCounter   	;Update debounce counter
                MOV     A,CurrentState    	;save current key state to
                MOV     CurrentState1,A   	;  template buffer
                JMP     Exit_Scan_Keyboard  ;to next scan
NotFirstKeyChanged:
                MOV     A,CurrentState    	;Is any key state changed ?
                XOR     A,CurrentState1   	;
                SNZ     Z                   ;
                JMP     FirstKeyChanged     ;Yes
                                            ;No
                INC     DebounceCounter   	;Update debounce counter
                MOV     A,DEBOUNCE          ;>= debounce time ?
                SUB     A,DebounceCounter 	;
                SZ      C                   ;
                JMP     Exit_Scan_Keyboard  ;No
                CLR     DebounceCounter   	;Yes: debounce has finished and
                                            ;     reinitilize


;----------------------------------------------------------------------------
;Debounce finished
;check Phantom key
CheckPhantom:
                CLR     WDT
    ;How many key pressed ?
                MOV     A,08H       		;Scan in numbers = 8
                MOV     Temp1,A    			;
                CLR     ScanTemp  			;initial number of keys pressed to 0
                MOV     A,CurrentState    	;current key pressed state
CheckPhantomLp:
                RRC     ACC             	;check every column by right rotated
                SNZ     C               	;key pressed ?
                INC     ScanTemp      		;Yes: Update the number of keys pressed
                SDZ     Temp1          		;
                JMP     CheckPhantomLp  	;
                MOV     A,ScanTemp    		;ScanTemp = the number of keys pressed
                AND     A,0FEH          	;Key pressed numbers >= 2 ?
                SZ      Z               	;
                JMP     CheckPhantomOK  	;No (Have phantom key is impossible)
                                        	;Yes (Maybe have phantom key)
    ;Scan line to found phantom key
                CLR     GhostScanLine 		;scan from line 0
GhostLoop:
                CLR     WDT
    ;Whether phost scan line is equal to current scan line ?
                MOV     A,ScanLineNo
                XOR     A,GhostScanLine
                SZ      Z
                JMP     NextLine            ;Yes
    ;set scan line to low
                ;Get the port and bit no. of scan line
                MOV     A,GhostScanLine   	;
                CALL    GetScanPortAndBit   ;Output: PortNo & BitNo
   ;get key state of scan line
                MOV     A,10                ;Delay 30 intruction time
                CALL    DelayNInst          ;   about 20 us in 6 M sysclk
                MOV     A,ScanIn          ;Get key state
                MOV     CurrentState1,A   	;Save key state
    ;check the same 0 value position between CurrentState and CurrentState1
    ;the 0 value of result means the same 0 value position
                OR      A,CurrentState
                MOV     BitMap,A          	;Save result to buffer
                XOR     A,0FFH              ;Have any 0 value ?
                SZ      Z                   ;
                JMP     NextLine            ;No
    ;How many 0 counts in BitMap          	;Yes
                MOV     A,08H
                MOV     ScanTemp,A
                CLR     ACC
BitMapCountLp:
                RRC     BitMap
                SNZ     C
                INC     ACC                 ;0 value counts
                SDZ     ScanTemp
                JMP     BitMapCountLp
                AND     A,0FEH              ;0 value counts >= 2 ?
                SNZ     Z                   ;
                JMP     FoundPhantom        ;Yes
                                            ;No
    ;-------- this block maybe not need ---------------------------------
    ;Whether CurrentState1 is equal to CurrentState ?             		;
    ;If no, phantom keys is found.                                    	;
                MOV     A,CurrentState                              	;
                XOR     A,CurrentState1                             	;
                SNZ     Z                                             	;
                JMP     FoundPhantom        ;No                       	;
    ;--------------------------------------------------------------------
;----------------------------------------------------------------------------

NextLine:
                INC     GhostScanLine     	;to scan next line
                MOV     A,end_of_ScanLines       	;End scan line ?
                XOR     A,GhostScanLine   	;
                SNZ     Z                   ;
                JMP     GhostLoop           ;No (Continue)
                JMP     CheckPhantomOK      ;Yes

;----------------------------------------------------------------------------
FoundPhantom:
;Get phantom key index & save to report buffer
                SZ      bPhantom
                JMP     Exit_Scan_Keyboard
                SET     bKB_KeyUpt    ;Key is changed
                SET     bPhantom      ;Found a phantom key
                JMP     Error_Exit_Keyboard
;----------------------------------------------------------------------------

CheckPhantomOK:
                CLR     WDT
                MOV     A,ScanLineNo      			;Get the buffer pointer of
                ADD     A,OFFSET STATE_BUF_BEG     	;   current scan line state
                MOV     MP0,A               		;
    ;found any changed
                MOV     A,CurrentState    	;Any key changed in current scan
                XOR     A,R0                ;  line
                MOV     ChangeKeyMap,A    	;
                CLR     KeyNo             	;bit counter
                MOV     A,80H               ;1000 0000b
                MOV     KeyBit,A          	;Set first bit to check
CheckChangeKeyLp:
                RL      KeyBit            	;Update check bit order
                RRC     ChangeKeyMap      	;check changed key bitmap
                SNZ     C                   ;changed ?
                JMP     NotChanged          ;No
                                            ;Yes

;Get key index from key index table and save it to keyboard buffer.
    ;Get scan line base address
				rla		ScanLineNo
				rl		acc
				rl		acc
                ADD     A,KeyNO           	;
                ADD     A,LOW KBIndexORG    ;
                CALL    GetKeyboardIndex    ;Get keyboard index code in ACC
                MOV     IndexBuf,A  		;Save keyboard index code to IndexBuf

                MOV     A,IndexBuf        	;Is modifier key ?
                SUB     A,0E0H              ;
                SNZ     C
                JMP     NotModifierKey
                MOV     A,IndexBuf        	;Is modifier key ?
                SUB     A,0E8H              ;
                SNZ     C
                JMP     ModifierKey

;Not a modifier key
NotModifierKey:
    ;Check press or release
                SNZ     CurrentState.0    	;press or release ?
                JMP     MakeCode            ;press

;				set		break_key
;release Code:
    ;to find the keyboard index in keyboard buffer and remove it
                CALL    RemoveKey           ;Remove ACC key index from
                                            ;   keyboard buffer, if found
                SNZ     Z                   ;Any one moved ?
            	JMP     NotChanged          ;No(means buffer still full)
                                           	;Don't check current line again
    ;to sort keyboard buffer
                CALL    SortKeyboardbuf     ;reorder all 0 value in keyboard
                JMP     UpdateState
MakeCode:
                MOV     A,BUFF_Index      	;keyboard buffer Full ?
                XOR     A,OFFSET KEY_BUF_END       ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -