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

📄 keyboard2.asm

📁 Keil c51 写的
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;常量区
BAT_OK	equ	0x0aa
BAT_ER	equ	0x0fc
CMD_Rev_OK	equ	0xFA
CMD_Rev_Err	equ	0xFC
;DitherTime	equ	0x
Row0_7		equ	p2;
Row8_15		equ	p1;   p3;
Col0_7		equ	p0;
;命令常量
CMD_Reset	equ	0xFF 	;(Reset)
CMD_Resend	equ	0xFE 	;(Resend)
;CMD_Set_MkBk	equ	0xFC	;不发机打
;CMD_Set_MkTy	equ	0xFB 	;(Set Key Type Typematic)
CMD_Set_AllMkbkTy	equ	0xFA	;
CMD_Set_AllMk	equ	0xF9 	;(Set All Keys Make)
CMD_Set_AllMkBk	equ	0xF8	;0xF8 (Set All Keys Make/Break)
CMD_Set_AllTy	equ	0xF7 	;(Set All Keys Typematic)
CMD_Set_Default	equ	0xF6 	;(Set Default) 载入缺省的机打速率/延时10.9cps/500ms 按键类型(所有按键都使能机打/通
				;	码/断码) 以及第二套扫描码集
CMD_Dis		equ	0xF5 	;(Disable)
CMD_Ena		equ	0xF4 	;(Enable)
CMD_Set_TyRtDe	equ	0xF3 	;(Set Typematic Rate/Delay)
CMD_Read_ID	equ	0xF2 	;(Read ID)0xAB 0x83
CMD_Set_Code	equ	0xF0 	;(Set Scan Code Set)
CMD_Echo	equ	0xEE 	;(Echo)
CMD_SET_CapsNum	equ	0xED	;Caps	Num	Scroll 


KB_CLK	bit	T0;     p1.6;
KB_DATA	bit	T1;     p1.7;
Caps	bit	P1.0;     p1.3;
Numb	bit	P1.1;     p1.4;
;Numb	bit	P1.2;     p1.4;

Flag_Disable	bit	0x20.0
Flag_All_Mk	bit	0x20.1
Flag_All_Ty	bit	0x20.2
Flag_All_Bk	bit	0x20.3
Flag_Host_Request	bit	0x20.4

BusFree		bit	0x22.0
NeedResp	bit	0x22.1
CMD_Waite_Data	bit	0x22.2
IS_Break	bit	0x22.3
workLastState	equ	0x23	;暂存正在检测的行的上次状态

r0_addr	equ	0x10
r1_addr	equ	0x11
r2_addr	equ	0x12
r3_addr	equ	0x13
r4_addr	equ	0x14
r5_addr	equ	0x15
r6_addr	equ	0x16
r7_addr	equ	0x17


;变量
LastState	equ	0x30
KeyBuff		equ	0x0	;长度为16字节
KBCount		equ	0x40	;The data count in buff
KBHead		equ	0x41
KBTrail		equ	0x42
CMD		equ	0x43	;The command received from host
TypicalIntvl	equ	0x44	;The interval value of typical
TypicalDly	equ	0x45	;The delay value of typical delay
LastSendData	equ	0x46	;The Byte last send 
LastKey		equ	0x47	;The key last pressed 
TpclDly		equ	0x48	;
TpclIntvl	equ	0x49
TypicalDelay	equ	0x50
TypicalInterval	equ	0x51
;--------------------------------------------------------------
org	0x0000
	jmp	StartP
;**********************************************************
;主程式
;
;**********************************************************	
StartP	:
	clr	EA
	mov	sp	,#0x60
	mov	psw	,#0x10
	call	Delay20ms
	call	DO_Dat

MainLoop	:

	call	Check_Data_From_Host	
	call	ScanKey
	call	SendBuff

	Ajmp	MainLoop

;---------------------------------------------------
;Delay 10 us for 6M
;2005-10-16
;ice.xiang
;---------------------------------------------------
Delay10us	:
	nop
	ret

;---------------------------------------------------
;Delay 20 us for 6M
;2005-10-16
;ice.xiang
;---------------------------------------------------
Delay20us	:
	nop	;1

	nop
	nop	
	nop	;6
	nop
	nop

	ret
;---------------------------------------------------
;Delay 50 us for 6M
;2005-10-16
;ice.xiang
;---------------------------------------------------
Delay50us	:
	nop	;1

	ACall	Delay20us
	ACall	Delay20us

	ret

;---------------------------------------------------
;Delay 1 ms for 6M
;2005-10-16
;ice.xiang
;---------------------------------------------------
Delay1ms	:		;4
	push	r0_addr		;4
	
	mov	r0	,#245	;2
	djnz	r0	,$	;4*246	=984
	nop

	pop	r0_addr		;4
	ret			;4

;---------------------------------------------------
;Delay 5 ms for 6M
;2005-10-16
;ice.xiang
;---------------------------------------------------
Delay5ms	:		;4
	push	r0_addr		;4
	push	r1_addr		;4
	
	mov	r1	,#4	;2	10
	dl5ms_0	:
	mov	r0	,#250	;2*4
	djnz	r0	,$	;4*250	=1ms
	djnz	r1	,dl5ms_0;4*4
	
	mov	r0	,#237	;2
	djnz	r0	,$	;4*237	=478

	pop	r1_addr		;4	10
	pop	r0_addr		;4
	ret			;4

;*************************************************

;---------------------------------------------------
;Delay 50 ms for 6M
;2005-10-16
;ice.xiang
;---------------------------------------------------
Delay50ms	:		;4
	push	r0_addr	
	mov	r0	,#20
	d50_	:
	call	Delay5ms
	djnz	r0	,d50_
	pop	r0_addr
	ret

;---------------------------------------------------
;Send all byte in buff To Host ,except while Buss is busy
;
;2005-12-24
;ice.xiang
;---------------------------------------------------
SendBuff	:
	push	acc
	sb_next	:
	mov	a	,KBCount
	jz	sb_End

	jb	Flag_Host_Request,sb_End	;if Pc reqest to send 
	acall	Send_1Byte_To_Host
	jmp	sb_next
	sb_End	:
	pop	acc
	ret
;*************************************************


;---------------------------------------------------
;Send 1 byte in buff To Host
;
;2005-10-16
;ice.xiang
;---------------------------------------------------
Send_1Byte_To_Host	:
	push	acc
	push	r0_addr

	jnb	KB_Data	,s1th_Host_Req	;check bus is free?
	jnb	KB_CLK	,s1th_Host_Req

	mov	a	,KBCount
	jz	slth_End		;if Buff is empty then end
	mov	a	,KBTrail	;Get the head of buffer
	add	a	,#KeyBuff
	mov	r0	,a
	mov	b	,@r0		;Got the byte to send

	inc	KBTrail			;adjust the buffer Trail 
	anl	KBTrail	,#0x0f		;The buffer size is 16

	mov	a	,b		;make parity bit
	mov	c	,p
	cpl	c
	mov	r0	,#9		;send 8 bits data and 1 bit parity bit
	
	clr	KB_Data			;send start bit
	ACall	Delay20us
	clr	KB_CLK
	ACall	Delay20us
	ACall	Delay20us

	slth_Send_bits	:		;send 8bits data and 1bit parity
	setb	KB_CLK
	ACall	Delay20us
	jnb	KB_CLK	,s1th_Host_Req	;Host requset check
	rrc	a	
	mov	KB_Data	,c
	ACall	Delay20us
	clr	KB_CLK	
	ACall	Delay20us
	ACall	Delay20us

	djnz	r0	,slth_Send_bits
	
	setb	KB_CLK			;send stop bit
	ACall	Delay20us
	jnb	KB_CLK	,s1th_Host_Req
	setb	KB_DATA	
	ACAll	Delay20us
	jnb	KB_DATA	,s1th_Host_Req

	clr	KB_CLK
	ACAll	Delay20us
	ACAll	Delay20us
	
	setb	KB_CLK			;1 byte send complished
	
	dec	KBCount			;adjust the count of data in Key buffer
	Ajmp	slth_End


	s1th_Err	:
	call	delay50ms
	call	delay50ms
	Ajmp	slth_End
	s1th_Host_Req	:		;The host requse send data to device
	setb	Flag_Host_Request
	ajmp	slth_ret
	slth_End	:
	call	Delay5ms
	call	Delay5ms
	slth_ret	:
	setb	KB_CLK			;Be sure the Clock and Data bus is High
	Setb	KB_Data
	pop	r0_addr
	pop	acc
	ret

;---------------------------------------------------
;Check Data from Host
;2005-12-24
;ice.xiang
;---------------------------------------------------
Check_Data_From_Host	:
	push	b
	push	acc
	push	r0_addr	
	
	jb	KB_DATA	,cdfh_End	;Data Line is High ,Data Bus is free
	
	mov	b	,#25
	cdfh_wait_Req	:
	jb	KB_CLK	,cdfh_Req	;Wait 100us for Clock change for Low 
	djnz	b	,cdfh_wait_Req	;
	ajmp	cdfh_End		;Data=0 clk=0 is not a Requste single
	
	cdfh_Req	:
	mov	r0	,#9		;be ready to recevie 8bits data and 1bit Piriaty

	cdfh_Do_Rec	:
	clr	KB_CLK			;Recieve the 8bits data and 1 bit parity
	ACall	Delay20us
	ACall	Delay20us	

	setb	KB_CLK			
	ACall	Delay20us

	jnb	KB_CLK	,cdfh_Err	; Check Host drop the send
	rrc	a	
	mov	c,KB_Data		;Read the Data line
	ACall	Delay20us

	djnz	r0	,cdfh_Do_Rec	;Data recieve over?

	clr	KB_CLK
	ACall	Delay20us		;recieve stop bit
	ACall	Delay20us

	setb	KB_CLK
	ACall	Delay20us

	jnb	KB_CLK	,cdfh_Err	; Check Host drop the send
	jnb	KB_DATA	,cdfh_Err	; Is not sotp bits

	clr	KB_DATA			;send the response bit
	ACall	Delay20us		;Host check 
	clr	KB_CLK	
	ACall	Delay20us		;
	ACall	Delay20us			
	
	setb	KB_CLK
	ACall	Delay20us		;
	setb	KB_DATA			;a byte recieve complished
	
	Call	Parse_CMD_From_Host	

	cdfh_Err	:
	call	delay50ms
	call	delay50ms
	cdfh_End	:

	clr	Flag_Host_Request	;clear Host reqest flag
	pop	r0_addr	
	pop	acc
	pop	b
	ret



;---------------------------------------------------
;Clear Key buffer
;;2005-12-24
;ice.xiang
;---------------------------------------------------
ClearKeyBuff	:
	mov	KBHead	,#0
	mov	KBCount	,#0
	mov	KBTrail	,#0
	ret
;***************************************************
;---------------------------------------------------
;Write a byte to Key buffer
;r7	: The byte to write
;2005-10-16
;ice.xiang
;---------------------------------------------------
Write_Byte_in_Buff	:
	push	r0_addr
	push	acc
	mov	a	,#16
	clr	c
	subb	a	,KBCount
	jc	wbib_Buff_Full
	
	mov	r0	,KBHead
	mov	a	,r7
	mov	@r0	,a
	
	inc	KBCount
	inc	KBHead
	anl	KBHead	,#0x0f


	wbib_Buff_Full	:
	pop	acc
	pop	r0_addr
	ret
;---------------------------------------------------
;Initianize the paramters and close the Numb and Cap
;
;2005-10-16
;ice.xiang
;---------------------------------------------------
DO_Dat:
	setb	Caps
	setb	Numb

	;Memory	
	mov	r0	,#0x00
	mov	b	,#0x10
	iniMemLoop1	:
	mov	@r0	,#0
	djnz	b	,iniMemloop1
	;initianlize LastStates 
	mov	r0	,#LastState
	mov	b,	#16
	dat_Ini_State	:
	mov	@r0	,#0xff
	inc	r0	
	djnz	b	,dat_Ini_Sate

	mov	LastKey	,#0
	;Initializtion Typical value
	mov	TypicalDelayCount	,#Typical_Delay;#125	;equ	0x53	;机打延时计数器
	mov	TypicalDelay		,#Typical_Delay;#125		;equ	0x54	;机打延时设定值
	mov	TypicalIntervalCount	,#Typical_Inter;#25	;equ	0x55	;机打速度间隔计数
	mov	TypicalInterval		,#Typical_Inter;#25	;		;equ	0x56	;机打速度间隔设定值


	call	ClearKeyBuff
	mov	r7	,#BAT_OK
	call	Write_Byte_in_Buff
	call	Send_1Byte_To_Host
	call	Delay1ms

	clr	Caps
	clr	Numb
	
	setb	KB_CLK
	setb	KB_DATA

	ret

;---------------------------------------------------
;Parse command from host
;a	: the command to parse 
;a	: return Value if successed a = 0xFA else a=0xFC
;2005-12-24
;ice.xiang
;---------------------------------------------------
Parse_CMD_From_Host:
;------->
	push	b
	
	jnb	CMD_Waite_Data	,pcfh_Cmd_Byte
	;接收命令参数
	clr	CMD_Waite_Data
	mov	r7	,#CMD_Rev_Ok
	call	SendBuff	;send the response byte to host

	mov	b	,a
	
	mov	a	,CMD
	;设定机打延时和速率
	cjne	a	,#CMD_Set_TyRtDe	,pc_Data_CMD_Set_Code
	mov	b	,a
	anl	a	,#0x1f

	mov	TypicalIntvl	,#50

	mov	CMD	,#0
	jmp	CMD_Over
	
	;扫描码集
	pc_Data_CMD_Set_Code	:
	cjne	a	,#CMD_Set_Code	,pc_Data_CMD_SET_CapsNum
	mov	CMD	,#0
	jmp	CMD_Over

	pc_Data_CMD_SET_CapsNum	:
	cjne	a	,#CMD_SET_CapsNum	,pc_Data_default
	rrc	a	
	;scroll screen
	rrc	a	
	mov	Numb	,c
	rrc	a	
	mov	Caps	,c
	mov	CMD	,#0
	jmp	CMD_Over

	pc_Data_default:	;default hanle;
	jmp	pc_Err;
	;处理命令
	pcfh_Cmd_Byte	:
	mov	CMD	,a	;save the command 

	cjne	a	,#CMD_Reset	,_Resend_	
	call	DO_CMD_Reset	;Command Reset 
	mov	CMD	,#0
	jmp	CMD_Over
	
	_Resend_:
	cjne	a	,#CMD_Resend	,_Set_AllMkbkTy_
	call	Do_CMD_Resend	;Command Resend
	mov	CMD	,#0
	jmp	CMD_Over

	_Set_AllMkbkTy_	:
	cjne	a	,#CMD_Set_AllMkBKTY	,_CMD_Set_AllMk
	Call	do_CMD_Set_AllMkbkTy	;make ,break, typical
	;setb	CMD_Waite_Data		;wait data
	jmp	CMD_Over
	
	_CMD_Set_AllMk	:
	cjne	a	,#CMD_Set_AllMk	,_CMD_Set_AllMkBk
	call	do_CMD_Set_AllMk
;	mov	CMD	,#0
	jmp	CMD_Over
	
	_CMD_Set_AllMkBk	:
	cjne	a	,#CMD_Set_AllMkBk	,_CMD_Set_AllTy
	call	do_CMD_Set_AllMkBk
;	mov	CMD	,#0
	jmp	CMD_Over

	_CMD_Set_AllTy	:
	cjne	a	,#CMD_Set_AllTy	,_CMD_Set_Default
	call	do_CMD_Set_AllTy
;	mov	CMD	,#0
	jmp	CMD_Over


	_CMD_Set_Default	:
	cjne	a	,#CMD_Set_Default	,_CMD_Dis
	call	do_CMD_Set_Default
;	mov	CMD	,#0
	jmp	CMD_Over
	
	_CMD_Dis	:
	cjne	a	,#CMD_Dis	,_CMD_Ena
	call	do_CMD_Dis
;	mov	CMD	,#0
	jmp	CMD_Over

	_CMD_Ena	:
	cjne	a	,#CMD_Ena	,_CMD_Set_TyRtDe
	call	do_CMD_Ena
;	mov	CMD	,#0
	jmp	CMD_Over

	_CMD_Set_TyRtDe	:
	cjne	a	,#CMD_Set_TyRtDe	,_CMD_Read_ID
	call	do_CMD_Set_TyRtDe
;	mov	CMD	,#0
	jmp	CMD_Over

	_CMD_Read_ID	:
	cjne	a	,#CMD_Read_ID	,_CMD_Set_Code
	call	do_CMD_Read_ID
;	mov	CMD	,#0
	jmp	CMD_Over

	_CMD_Set_Code	:
	cjne	a	,#CMD_Set_Code	,_CMD_Echo
	call	do_CMD_Set_Code

⌨️ 快捷键说明

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